SQL Server游标结束事务

时间:2016-11-22 09:26:22

标签: sql sql-server transactions cursor

我有文章'ArticleQuantity'的数量表。文章是从其他文章构建的,我们将它们命名为SourceArticles。 因此,当用户想要构建文章时,我必须使用游标进行事务处理:

 - for every needed SourceArticles check if its sum in 'ArticlesQuantity' > 0:
 - if there's not enough quantity of SourceArticles end transaction and remove some documents;
 - else remove SourceArticles, add Article, update some documents;

问题是 - 游标如何“制动”交易。 Raiserror会不会足够?回滚后我必须删除在事务之前创建的文档,是否可以在Raiserror之后?或者更好的是创建一个标志并在光标中更新它:

- TRANSACTION
- flag = 0
- for every needed SourceArticles check if its sum in 'ArticlesQuantity' > 0:
- if not, flag = 1
- and later, if flag == 1:
     - there's not enough quantity of SourceArticles so remove some documents, COMMIT TRAN;
     - else remove SourceArticles, add Article, update some documents, COMMIT TRAN;

或者有没有办法可以在没有游标的情况下执行此操作?像临时表一样

SourceArticle_ID, SourceArticle_Needed, SourceArticle_InStorage, [SourceArticle_InStorage - SourceArticle_Needed]

然后检查最后一列值=> 0

0 个答案:

没有答案
相关问题