获取SQL Server中的身份列 [英] get identity column in sql server
本文介绍了获取SQL Server中的身份列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我创建带有标识列的表,此列为id,
并与其他列名称一样,请记住
我插入后如何在过程
中获取此标识列的值
创建 过程 p_dd( @P_name varchar ( 40 ), @ P_address varchar ( 40 ), @ P_id int 输出)
为
开始
插入 插入 mytbel(名称,地址)
值( @ P_name , @ P_address )
结束
所以没有办法获得此列的价值吗?
感谢您的帮助.
SCID_IDENTITY [ ^ ]
有几种获取新插入的id值的方法.如Mark所写的那样使用范围标识.您的过程如下所示:
创建 过程 p_dd( @P_name varchar ( 40 ), @ P_address varchar ( 40 ), @ P_id int 输出) 为 开始 插入 插入 mytbel(名称,地址) 值( @ P_name , @ P_address ) ; set @ P_id = scope_identity(); 结束
另一种方法是使用OUTPUT子句从insert语句中获取实际值.例如:
创建 过程 p_dd( @P_name varchar ( 40 ), @ P_address varchar ( 40 ), @ P_id int 输出) 为 开始 插入 插入 mytbel(名称,地址) 插入输出.id插入 @ P_id 值( @ P_name , @ P_address ) ; 结束
hi everyone
i create table with identity column this column is id ,
and with other column name,adreess so
how i can after inserted get value of this identity column in procedure
create procedure p_dd(@P_name varchar(40),@P_address varchar(40),@P_id int output )
as
begin
insert into mytbel (name,address)
values (@P_name,@P_address)
end
so there is no way to get value of this column ?
thanks for any hlep
解决方案
SCOPE_IDENTITY[^]
There are several ways of getting the value of newly inserted id. On is as Mark wrote use the scope identity. Your procedure would look something like:
create procedure p_dd(@P_name varchar(40),@P_address varchar(40),@P_id int output ) as begin insert into mytbel (name,address) values (@P_name,@P_address); set @P_id = scope_identity(); end
Another way is to get the actual value from the insert statement using OUTPUT clause. for example:
create procedure p_dd(@P_name varchar(40),@P_address varchar(40),@P_id int output ) as begin insert into mytbel (name,address) output inserted.id into @P_id values (@P_name,@P_address); end
这篇关于获取SQL Server中的身份列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文