HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to get last inserted id and insert it into another table using stored procedure?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
storedlastinsertintoinsertedproceduregetanotherusinghow

Problem

Here is my SP.

ALTER PROCEDURE [dbo].[My_SP]
    -- Add the parameters for the stored procedure here
    @ClientID bigint,
    @CreatedBy int =null,
    @CreatedDateTime datetime = null,
    @ModifiedBy int = null,
    @ModifiedDateTime datetime = null,
    @Name nvarchar(255)

    SET NOCOUNT ON;

    INSERT INTO dbo.Clients
    (
    CreatedBy,
    CreatedDateTime,
    ModifiedBy,
    ModifiedDateTime,
    Name,
    Active
    )
    VALUES
    (
    @CreatedBy,
    @CreatedDateTime,
    @ModifiedBy,
    @ModifiedDateTime,
    @Name,
    1
    )

    SELECT @Name AS ClientName

    INSERT INTO BusinessRules (
                        ClientId,
                        ProjectId,
                        TemplateName,
                        Revised,
                        Serial,
                        Topic,
                        QualityItem,
                        CriticalToQC,
                        [Description],
                        OwnerNotes,
                        RelevantDocs,
                        WhereToCheck) SELECT 
            @ClientID, 
            null,
            b.TemplateName, 
            b.Revised,
            b.Serial,
            b.Topic, 
            b.QualityItem,
            b.CriticalToQC,
            b.[Description],
            b.OwnerNotes,
            b.RelevantDocs,
            b.WhereToCheck
    FROM BusinessRules AS b WHERE  ProjectID = 394


I need to use the inserted @ClientID in the next INSERT INTO statementas ClientId.

How can I achieve this?

Solution

Simply. Just add SET @ClientID = SCOPE_IDENTITY() after first INSERT statement. So you don't need @ClientID input parameter.

ALTER PROCEDURE dbo.My_SP
-- Add the parameters for the stored procedure here
--@ClientID         BIGINT,
@CreatedBy        INT           = NULL,
@CreatedDateTime  DATETIME      = NULL,
@ModifiedBy       INT           = NULL,
@ModifiedDateTime DATETIME      = NULL,
@Name             NVARCHAR(255)

AS
     SET NOCOUNT ON;

     DECLARE @ClientID as BIGINT

     INSERT INTO dbo.Clients
     (CreatedBy,
      CreatedDateTime,
      ModifiedBy,
      ModifiedDateTime,
      Name,
      Active
     )
     VALUES
     (@CreatedBy,
      @CreatedDateTime,
      @ModifiedBy,
      @ModifiedDateTime,
      @Name,
      1
     );

     SET @ClientID = SCOPE_IDENTITY();

     SELECT @Name AS ClientName;

     INSERT INTO BusinessRules
     (ClientId,
      ProjectId,
      TemplateName,
      Revised,
      Serial,
      Topic,
      QualityItem,
      CriticalToQC,
      Description,
      OwnerNotes,
      RelevantDocs,
      WhereToCheck
     )
            SELECT @ClientID,
                   NULL,
                   b.TemplateName,
                   b.Revised,
                   b.Serial,
                   b.Topic,
                   b.QualityItem,
                   b.CriticalToQC,
                   b.Description,
                   b.OwnerNotes,
                   b.RelevantDocs,
                   b.WhereToCheck
            FROM BusinessRules AS b
            WHERE ProjectID = 394;

Code Snippets

ALTER PROCEDURE dbo.My_SP
-- Add the parameters for the stored procedure here
--@ClientID         BIGINT,
@CreatedBy        INT           = NULL,
@CreatedDateTime  DATETIME      = NULL,
@ModifiedBy       INT           = NULL,
@ModifiedDateTime DATETIME      = NULL,
@Name             NVARCHAR(255)

AS
     SET NOCOUNT ON;

     DECLARE @ClientID as BIGINT

     INSERT INTO dbo.Clients
     (CreatedBy,
      CreatedDateTime,
      ModifiedBy,
      ModifiedDateTime,
      Name,
      Active
     )
     VALUES
     (@CreatedBy,
      @CreatedDateTime,
      @ModifiedBy,
      @ModifiedDateTime,
      @Name,
      1
     );

     SET @ClientID = SCOPE_IDENTITY();

     SELECT @Name AS ClientName;

     INSERT INTO BusinessRules
     (ClientId,
      ProjectId,
      TemplateName,
      Revised,
      Serial,
      Topic,
      QualityItem,
      CriticalToQC,
      Description,
      OwnerNotes,
      RelevantDocs,
      WhereToCheck
     )
            SELECT @ClientID,
                   NULL,
                   b.TemplateName,
                   b.Revised,
                   b.Serial,
                   b.Topic,
                   b.QualityItem,
                   b.CriticalToQC,
                   b.Description,
                   b.OwnerNotes,
                   b.RelevantDocs,
                   b.WhereToCheck
            FROM BusinessRules AS b
            WHERE ProjectID = 394;

Context

StackExchange Database Administrators Q#158779, answer score: 4

Revisions (0)

No revisions yet.