I have an insert stored procedure with incremented Id (int). I’ll like to take that Id and insert it to another table and also one of variable from the insert stored procedure.
I know you can do select from inserted but if I’m not mistaking that allowed you to select values from the inserted row.
Is there anyway to persevere the variable after executed the stored procedure and pass it to trigger?
Edit: sorry, let me included the stored procedure that I’m using. Table foo have a Id that is auto incremented and I want a trigger to insert the foo’s Id and the @mid to another table after this stored procedure is run. However as you can see that @mid is not being insert to table foo.
ALTER PROCEDURE [dbo].[foo]
@userName varchar(50),
@somefoo varbinary(max),
@mid int
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO fooTable(UserName, SomeFoo)
VALUES (@userName, @somefoo);
END
The inserted pseudo-table allows you to select the value that is now in the table. The deleted pseudo-table allows you to select the value that used to be in the table before the insert happened. Since you say insert proc i am assuming it’s not an insert/update proc. I am not clear on what you want to do. You want the trigger to copy the identity field and one other column? Easy. You want the trigger to copy the identity field and a proc parameter that is not actually stored in the table? Impossible – but if that’s what you want, then you can do it in the proc instead.
1) Trigger:
2) Proc: