exec (@Sql)에 의해 삽입 된 삽입 된 레코드의 ID 값을 찾으려고 시도하지만 exec()가 다른 범위에서 실행하는 것처럼 보입니다.exec (@Sql)을 호출 한 후 ID 값을 얻는 방법
/*
create table [dbo].[__Test](
[id] [int] IDENTITY(1,1) NOT NULL,
[description] [varchar](100) NULL
) ON [PRIMARY]
GO
*/
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]
/*
if exists(select * from sys.objects where object_id = object_id('[dbo].[__Test]') and type='U')
drop table [dbo].[__Test]
GO
*/
반환 :
@@IDENTITY scope_identity ident_current
---------- -------------- -------------
14 NULL 14
및 __Test에 트리거가있는 경우, 반환
@@IDENTITY scope_identity ident_current
---------- -------------- -------------
6 NULL 14
그래서 @@ IDENTITY 트리거 삽입 될 수있는, 실행이 범위에 포함되지이다 ident_current()는 다른 사용자의 것이 될 수 있습니다.
exec()로 만든 삽입물에서 신원 값을 안정적으로 찾는 방법이 있습니까? sp_executesql을을 사용하여
, 감사합니다. – avenmore