Click here to Skip to main content
15,888,144 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
Posted

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.
 
Share this answer
 
you can solve it by creating a stored procedure with your queries. sample one looks like below one.
SQL
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900