如何为事务编写存储过程 [英] how to write a stored procedure for transactions

查看:62
本文介绍了如何为事务编写存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表名称clienttest,我想使用存储过程从其他数据中插入一些数据.该数据具有forloop.now现在如何编写事务查询,如果数据中有任何错误,则不要插入将数据插入表中,否则应插入并提交数据.在这方面请提供帮助.

Hi I have a table name clienttest in that i want to insert some data using storedprocedure from antoher data.The data has a forloop.now how can write a transcation query that if any error is there in the data then dont insert the data into the table else data should be inserted and commited.please help in this regard

推荐答案

BEGIN TRANSACTION myTrans
...
COMMIT TRANSACTION myTrans


示例:
-- SP
BEGIN TRAN
    UPDATE Authors
    SET Phone = '415 354-9866'
    WHERE au_id = '724-80-9391'

    SELECT * FROM Authors

    UPDATE Publishers
    SET city = 'Calcutta', country = 'India'
    WHERE pub_id = '9999'
COMMIT TRAN


此处:
MSDN:开始交易(Transact-SQL) [ SQL Server事务和错误处理 [


Here:
MSDN: BEGIN TRANSACTION (Transact-SQL)[^]
SQL Server Transactions and Error Handling[^]


这篇关于如何为事务编写存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆