SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query is updating all records, not one


Query is updating all records, not one

Author
Message
otgmcse
otgmcse
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 38
I have a question on a query...Im trying to update a value from a table, but it should only update top 1. Here's my query:

UPDATE T_VM_QUEUE SET STATUS_CD = 'P' WHERE STATUS_CD = (SELECT TOP (1) STATUS_CD FROM T_VM_QUEUE WHERE Dialer_Tbl_Nme = '5010_5010' ORDER BY ImportRuleDate ASC)



Instead of updating Top 1, it updates all the records. What the heck am I doing wrong?
otgmcse
otgmcse
Forum Newbie
Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)Forum Newbie (6 reputation)

Group: General Forum Members
Points: 6 Visits: 38
figured it out from stackoverflow

WITH CTE AS (SELECT TOP (1) * FROM T_VM_QUEUE WHERE STATUS_CD = 'P' AND Dialer_Tbl_Nme = '5010_5010' ORDER BY ImportRuleDate ASC) UPDATE CTE SET STATUS_CD = 'S'
Ed Wagner
Ed Wagner
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16655 Visits: 10070
Your first query was updating T_VM_QUEUE where STATUS_CD was equal to the value returned by the subquery used in the where clause. The second one is updating only one row because only one row is returned by the CTE. That's a really cool feature of the CTE, and you can use it to perform deletes as well.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
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