Sql Server 在插入语句后返回标识列的值 [英] Sql Server return the value of identity column after insert statement

查看:51
本文介绍了Sql Server 在插入语句后返回标识列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 sql server 中使用存储过程返回插入某些值的表中的标识列值?例如,如果我们在表中插入数据,则使用存储过程:

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 if we insert data in a table:

表 TBL

  • UserID 整数,身份,自动递增
  • 名称 varchar
  • 用户名 varchar
  • 密码varchar

因此,如果我运行存储过程并插入一些值,例如:

So if I run the store procedure inserting some values like:

 Insert into TBL (Name, UserName, Password)
 Values ('example', 'example', '$2a$12$00WFrz3dOfLaIgpTYRJ9CeuB6VicjLGhLset8WtFrzvtpRekcP1lq')

如何返回将发生此插入的 UserID 的值.我需要其他一些操作的 UserID 值,有人能解决这个问题吗?

How can I return the value of UserID at which this insertion will take place. I need The value of UserID for some other operations, can anybody solve this?

推荐答案

Insert into TBL (Name, UserName, Password) Output Inserted.IdentityColumnName
 Values ('example', 'example', 'example')

这篇关于Sql Server 在插入语句后返回标识列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆