Insert a single row and return its primary key

前端 未结 3 1832
太阳男子
太阳男子 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:39

    Here is another alternative I think is very clean. It uses OUTPUT syntax and the Result Set instead of Parameter Mapping, which requires less configuration.

    insert dbo.ImportData (EndDate) 
    output inserted.Id 
    values (NULL);
    
    • Replace Id in this statement with the name of your identity column.
    • Use "Single row" as the Resultset type.
    • No parameter mappings
    • Add this to the Result Set tab: Result Name: 0 (meaning the first column) and select your variable name e.g. User::tablePk (Variable type: Int32)

提交回复
热议问题