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.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VasanthPosted Apr 10, 2010, 8:09 AM
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 PatelPosted Apr 29, 2010, 10:56 PM