一个查询的执行取决于另一个查询 [英] Execution of one query depends on the other query

查看:69
本文介绍了一个查询的执行取决于另一个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在存储过程中有以下2个查询:

Dear All,

I have the below 2 queries in a stored procedure:

insert into Names(PhysicianId,FName,LName) values(@PhysicianId,@FName,@LName)

insert into dummy(DummyName,DummyCity) values('xxx','yyy')



在这里,仅当第一个查询执行时,第二个查询才应执行.
也就是说,当代码到达具有相应参数的存储过程时,并且如果第一个查询有任何问题,第二个查询也将不被执行.


有什么办法吗?

非常感谢您对此的建议.


谢谢



Here, the second query should get executed if and only if the first query executes.
i.e., when the code reaches the stored procedure with the respective parameters and if there is any issue with the first query, the second query also should not get executed.


Is there any way for doing this?

I would greatly appreciate your suggestion on this.


Thanks

推荐答案

您可以使用Tansaction


像这样


You can use Tansaction


Like this


BEGIN TRANSACTION;

BEGIN TRY
   insert into Names(PhysicianId,FName,LName) values(@PhysicianId,@FName,@LName)
 
   insert into dummy(DummyName,DummyCity) values('xxx','yyy')END TRY
BEGIN CATCH
    SELECT
        ERROR_NUMBER() AS ErrorNumber
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage;

    IF @@TRANCOUNT > 0
        ROLLBACK TRANSACTION;
END CATCH;

IF @@TRANCOUNT > 0
    COMMIT TRANSACTION;
GO


这篇关于一个查询的执行取决于另一个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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