日期:2014-05-18  浏览次数:20585 次

sql server 2000 存储过程返回自增列的值
我有一表 sy
xh int (自增)
xm varchar(4)

建存储过程如下
CREATE PROCEDURE syxm
@xm varchar(4),
@xh int output
as
insert into sy (xm)values(@xm)
Select @xh=@@IDENTITY
go
目的;在新增数据后返回xh

用查询分析器执行
declare @xm varchar (4),@xh int set @xm='bbbb' exec syxm @xm, @xh
能新增数据不能返回带自增列的序号



------解决方案--------------------
或者:
declare @xm varchar (4),@xh int set @xm='bbbb' 
exec syxm @xm, @xh output
select @xh
------解决方案--------------------
最后面要查询的话 需要select @xh
------解决方案--------------------
SQL code
declare @xm varchar (4),@xh int set @xm='bbbb'  
exec syxm @xm, @xh output
select @xh

------解决方案--------------------
SQL code

if object_id('sy') is not null
   drop table sy
go
create table sy
(
 xh  int identity(1,1),
 xm varchar(4)
)
go
if object_id('up_tb') is not null
   drop procedure up_tb
go
create procedure up_tb (@xm varchar(4),@xh int output)
as
  insert into sy (xm) values (@xm)
  select @xh=@@IDENTITY
go

declare @xh int
exec up_tb '张三',@xh output
select @xh
/*
-----------
1

(1 行受影响)

*/