关于计数以及Index返回订单号升级版(控制字符长度,控制年月标记)

数据库表操作:

EXEC sys.sp_dropextendedproperty @name=N'Name' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'Updated'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'CreateDate'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'KeyWord'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'ThisSigle'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'ThisIndex'

GO

EXEC sys.sp_dropextendedproperty @name=N'MS_Description' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'NoType'

GO

ALTER TABLE [dbo].[SetNoIndex] DROP CONSTRAINT [DF_SetNoIndex_Updated]
GO

ALTER TABLE [dbo].[SetNoIndex] DROP CONSTRAINT [DF_SetNoIndex_CreateDate]
GO

/****** Object:  Table [dbo].[SetNoIndex]    Script Date: 2023/8/2 20:36:51 ******/
DROP TABLE [dbo].[SetNoIndex]
GO

/****** Object:  Table [dbo].[SetNoIndex]    Script Date: 2023/8/2 20:36:51 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[SetNoIndex](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[NoType] [int] NULL,
	[ThisIndex] [int] NULL,
	[ThisSigle] [nvarchar](30) NULL,
	[KeyWord] [nvarchar](30) NULL,
	[CreateDate] [datetime] NULL,
	[Updated] [datetime] NULL
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[SetNoIndex] ADD  CONSTRAINT [DF_SetNoIndex_CreateDate]  DEFAULT (getdate()) FOR [CreateDate]
GO

ALTER TABLE [dbo].[SetNoIndex] ADD  CONSTRAINT [DF_SetNoIndex_Updated]  DEFAULT (getdate()) FOR [Updated]
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0月累计,1日累计,2年累计,3,总共累计' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'NoType'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'当前排序' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'ThisIndex'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'当前标记(如果是日累计就是20230802,如果是月累计就是 202308 ,如果是年累计就是 2023,如果是 所有累计就是0)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'ThisSigle'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'关键字' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'KeyWord'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'CreateDate'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'更新时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex', @level2type=N'COLUMN',@level2name=N'Updated'
GO

EXEC sys.sp_addextendedproperty @name=N'Name', @value=N'编号自动生成器' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex'
GO

数据库 脚本操作包括测试:

declare  @SetNoType int ,@SetThisSigle nvarchar(10), @SetKeyword nvarchar(10),@ThisDate nvarchar(20),@OutStr nvarchar(20),@OutStrLength int  ;
set @ThisDate =CONVERT(nvarchar, getdate(), 112);---当前日期全部数据(20230108set @SetNoType = 3;--设定类型
set  @OutStrLength =4;--序号保留多少位
set @SetKeyword = '测试';---当前关键字
if (@SetNoType = 0)--0月累计
begin
  set @SetThisSigle = SUBSTRING(@ThisDate,1,6); 
end
else if(@SetNoType =1 )--1日累计
begin
 set @SetThisSigle = @ThisDate; 
end
else if(@SetNoType =2)--2年累计
begin 
 set @SetThisSigle = SUBSTRING(@ThisDate,1,4); 
end 
else --3总共累计
begin
 set @SetThisSigle ='';
end 
 declare @SetIndex int ;
 if  exists(select * from  SetNoIndex where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle )
  begin -- 如果存在就更新 
     update SetNoIndex set ThisIndex = ThisIndex+1 ,Updated =getdate() where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle;--先更新
     select  @SetIndex= ThisIndex from  SetNoIndex where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle ;--在提取结果
  end 
  else
  begin
      set  @SetIndex = 1;
      insert  into SetNoIndex ([NoType],[ThisIndex],[ThisSigle],[KeyWord])  values (@SetNoType,@SetIndex,@SetThisSigle,@SetKeyword);-- 插入数据
  end 
  --select DATALENGTH( @ThisDate);
  --select  @ThisDate;
  --select @SetThisSigle;
  select @SetThisSigle + right('00000000000'+convert(varchar, @SetIndex),@OutStrLength) from SetNoIndex where  KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle; 

程序操作方法:

     
        /// 
        ///获取当前返回的编号0月累计,1日累计,2年累计,3,总共累计
        /// 
        ///  0月累计,1日累计,2年累计,3,总共累计
        /// 当前关键字(需要的表单名子)
        /// index编号长度 0001 就是4 
        /// 
        public string GetTHisNoIndex( string SetKeyword,int SetNoType=0, int OutIndexLength=5)
            {
                string sql = $@"declare  @SetNoType int ,@SetThisSigle nvarchar(10), @SetKeyword nvarchar(10),@ThisDate nvarchar(20),@OutStr nvarchar(20),@OutStrLength int  ;
set @ThisDate =CONVERT(nvarchar, getdate(), 112);---当前日期全部数据(20230108)
set @SetNoType = {SetNoType};--设定类型
set  @OutStrLength ={OutIndexLength};--序号保留多少位
set @SetKeyword = '{SetKeyword}';---当前关键字
if (@SetNoType = 0)--0月累计
begin
  set @SetThisSigle = SUBSTRING(@ThisDate,1,6); 
end
else if(@SetNoType =1 )--1日累计
begin
 set @SetThisSigle = @ThisDate; 
end
else if(@SetNoType =2)--2年累计
begin 
 set @SetThisSigle = SUBSTRING(@ThisDate,1,4); 
end 
else --3总共累计
begin
 set @SetThisSigle ='0';
end 
 declare @SetIndex int ;
 if  exists(select * from  SetNoIndex where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle )
  begin -- 如果存在就更新 
     update SetNoIndex set ThisIndex = ThisIndex+1 ,Updated =getdate() where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle;--先更新
     select  @SetIndex= ThisIndex from  SetNoIndex where KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle ;--在提取结果
  end 
  else
  begin
      set  @SetIndex = 1;
      insert  into SetNoIndex ([NoType],[ThisIndex],[ThisSigle],[KeyWord])  values (@SetNoType,@SetIndex,@SetThisSigle,@SetKeyword);-- 插入数据
  end 
  --select DATALENGTH( @ThisDate);
  --select  @ThisDate;
  --select @SetThisSigle;
  select @SetThisSigle + right('00000000000'+convert(varchar, @SetIndex),@OutStrLength) from SetNoIndex where  KeyWord = @SetKeyword  and  ThisSigle = @SetThisSigle;  ";
                string OutStr = DBUtility.DapperDbHelper.ExecuteScalar<string>(sql);
                return OutStr;
              } 

另一种应用场景

/// 
/// 获取当前编号
/// 
/// 当前类型
/// 店铺No
/// 电话号码后四位
/// 
   public string GetNoByType(ENoTypes ThisType, string DPNO = "",string PhoneNo="0000")
        {
            string GetFromDB = "";//数据库获取的数据
            string OutNos = "";
          
            switch (ThisType)
            {
                case ENoTypes.客户编号:
                    GetFromDB = GetIndexNo($"客户编号{DPNO}", 2, 3);
                    OutNos = $"YYU{DPNO}KH{GetFromDB}{PhoneNo}"; //YYU+001(店铺编号)+KH+2023(年)+999(流水号)+0000(电话号码后4位)
                    break;
                case ENoTypes.店铺编号:
                    GetFromDB = GetIndexNo("店铺编号", 3, 3);
                    OutNos = GetFromDB; //001(店铺编号)
                    break;
                case ENoTypes.报告编号:
                    GetFromDB = GetIndexNo($"报告编号{DPNO}", 1, 2);
                    OutNos = $"YYU{DPNO}{GetFromDB}"; //YYU+001(店铺编号)+2023(年)+08(月)+02(日)+99(流水号)
                    break;
                case ENoTypes.样本编号:
                    GetFromDB = GetIndexNo($"样本编号{DPNO}", 1, 2);
                    OutNos = $"YYU{DPNO}YB{GetFromDB}"; //YYU+001(店铺编号)+YB+2023(年)+08(月)+02(日)+99(2位流水号)
                    break;
                case ENoTypes.样本类型编号:
                    GetFromDB = GetIndexNo("样本类型编号", 2, 3);
                    OutNos = $"YYUYT{GetFromDB}"; //YYU+001(店铺编号)+YB+2023(年)+08(月)+02(日)+99(2位流水号)
                    break;
                case ENoTypes.项目编号:
                    GetFromDB = GetIndexNo("项目编号", 3, 3);
                    OutNos = $"YYUXM{GetFromDB}"; // 
                    break;
                default://用户编号
                    GetFromDB = GetIndexNo("用户编号", 3, 5);
                    OutNos = $"YYU{GetFromDB}"; //YYU+99999(流水号)
                    break;
            }
            return OutNos; 
        } 

你可能感兴趣的:(数据库,服务器)