GetSerialNoByOrders

本文介绍了一个 SQL Server 中用于生成基于当前日期的唯一订单序列号的存储过程。该过程根据传入的日期参数,结合当日订单数量动态生成固定长度的序列号。

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

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
--
 Author:        <Author,,Name>
--
 Create date: <Create Date, ,>
--
 Description:    <Description, ,>
--
 =============================================
ALTER FUNCTION [dbo].[GetSerialNoByOrders] 
(
@NowDate as datetime
)
RETURNS char(12)
AS
BEGIN
    
-- Declare the return variable here
    DECLARE @Result char(12)
    
DECLARE @Prefix as char(8)
    
DECLARE @Suffix as char(4)
    
DECLARE @MaxNum as int
    
     
    
set @Prefix = dbo.FormatDate2String(@NowDate)
    
    
-- Add the T-SQL statements to compute the return value here
    select @MaxNum = count(id) + 1
    
from dbo.Orders
    
where  datediff(d,CheckinTime,@NowDate= 0
    
and Status <> 0

    
if @MaxNum = 0
        
begin
            
set @Suffix = '0001'
        
end
    
else
        
begin
            
DECLARE @Length as int
            
DECLARE @PrefixAddition as varchar(4)
            
set @Length = 4 - len( (@MaxNum) )
            
set @PrefixAddition = ''
            
while(@Length > 0)
                
begin
                    
set @PrefixAddition = '0' + @PrefixAddition
                    
set @Length = @Length - 1
                
end
            
set @Suffix = @PrefixAddition + cast ( @MaxNum as varchar)

        
end
    
set @Result = @Prefix + @Suffix
    
-- Return the result of the function
    RETURN @Result

END

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值