Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


A little T-SQL syntax help please


A little T-SQL syntax help please

Author
Message
igloo21
igloo21
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 112
Hello-
I’m having a minor issue and I’m having trouble figuring out why. So basically all I want to do is update one column using the UPDATE and SELECT below. I’m expecting to update roughly 17,600 records which is what I get back when I run just the SELECT without the UPDATE. When I run the script with the UPDATE included I end up updating over 55,000 rows (in my test environment of course )
I think it may be something I’m doing wrong in the UPDATE part. Can someone please give me a hint what I’m doing wrong?
UPDATE Productss
SET HotLeadParticipant=0 WHERE EXISTS(
SELECT DISTINCT

a.BoxID
,a.BoxName
,p.ProductsID
,p.ProductsName
,p.HotLeadParticipant
FROM
dbo.ValidProducts vp
INNER JOIN dbo.BoxProducts ap
ON vp.BoxProductID = ap.BoxProductID
INNER JOIN dbo.Boxs a
ON ap.BoxID = a.BoxID
INNER JOIN dbo.tblProducts p
ON vp.ProductID = p.ProductsD
WHERE a.BoxID IN (297,
318,
337,
354,
397,
398,
504,
535,
557,
604,
733)


davidandrews13
davidandrews13
SSC Eights!
SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)SSC Eights! (826 reputation)

Group: General Forum Members
Points: 826 Visits: 4450
i'm presuming that the code in your EXISTS statement always brings back at least one row?
it looks like, because it always brings back data, the EXISTS statement will always come back TRUE.

so your basically saying:

UPDATE Productss
SET HotLeadParticipant=0
WHERE
1 = 1 --this will always be true

that means your updating every record in your table..
Keith Tate
Keith Tate
SSChasing Mays
SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)

Group: General Forum Members
Points: 620 Visits: 979
Can you join Productss (is it really spelled with two S's :heheSmile to the select statement i.e.:
UPDATE pdt
SET pdt.HotLeadParticipant=0
FROM
dbo.ValidProducts vp
INNER JOIN dbo.BoxProducts ap
ON vp.BoxProductID = ap.BoxProductID
INNER JOIN dbo.Boxs a
ON ap.BoxID = a.BoxID
INNER JOIN dbo.tblProducts p
ON vp.ProductID = p.ProductsD
INNER JOIN dbo.Productss pdt
ON vp.ProductID = pdt.ProductID
WHERE a.BoxID IN (297,
318,
337,
354,
397,
398,
504,
535,
557,
604,
733)





Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
Keith Tate
Keith Tate
SSChasing Mays
SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)SSChasing Mays (620 reputation)

Group: General Forum Members
Points: 620 Visits: 979
igloo21 (1/10/2014)
Hello-
I’m having a minor issue and I’m having trouble figuring out why. So basically all I want to do is update one column using the UPDATE and SELECT below. I’m expecting to update roughly 17,600 records which is what I get back when I run just the SELECT without the UPDATE. When I run the script with the UPDATE included I end up updating over 55,000 rows (in my test environment of course )
I think it may be something I’m doing wrong in the UPDATE part. Can someone please give me a hint what I’m doing wrong?
UPDATE Productss
SET HotLeadParticipant=0 WHERE EXISTS(
SELECT DISTINCT

a.BoxID
,a.BoxName
,p.ProductsID
,p.ProductsName
,p.HotLeadParticipant
FROM
dbo.ValidProducts vp
INNER JOIN dbo.BoxProducts ap
ON vp.BoxProductID = ap.BoxProductID
INNER JOIN dbo.Boxs a
ON ap.BoxID = a.BoxID
INNER JOIN dbo.tblProducts p
ON vp.ProductID = p.ProductsD
WHERE a.BoxID IN (297,
318,
337,
354,
397,
398,
504,
535,
557,
604,
733)

Also remember that it is only going to update the number of products which might be less then the number of rows returned in the select statement because there might be more than one row per product.



Microsoft Certified Master - SQL Server 2008
Follow me on twitter: @keith_tate

Forum Etiquette: How to post data/code on a forum to get the best help
igloo21
igloo21
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 112
davidandrews13 (1/10/2014)
i'm presuming that the code in your EXISTS statement always brings back at least one row?
it looks like, because it always brings back data, the EXISTS statement will always come back TRUE.

so your basically saying:

UPDATE Productss
SET HotLeadParticipant=0
WHERE
1 = 1 --this will always be true

that means your updating every record in your table..


Yea I believe your correct now that I think about it. Blush
igloo21
igloo21
SSC Rookie
SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)SSC Rookie (33 reputation)

Group: General Forum Members
Points: 33 Visits: 112
[quote]Keith Tate (1/10/2014)
Can you join Productss (is it really spelled with two S's :heheSmile

Actually no it's not spelled with two s's I did some code scrubbing before posting it and I missed that:-D.

But yea, I suppose can join Products.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search