Sql Server return the value of identity column after insert statement

前端 未结 7 673
甜味超标
甜味超标 2020-12-13 17:44

Is it possible in sql server using stored procedure to return the identity column value in a table against which some values are inserted? For example using stored procedure

相关标签:
7条回答
  • 2020-12-13 18:49

    You can explicitly get back Identity columns using SqlServer's OUTPUT clause: Assuming you have an identity/auto-increment column called ID:

    $sql='INSERT INTO [xyz].[myTable] (Field1, Field2, Field3) OUTPUT Inserted.ID VALUES  (1, 2, '3')';
    

    Note that in Perl DBI you would then execute the INSERT statement, just like it was a SELECT. And capture the output like it was a SELECT

    $sth->execute($sql);
    @row=$sth->fetchrow_array; #Only 1 val in 1 row returned
    print "Inserted ID: ", $row[0], "\n";
    

    Presumably this is preferable because it doesn't require another request.

    0 讨论(0)
提交回复
热议问题