SQL Server 获得唯一编号的方法

2016-12-23 10:16:04来源:oschina作者:加速器人点击

USE [数据库名称]
go
CREATE TABLE [dbo].[t_SerialNo](
[id] [int] IDENTITY(1,1) NOT NULL,
[name] [nvarchar](20) NOT NULL,
[currentNo] [bigint] NOT NULL,
[description] [nvarchar](max) NULL,
CONSTRAINT [PK_t_SerialNo] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX= OFF, STATISTICS_NORECOMPUTE= OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS= ON, ALLOW_PAGE_LOCKS= ON) ON [PRIMARY]
) ON [PRIMARY]go
insert into [dbo].[t_SerialNo]([name],[currentNo],[description]) values('ComplexTagQuery',1,'复杂标签的查询临时保存编号')
go
-- 蒋振
-- 获得唯一编号
-- 编号存储在t_SerialNo(id,name,currentNo,description)表中
-- e.g.
-- use scada;
-- declare @no bigint
-- exec [dbo].[P_GetSerialNo] 'ComplexTagQuery',@no output
-- print convert(char(20),@no)
alter procedure [dbo].[P_GetSerialNo]
@name nvarchar(20),
@no bigint out
as
begin
begin tran
update t_SerialNo with(rowlock)
set @no = currentNo + 1, currentNo = currentNo + 1 where name = @name
commit tran
return @no
end
go

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台