借助数据库生成订单号

10 篇文章 0 订阅
2 篇文章 0 订阅

借助数据库生成订单号

平时在项目开发中,遇到自己生成订单号的情况很常见,下面就介绍一种借助数据库生成订单号的方法。

在数据库中创建IDPool表

USE [数据库名称]
GO

/****** Object:  Table [dbo].[IDPool]    Script Date: 2018/3/13 16:48:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[IDPool](
    [OID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [LeaseholderID] [int] NOT NULL,
    [DbName] [varchar](64) NOT NULL,
    [TableName] [varchar](64) NOT NULL,
    [MaxValue] [int] NULL,
    [Prefix] [varchar](64) NULL,
    [IDType] [smallint] NOT NULL,
    [InDate] [datetime] NOT NULL,
 CONSTRAINT [PK_IDPool] PRIMARY KEY CLUSTERED 
(
    [OID] 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

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[IDPool] ADD  CONSTRAINT [DF_IDPool_LeaseholderID]  DEFAULT ((1)) FOR [LeaseholderID]
GO

ALTER TABLE [dbo].[IDPool] ADD  CONSTRAINT [DF_IDPool_IDType]  DEFAULT ((0)) FOR [IDType]
GO

ALTER TABLE [dbo].[IDPool] ADD  CONSTRAINT [DF_IDPool_InDate]  DEFAULT (getdate()) FOR [InDate]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'IDPool', @level2type=N'COLUMN',@level2name=N'OID'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'库名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'IDPool', @level2type=N'COLUMN',@level2name=N'DbName'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'表名' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'IDPool', @level2type=N'COLUMN',@level2name=N'TableName'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'当前值' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'IDPool', @level2type=N'COLUMN',@level2name=N'MaxValue'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自递增ID的池子' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'IDPool'
GO

新建存储过程

USE [数据库名称]
GO

/****** Object:  StoredProcedure [dbo].[UP_GetID]    Script Date: 2018/3/13 16:53:15 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE  PROCEDURE [dbo].[UP_GetID] --1,'数据库名称','订单表名称','F',4,1,4,1
@LeaseholderID INT,
@DbName varchar(64),--数据库名
@TableName varchar(64),--表名
@Prefix varchar(10)='',--前缀
@Type smallint=0,      --类型 -0:前缀+ID, 1:前缀+年(两位)月日+ID,2:前缀+年(两位)月+ID,3:前缀+年(两位)+ID
@BeginNum int=1,          --开始
@NumLen int=0,          --补零 0\1不补0,
@ID varchar(100) output
AS
BEGIN
SET NOCOUNT ON;
DECLARE @MaxValue int,@i int
SET @ID=''


DECLARE @MaxTab table(MaxValue int);

Merge dbo.IDPool as a
Using(
select
    TableName=@TableName ,
    DbName=@DbName,
    Prefix=@Prefix,
    Indate_Start = CASE @type
                  WHEN 0 THEN 0
                  WHEN 1 THEN DATEDIFF(DAY, 0, GETDATE())
                  WHEN 2 THEN DATEDIFF(DAY, DAY(GETDATE()) - 1, GETDATE())
                  WHEN 3 THEN DATEADD(YEAR, YEAR(GETDATE()) - 1900, 0)
  WHEN 4 THEN CONVERT(VARCHAR(16),  DATEADD(MINUTE, 0, GETDATE()), 120 )
              END,
    Indate_Stop = CASE @type
                  WHEN 0 THEN CONVERT(datetime, '9999-12-31')
                  WHEN 1 THEN DATEDIFF(DAY, -1, GETDATE())
                  WHEN 2 THEN DATEADD(Month, 1, DATEDIFF(DAY, DAY(GETDATE()) - 1, GETDATE()))
                  WHEN 3 THEN DATEADD(YEAR, YEAR(GETDATE()) - 1899, 0)
  WHEN 4 THEN CONVERT(VARCHAR(16),  DATEADD(MINUTE, 1, GETDATE()), 120 )

              END
) as b 
on a.TableName = b.TableName and a.DbName =b.DbName and a.Prefix=b.Prefix
and a.indate >= b.Indate_Start and a.indate < b.Indate_Stop 
WHEN MATCHED THEN
UPdate 
SET MaxValue=MaxValue+1
WHEN NOT MATCHED THEN
INSERT ([DbName],[TableName],[Prefix],idtype,[InDate],[MaxValue])

VALUES(@DbName,@TableName,@Prefix,@type,CONVERT(VARCHAR(16), getdate(), 120 ),@BeginNum) 
OUTPUT inserted.MaxValue into @MaxTab
; 

select @MaxValue=MaxValue from @MaxTab
;

--PRINT @MaxValue
IF @NumLen>0 AND LEN(CAST(@MaxValue AS varchar))<=@NumLen
BEGIN
    SET @i=0
    WHILE @i<@NumLen
    BEGIN
        SET @ID=@ID+'0'
        set @i=@i+1
    END
    print(@id)
    SET @ID=RIGHT(@ID+lTRIM(STR(@MaxValue)),@NumLen)
END
ELSE
    SET @ID=lTRIM(STR(@MaxValue))
--print(@id)
SET @ID=@Prefix+ CASE 
WHEN @type=1
THEN SUBSTRING(CONVERT(VARCHAR(100),GETDATE(),112),3,6)
WHEN @type=2
THEN SUBSTRING(CONVERT(VARCHAR(100),GETDATE(),112),3,4)
WHEN @type=3
THEN SUBSTRING(CONVERT(VARCHAR(100),GETDATE(),112),3,2)
WHEN @type=4
THEN  SUBSTRING(replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':',''),3,12) 
ELSE
''
                END
            +@ID
select @ID
RETURN @MaxValue
END
GO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值