SQL和存储过程中的多条语句 [英] SQL and multiple statements in stored procedure
本文介绍了SQL和存储过程中的多条语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 SQL Server 2005,我有一个非常简单的存储过程:
I'm working on SQL server 2005 and I have a very simple stored procedure:
create PROCEDURE [dbo].[tblTabel_Insert]
@ID int,
@Code nvarchar(50) = null
AS
SET NOCOUNT ON;
IF EXISTS (SELECT ID, code FROM tblTabel WHERE ID = @ID and code = @Code)
UPDATE tblTabel SET ID = @ID,code = @Code WHERE ID = @ID
ELSE
BEGIN
INSERT INTO tblTabel (ID,code) VALUES ( @ID ,@Code);
END
我的问题是:在我的存储过程中可以有多个查询吗?我想添加行
My question is: is it posible to have multiple queries in my stored procedure ? I want to add the lines
UPDATE tblTabelB SET ID = @ID,code = @Code WHERE ID = @ID
UPDATE tblTabelC SET ID = @ID,code = @Code WHERE ID = @ID
在我的 if exists 部分.如何以正确的方式更改我的存储过程?
in my if exists section. How do I change my stored procedure in the correct way ?
推荐答案
把它们放在 BEGIN 和 END 之间
Just put them between BEGIN and END
IF EXISTS (SELECT ID, code FROM tblTabel WHERE ID = @ID and code = @Code)
BEGIN
UPDATE tblTabel SET ID = @ID,code = @Code WHERE ID = @ID
UPDATE tblTabelB SET ID = @ID,code = @Code WHERE ID = @ID
UPDATE tblTabelC SET ID = @ID,code = @Code WHERE ID = @ID
END
ELSE
BEGIN
INSERT INTO tblTabel (ID,code) VALUES ( @ID ,@Code);
END
这篇关于SQL和存储过程中的多条语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文