Insert a single row and return its primary key

前端 未结 3 1831
太阳男子
太阳男子 2020-11-30 13:34

In SSIS, How do I use the execute SQL task to insert a single row with no parameters and get the primary key back so I can set it to user variable? My insert query is simply

3条回答
  •  無奈伤痛
    2020-11-30 14:24

    Alternatively to bilinkc's version, without parameters:

    Execute SQL Task

    General tab ResultSet: Single Row

    SQL

    INSERT INTO dbo.ImportData (EndDate) VALUES (NULL);
    SELECT SCOPE_IDENTITY() AS LastId
    

    In the mapping of the single-row result set, enter LastId in the result name box, and map to your variable.

    May well be marginally faster with a single output parameter (bilinkc's version), depends on how SSIS does it 'under the covers' and whether it creates a full datareader versus a single sp_ExecuteSQL call with output parameter.

提交回复
热议问题