Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update Statement

Author  Topic 

jonsey54
Starting Member

4 Posts

Posted - 2008-05-17 : 11:11:17

Hi,

I am a little confued with the below SQL. Based on the where clause of the update statement I was expecting the @products table to be updated with the maximum version of the label (i.e., version 2) but that isn't the case. Could anybody please explain?

Many Thanks,


declare @products table(
productuid int,
labeluid int
)

insert into @products values (46047,-1)
insert into @products values (46047,-1)

declare @labels table(
productuid int,
labeluid int,
ctype varchar(10),
version int
)

insert into @labels values (46047,123,'landscape',1)
insert into @labels values (46047,345,'portrait',2)

update ct
set ct.labeluid = cv.labeluid
from @products ct
join @labels cv on ct.productuid = cv.productuid
where cv.version = (
select max(version) from @labels cv2
where cv.productuid = cv2.productuid
and cv.ctype = cv2.ctype
)

select * from @products

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-17 : 12:05:35
But there is no version column in the @Products table. I am not sure what you want.

Can you please post expected output?

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

jonsey54
Starting Member

4 Posts

Posted - 2008-05-17 : 15:19:33
In the update statement I am doing a join with the labels table based on the productuid. In the same statement I am also specifying the latest version of the label in the where clause. So I would've expected the output

46047 345
46047 345

the 345 coming from the join with the labels table and taking the max version of the label.

Many Thanks
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2008-05-17 : 15:24:54
..and I am getting the exact output you specified.

productuid  labeluid    
----------- -----------
46047 345
46047 345

(2 row(s) affected)


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-17 : 15:31:34
quote:
Originally posted by jonsey54

In the update statement I am doing a join with the labels table based on the productuid. In the same statement I am also specifying the latest version of the label in the where clause. So I would've expected the output

46047 345
46047 345

the 345 coming from the join with the labels table and taking the max version of the label.

Many Thanks


I dont think you will get both the labeluids as 345 as you've a check condition and cv.ctype = cv2.ctype in the last subquery. The first join yioelds both values from @labels (123 & 345). The second condition you're trying to return the max version of records for each ctype. Since each ctype is different (portrait,landscape) they will yield different records and you will get 123,345 as two ids in output. However,if you drop this condition check you will be getting the same id which is the one corresponding to max version.
Go to Top of Page

jonsey54
Starting Member

4 Posts

Posted - 2008-05-18 : 06:57:45
Thanks for the replies.

Harsh, I am still getting 123 returned as the label in my result sets. I don't understand how you are getting 345.

I still don't understand why I'm getting 123.

visakh16, could you explain a little more what you would expect to see as the output and why it would be like that. I understand what you mean by it would return the max version for each type but then I thought ut would return 4 results instead of 2, e.g.,

productuid labeluid
----------- -----------
46047 123
46047 345
46047 123
46047 345

Many Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-18 : 13:20:20
quote:
Originally posted by jonsey54

Thanks for the replies.

Harsh, I am still getting 123 returned as the label in my result sets. I don't understand how you are getting 345.

I still don't understand why I'm getting 123.

visakh16, could you explain a little more what you would expect to see as the output and why it would be like that. I understand what you mean by it would return the max version for each type but then I thought ut would return 4 results instead of 2, e.g.,

productuid labeluid
----------- -----------
46047 123
46047 345
46047 123
46047 345

Many Thanks



What you're doing is updating your product table which has only 2 records so definitely there would be only 2 rows in output. The value coming for labelid cant be guaranteed it can be any of these combinations 123,123;123,345;345,345;345,123. This is because the first join will give you 4 results which on applying subquery condition will give you your above result (4 records above) which is used for updating products. so based on what order they are fetched (which we can never guarantee) the update takes any of four values.
Go to Top of Page
   

- Advertisement -