Note that there are some explanatory texts on larger screens.

plurals
  1. POSQl Server Express 2005 - updating 2 tables and atomicity?
    primarykey
    data
    text
    <p>First off, I want to start by saying I am not an SQL programmer (I'm a C++/Delphi guy), so some of my questions might be really obvious. So pardon my ignorance :o)</p> <p>I've been charged with writing a script that will update certain tables in a database based on the contents of a CSV file. I have it working it would seem, but I am worried about atomicity for one of the steps: One of the tables contains only one field - an int which must be incremented each time, but from what I can see is not defined as an identity for some reason. I must create a new row in this table, and insert that row's value into another newly-created row in another table.</p> <p>This is how I did it (as part of a larger script):</p> <pre><code>DECLARE @uniqueID INT, @counter INT, @maxCount INT SELECT @maxCount = COUNT(*) FROM tempTable SET @counter = 1 WHILE (@counter &lt;= @maxCount) BEGIN SELECT @uniqueID = MAX(id) FROM uniqueIDTable &lt;----Line 1 INSERT INTO uniqueIDTableVALUES (@uniqueID + 1) &lt;----Line 2 SELECT @uniqueID = @uniqueID + 1 UPDATE TOP(1) tempTable SET userID = @uniqueID WHERE userID IS NULL SET @counter = @counter + 1 END GO </code></pre> <p>First of all, am I correct using a "WHILE" construct? I couldn't find a way to achieve this with a simple UPDATE statement.</p> <p>Second of all, how can I be sure that no other operation will be carried out on the database between Lines 1 and 2 that would insert a value into the uniqueIDTable before I do? Is there a way to "synchronize" operations in SQL Server Express?</p> <p>Also, keep in mind that I have no control over the database design.</p> <p>Thanks a lot!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload