如何在单个事件中更新2个表数据.....? [英] how to update 2 tables data in single event.....?
本文介绍了如何在单个事件中更新2个表数据.....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在单个事件中执行两个查询..我该怎么做...应该通过执行两个差异查询来实现..还是我可以使用更新查询.hav将其连接起来..am来更新2个表...
i would like to execute two queries in single event..how can i do that...should i do it by executing two diff queries..or can i concatenate it..am using update queries.hav to update in 2 tables...
推荐答案
只需将两个更新查询放在一条语句中.但是请注意:如果第二个查询失败,则第一个查询已准备就绪.如果要回滚,则必须使用事务.
Just put the two update queries in one statement. But be aware: If the second query fails, the first one is allready executed. If you want to roll this back you have to use transactions.
您可以通过使用查询创建存储过程来解决该问题.示例一看起来像下面的一个.
you can solve it by creating a stored procedure with your queries. sample one looks like below one.
CREATE PROCEDURE [sp_UpdateGRN]
(
@parameter1 nvarchar,
@parameter2 nvarchar,
.
.
.
@parametern nvarchar,
)
AS
BEGIN
BEGIN TRANSACTION
BEGIN TRY
INSERT INTO [tblTransaction]
(Field1,Field2,......Fieldn)
VALUES
(@parameter1,@parameter2,......@parametern)
INSERT INTO [tblStock]
(Field1,Field2,......Fieldn)
VALUES
(@parameter1,@parameter2,......@parametern)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage
END CATCH
END
请根据您的需要进行更改.
please do make/changes based on your needs.
这篇关于如何在单个事件中更新2个表数据.....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文