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
in my if exists section. How do I change my stored procedure in the correct way ?
Each procedure has one or more statements. In our case, these are SQL statements. So, you can write a procedure that will – insert new data, update or delete existing, retrieve data using the SELECT statement. And even better, you can combine more (different statements) in the stored procedures.
To run a query with multiple statements, ensure that each statement is separated by a semicolon; then set the DSQEC_RUN_MQ global variable to 1 and run the query. When the variable is set to zero, all statements after the first semicolon are ignored.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With