hi
how to write a single stored procedure to insert values into two tables at a time. columns are different in two tables. Please give a stored procedure, urgent please help me.
Thanks.
How to write stored procedure to insert values into two tables?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

VasanthRank #1,486 · 1.1K reputationPosted Apr 10, 2010, 4:09 AM EDT
CREATE PROCEDURE dbo.InsertTwoTables
@address1 nvarchar(500),
@mobileno nvarchar(50),
AS
BEGIN TRY
BEGIN TRANSACTION -- Start the transaction
Set Nocount On
DECLARE @ID int
INSERT INTO Table1(Address, MobileNo) VALUES (@address1, @mobileno)
SELECT @ID=@@IDENTITY
INSERT INTO ImageInAlbum(FKID, Address, MobileNo) VALUES (@ID, @address1, @mobileno)
-- If we reach here, success!
COMMIT
END TRY
BEGIN CATCH
-- Error
IF @@TRANCOUNT > 0
ROLLBACK
-- Raise an error
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH
Hope it helps out , please mark as answer.
Altaf Patel72 reputationPosted Apr 29, 2010, 6:56 PM EDT
CREATE PROCEDURE sp_myStoredProcedure @myInput int, @myString varchar(100), @myFloat AS beginSelect column1, column2 From Table1 Where column1 = @myInput Select column1, column2 From Table2 Where column1 = @myString and column2 = myFloat end