SQL 2005 写了个类型oracle sequence 的存储过程

本文介绍了一种使用SQL存储过程来生成唯一序列号的方法。通过创建一个表存储序列号,并利用存储过程实现序列号的自动递增,确保了每天生成的序列号都是唯一的。此方案适用于需要批量生成唯一编号的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

 

先建张存序列号的表
CREATE TABLE [dbo].[Sequence](
 [SequenceNO] [bigint] NOT NULL,
 [date] [datetime] NOT NULL
) ON [PRIMARY]

创建存储过程

CREATE PROCEDURE GetSerialNo
 @no int output
AS
begin

if not exists(select 1 from sequence where datediff(dd,date,getdate())=0)
 begin
  set @no=1
  insert sequence values(@no,getdate())
 end
else

    begin transaction
  select @no=max(sequenceNo) +1 from sequence  with(xlock,holdLock) where datediff(dd,date,getdate())=0
  update sequence set sequenceNo=@no where datediff(dd,date,getdate())=0
  if @@error <> 0
   begin
    raiserror('Error,can not get SerialNo',16,1)
    rollback
    return
   end
  commit transaction
 end
return @no
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值