Getting @@IDENTITY from TableAdapter

前端 未结 7 2175
自闭症患者
自闭症患者 2020-12-19 20:21

I am trying to complete a seemingly simple task that has turned into a several hour adventure: Getting @@Identity from TableAdapter.Insert().

7条回答
  •  旧时难觅i
    2020-12-19 20:48

    Here's my SQL Code that works.

    CREATE PROCEDURE [dbo].[Branch_Insert]
    (
        @UserId uniqueidentifier,
        @OrganisationId int,
        @InsertedID int OUTPUT
    )
    AS
        SET NOCOUNT OFF;
    INSERT INTO [Branch] ([UserId], [OrganisationId]) 
    VALUES (@UserId, @OrganisationId);
    
    SELECT Id, UserId, OrganisationId FROM Branch WHERE (Id = SCOPE_IDENTITY())
    SELECT @InsertedID = SCOPE_IDENTITY()
    

    Then when I create the Table Adapter - I can instantly see the @InsertedID parameter.

    Then from code, all I do is:

    int? insertedId = 0;
    branchTA.Insert(userId, orgId, ref insertedId);
    

    I'm not 100% whether using ref is the best option but this works for me.

    Good luck.

提交回复
热议问题