I am trying to find the id value of the inserted record inserted by exec (@Sql), but it seems that exec () is being calculated in another area.
declare @Sql varchar(512)
set @Sql = 'insert into [dbo].[__Test] ([description]) values (''Some text'')'
exec (@Sql)
select
@@IDENTITY as [@@IDENTITY],
scope_identity() as [scope_identity],
ident_current('__Test') as [ident_current]
returns:
@@IDENTITY scope_identity ident_current
---------- -------------- -------------
14 NULL 14
and if there is a trigger in __Test, it returns:
@@IDENTITY scope_identity ident_current
---------- -------------- -------------
6 NULL 14
So, @@ IDENTITY can be a trigger insert, execution is not in scope, but ident_current () can be from another user.
Is there any way to reliably find the identifier value from the insert made by exec ()?
source
share