开发者

Making a sequence of SQL statements atomic

I need to make the following sequence of steps in a sproc atomic. Here is an approximate simplified example:

Customesrs table (CustomerId,..,OrderMax)

Products table (ProductId,...)

AvailableProducts view (ProductId and other properties)

Orders (CustomerId,OrderId)

  1. select @OrderMax from the Customers table
  2. select TOP @Ordermax from AvailableProducts view
  3. update some properties in the Products based on the result s开发者_Go百科et of step 2
  4. insert orders into Orders table (based on the result set of step 2)
  5. return/select orders that were inserted

As I understand, there has to be a transaction on the whole thing and UPDLOCK. What has to be secured is Products table for update and Orders table for insert. However, the rows are queried from the view that is constructed from both of these tables.

What is the right way to make this sequence atomic and secure update and insert on the above tables?


Need to wrap all you logic in a Begin Transaction, Commit Transaction. The update / insert does not really care if the data came from a join unless it somehow created a situation where it could not roll back the transaction but it would have to get real messy to create a situation like that. If 3. and 4. have complex logic you may be forced into a cursor or .NET (but you can do some pretty complex logic with regular queries).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜