Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simultaneous Select/Update in one SP

Status
Not open for further replies.

benjatado

Programmer
May 15, 2005
52
0
0
US
Hi,

I have a SP that I am trying to achieve simultaneous select and update of the same (Top 30) IDs within a table.
Code:
SELECT TOP 30  [ID]
FROM   Incoming
WHERE  Processing = 'N' 

UPDATE Incoming set Processing = 'Y' 
where Incoming.[ID] in (SELECT TOP 30  [ID]
FROM   Incoming
WHERE  Processing = 'N')
The question is, will this update return the same IDs as the first select statement?

Is there a way to assign a variable within the first select statement and then pass those values to the UPDATE?

Thanks,

-b
 
How are you using TOP 30 without ORDER BY ?

I believe it would be the same, but you can create a temp table to be sure.
 
You should definitely have the same order by clause in your select statements. That would ensure that the same 30 Ids are returned every time. You also don't need the first select, update...select should work by itself.
 
Better yet, put the thirty rows in a temp table or table variable and join to that. Then you know you have the same thirty rows. Otherwise concurrent changes by other users could change the thirty selected even with the order by.

"NOTHING is more important in a database than integrity." ESquared
 
Good point - perhaps this thing should be wrapped in transaction?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top