I have a C# program that inserts into a table that has an identity column (id).
C# code
String sql = "insert into my_table (col_a,col_b) values (@val_a,@val_b); select @@identity;";
SqlCommand cmd = new SqlCommand(sql,conn);
...
... // parameters code
...
Int inserted_id = cmd.ExecuteScalar(); // Exception!
SQL Trigger
CREATE TRIGGER [dbo].[trg_my_trigger] ON [dbo].[my_table]
WITH EXEC AS CALLER
AFTER INSERT
AS
begin
declare @row_id int;
select @row_id = id from inserted;
insert into log_table(remarks1,remarks2) values ('new row inserted',@row_id);
end
GO
The code works fine, but the trigger causes the @@identity
to be that of the log_table
insert statement.
So I added this line at the bottom of the trigger:
begin
declare @row_id int;
select @row_id = id from inserted;
insert into log_table(remarks1,remarks2) values ('new row inserted',@row_id);
select @row_id; -- this is causing the error
end
GO
Now the trigger raises an error:
A trigger returned a resultset and the server option 'disallow results from triggers' is true.
I don't have access to modify server variables. All I want is for the trigger to insert into a log, then the inserted id be returned to c#.. How can I do that?