-1

我在数据库中有一个表(日志表,它已作为 ssis 错误处理和日志记录的一部分创建)e。我在此表中插入一行并尝试使用 scope_identity() 从表中获取最后一个标识值但是我的 scope_identity?() 为NULL。请参阅 dml 语句和用作一个存储过程的一部分的 scope_identity() 语句

insert into logging.execution_log
            (ParentLogID, Description, PackageName, PackageGuid, MachineName, ExecutionGuid, LogicalDate, Operator, StartTime, EndTime, Status,FailureTask)
        select @ParentLogID, @Description, @PackageName, cast(@PackageGuid as uniqueidentifier), @MachineName, cast(@ExecutionGuid as uniqueidentifier),
                @logicalDate, @operator, getdate(), null, @status, null



**set @LogID = cast(scope_identity() as int)**

但是当我尝试获取 @logid 的值时,它只返回 null

请注意,在logging.execution_log 表的LogId列上已经有一个标识规范

问候

*其他详细信息(我刚刚完成一些测试后才明白) 作为测试的一部分,我手动插入了一些新行,如下所示插入 logging.execution_log(ParentLogID、描述、PackageName、PackageGuid、MachineName、ExecutionGuid、LogicalDate、Operator , StartTime, EndTime, Status,FailureTask)

    select 1, 2, 3, **NEWID()**,
     5, **NEWID()**,
            7, 8, getdate(), null, 0, null

select scope_identity()
select * from logging.execution_log

然后它显示身份的最后一个值............很好,但问题是当我将 PackageGuid 作为system::Packageid(ssis 系统变量)和 ExecutionGuid 作为system::executioninstanceGUID然后它插入表中的行正确,但我无法使用 scope_identi 或 @@identity 或 ident_current 获取最后一个标识值(全部显示为空)*

4

2 回答 2

1

使用输出到子句:

insert into logging.execution_log
            (ParentLogID, Description, PackageName, PackageGuid, MachineName, ExecutionGuid, LogicalDate, Operator, StartTime, EndTime, Status,FailureTask)
        output LogID into @LogID
        select @ParentLogID, @Description, @PackageName, cast(@PackageGuid as uniqueidentifier), @MachineName, cast(@ExecutionGuid as uniqueidentifier),
                @logicalDate, @operator, getdate(), null, @status, null
于 2012-06-15T08:49:06.637 回答
0
begin try
    Set XACT_ABORT ON
    BEGIN TRAn
            INSERT into logging.execution_log
                (ParentLogID, Description, PackageName, PackageGuid, MachineName, 
                ExecutionGuid, LogicalDate, Operator, StartTime, EndTime, Status,FailureTask)
            Values(@ParentLogID, @Description, @PackageName, 
                cast(@PackageGuid as uniqueidentifier), @MachineName, 
                cast(@ExecutionGuid as uniqueidentifier),  @logicalDate, 
                @operator, getdate(), null, @status, null)

            Set @LogID = scope_identity()
    Commit Tran
End try

Begin Catch
    Select Error_Message();
    Rollback Tran
End Catch
于 2012-06-15T09:06:27.297 回答