数据库表操作:
EXEC sys.sp_dropextendedproperty @name=N'Name' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'SetNoIndex'GOEXEC 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'GOEXEC 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'GOEXEC 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'GOEXEC 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'GOEXEC 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'GOEXEC 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'GOALTER TABLE [dbo].[SetNoIndex] DROP CONSTRAINT [DF_SetNoIndex_Updated]
GOALTER 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
GOSET QUOTED_IDENTIFIER ON
GOCREATE 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]GOALTER TABLE [dbo].[SetNoIndex] ADD CONSTRAINT [DF_SetNoIndex_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GOALTER TABLE [dbo].[SetNoIndex] ADD CONSTRAINT [DF_SetNoIndex_Updated] DEFAULT (getdate()) FOR [Updated]
GOEXEC 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'
GOEXEC 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'
GOEXEC 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'
GOEXEC 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'
GOEXEC 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'
GOEXEC 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'
GOEXEC 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);---当前日期全部数据(20230108)
set @SetNoType = 3;--设定类型
set @OutStrLength =4;--序号保留多少位
set @SetKeyword = '测试';---当前关键字
if (@SetNoType = 0)--0月累计
beginset @SetThisSigle = SUBSTRING(@ThisDate,1,6);
end
else if(@SetNoType =1 )--1日累计
beginset @SetThisSigle = @ThisDate;
end
else if(@SetNoType =2)--2年累计
begin set @SetThisSigle = SUBSTRING(@ThisDate,1,4);
end
else --3总共累计
beginset @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 elsebeginset @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;
程序操作方法:
/// <summary>///获取当前/// </summary>/// <param name="SetNoType"> 0月累计,1日累计,2年累计,3,总共累计</param>/// <param name="SetKeyword">当前关键字(需要的表单名子)</param>/// <param name="OutIndexLength">index编号长度 0001 就是4 </param>/// <returns></returns>public string InsertOrUpdate( 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月累计
beginset @SetThisSigle = SUBSTRING(@ThisDate,1,6);
end
else if(@SetNoType =1 )--1日累计
beginset @SetThisSigle = @ThisDate;
end
else if(@SetNoType =2)--2年累计
begin set @SetThisSigle = SUBSTRING(@ThisDate,1,4);
end
else --3总共累计
beginset @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 elsebeginset @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;}