I was trying to insert a new row into two tables which has a relationship between. I wrote the stored procedure as follows.
ALTER PROCEDURE InsertUserProfile
You need to capture the auto-incremented value that you get when you insert into the first table, tbl_user_login. After you capture it, you need to use it to insert into the second table.
DECLARE @ID int
BEGIN TRANSACTION
INSERT INTO tbl_user_login VALUES (@UserID, @Pass, @Enabled, @Permission, @Rank)
SET @ID = SCOPE_IDENTITY()
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
INSERT INTO tbl_user_profile VALUES (@ID, @FName, @LName, @Phone, @Email1, @Email2)
IF @@ERROR <> 0
BEGIN
ROLLBACK
RETURN
END
COMMIT