Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

A little T-SQL syntax help please Expand / Collapse
Author
Message
Posted Friday, January 10, 2014 8:43 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:57 AM
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)

Post #1529845
Posted Friday, January 10, 2014 9:00 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Yesterday @ 8:53 AM
Points: 605, Visits: 3,534
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..
Post #1529857
Posted Friday, January 10, 2014 9:03 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 7:09 PM
Points: 593, Visits: 931
Can you join Productss (is it really spelled with two S's ) 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
Post #1529859
Posted Friday, January 10, 2014 9:05 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 7:09 PM
Points: 593, Visits: 931
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
Post #1529862
Posted Friday, January 10, 2014 10:22 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 10:57 AM
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.
Post #1529893
Posted Friday, January 10, 2014 10:27 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

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

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

But yea, I suppose can join Products.

Post #1529897
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse