Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 从上一个insert语句获取最后一个插入的id_Sql_Sql Server_Sql Server 2008_Tsql_Stored Procedures - Fatal编程技术网

Sql 从上一个insert语句获取最后一个插入的id

Sql 从上一个insert语句获取最后一个插入的id,sql,sql-server,sql-server-2008,tsql,stored-procedures,Sql,Sql Server,Sql Server 2008,Tsql,Stored Procedures,我有一个带有两个insert语句的存储过程,其中我想将第一个insert语句的ID插入第二个insert语句 CREATE PROC [dbo].[Log_Action] @action_description VARCHAR(MAX), @creator_id INT, @entity VARCHAR(50), @entity_identifier UNIQUEIDENTIFIER AS BEGIN DECLARE @return_value BIT;

我有一个带有两个insert语句的存储过程,其中我想将第一个insert语句的ID插入第二个insert语句

CREATE PROC [dbo].[Log_Action] 
    @action_description VARCHAR(MAX),
    @creator_id INT,
    @entity VARCHAR(50),
    @entity_identifier UNIQUEIDENTIFIER
AS
BEGIN
    DECLARE @return_value BIT;

    BEGIN TRY

        INSERT INTO Action_Lookup (action_description)
        VALUES (@action_description);

        INSERT INTO Audit ([user_id], action_id, CREATED, [guid], entity, entity_identifier)
        VALUES (@creator_id, SCOPE_IDENTITY(), GETDATE(), NEWID(), @entity, @entity_identifier);

        SET @return_value = 1;
        RETURN @return_value;

    END TRY
    BEGIN CATCH

        SET @return_value = 0;
        RETURN @return_value;

    END CATCH
END

SCOPE_IDENTITY()
返回null的问题,我也尝试了
@@IDENTITY
IDENT_CURRENT
但不起作用。

尝试
输出
子句:

CREATE PROC [dbo].[Log_Action]
    @action_description VARCHAR(MAX),
    @creator_id INT,
    @entity varchar(50),
    @entity_identifier uniqueidentifier
AS
BEGIN
    DECLARE @return_value bit;

    BEGIN TRY

    INSERT INTO Action_Lookup (action_description)
    OUTPUT
        @creator_id,
        inserted.[id], -- in [] there should be actual name of identity column
        GETDATE(),
        NEWID(),
        @entity,
        @entity_identifier
        INTO Audit ([user_id], action_id, created, [guid], entity, entity_identifier) 
    VALUES (@action_description);

        set @return_value = 1;
        return @return_value;
    END TRY
    BEGIN CATCH
        set @return_value = 0;
        return @return_value;
    END CATCH
END

尝试
输出
子句:

CREATE PROC [dbo].[Log_Action]
    @action_description VARCHAR(MAX),
    @creator_id INT,
    @entity varchar(50),
    @entity_identifier uniqueidentifier
AS
BEGIN
    DECLARE @return_value bit;

    BEGIN TRY

    INSERT INTO Action_Lookup (action_description)
    OUTPUT
        @creator_id,
        inserted.[id], -- in [] there should be actual name of identity column
        GETDATE(),
        NEWID(),
        @entity,
        @entity_identifier
        INTO Audit ([user_id], action_id, created, [guid], entity, entity_identifier) 
    VALUES (@action_description);

        set @return_value = 1;
        return @return_value;
    END TRY
    BEGIN CATCH
        set @return_value = 0;
        return @return_value;
    END CATCH
END

您确定表操作\u查找中的ID列已声明为标识吗?@Ricardo是的,它是标识。您是否尝试在插入操作\u查找后立即将标识值放入变量中,然后使用此变量的值?或者在
Action\u Lookup
table上有任何触发器吗?这个逻辑似乎在SQL FIDLE中工作(举个简单的例子)。您的第一个插入查询“Action\u Lookup”是否插入任何记录?可能会发生这样的情况:语句中可能有错误,因此不会插入任何记录,因此所有三个都不起作用。是否确定表Action_Lookup中的ID列声明为identity?@Ricardo是的,它是identity。您是否尝试在插入Action_Lookup后立即将identity值放入变量中,然后使用这个变量的值是多少?或者在
Action\u Lookup
table上有任何触发器吗?这个逻辑似乎在SQL FIDLE中工作(举个简单的例子)。您的第一个插入查询“Action\u Lookup”是否插入任何记录?可能会发生这样的情况:语句中可能有错误,因此不会插入任何记录,因此所有三个都不起作用。我尝试了它,发现了此错误“OUTPUT-INTO子句的目标表“Audit”不能位于(主键、外键)关系的任一侧。找到了引用约束“FK__Audit_uAction_uID_u19dfd96b”这是真的,表审计中的action_id引用了表action_lookupah中的id,我明白了,这是有道理的。我尝试了它,得到了这个错误“OUTPUT-INTO子句的目标表'Audit'不能位于(主键,外键)关系的任一侧。找到了引用约束'FK_Audit_action_id_u19dfd96b',这是真的,表审计中的action_id引用了表action_lookupah中的id,我明白了,这是有道理的。