/*建立测试数据*/ create table ordernum( num int) go insert ordernum values(0) go /*建立定单自动生成*/ create procedure p_create_order(@orderno int output) as begin declare @i_maxnum int set nocount on begin tran select @i_maxnum = isnull(num,0) from ordernum with(UPDLOCK) /*UPDLOCK记录在事物未提交前不能被更新*/ update ordernum set num = @i_maxnum + 1 commit tran set @orderno = @i_maxnum + 1 return 1 end--调用 declare @i_orderno int exec p_create_order @i_orderno output print(@i_orderno) drop table ordernum drop procedure p_create_order
create table ordernum( num int)
go
insert ordernum values(0)
go
/*建立定单自动生成*/
create procedure p_create_order(@orderno int output)
as
begin
declare @i_maxnum int
set nocount on
begin tran
select @i_maxnum = isnull(num,0) from ordernum with(UPDLOCK) /*UPDLOCK记录在事物未提交前不能被更新*/
update ordernum set num = @i_maxnum + 1
commit tran
set @orderno = @i_maxnum + 1
return 1
end--调用
declare @i_orderno int
exec p_create_order @i_orderno output
print(@i_orderno)
drop table ordernum
drop procedure p_create_order