主键表:

CREATE TABLE [dbo].[KEYCODE](
 [KeyName] [varchar](12) NOT NULL,
 [KeyTableName] [varchar](40) NULL,
 [KeyFieldName] [varchar](30) NULL,
 [StrLen] [int] NULL,
 [CodeLen] [int] NULL,
 [CodeMin] [float] NULL,
 [CodeMax] [float] NULL,
 [CodeType] [varchar](1) NULL,
 [CurrentCode] [float] NULL,
 [FirstLoop] [varchar](1) NULL,
 [Prefix] [varchar](6) NULL,
 [Suffix] [varchar](6) NULL,
 [Remarks] [varchar](500) NULL,
 CONSTRAINT [PK_KEYCODE] PRIMARY KEY CLUSTERED
(
 [KeyName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

主键生成SQL:

Create Procedure [dbo].[SP_GetKeyCode]
@KeyName Varchar(20),
@KeyCode Varchar(30) Output,
@IsReturn Bit = 0
AS
Declare @zeroStr as Varchar(30),@MaxRunningCode as Varchar(30),
@KeyTableName as Varchar(40),@KeyFieldName as Varchar(40),
@CodeLen as Smallint,@CodeMin as Decimal(20,0),@CodeMax as Decimal(20,0),
@CodeType as Varchar(1),@CurrentCode as Decimal(20,0),@FirstLoop as varchar(1),
@Prefix as Varchar(6),@Suffix as Varchar(6),@preFixStr as Varchar(10),
@tmpstr as Varchar(30),@tempStr1 as Varchar(40),@sSQL as Varchar(400),
@CCode as Decimal(20,0), @RetStr as Varchar(20),@CurrentKeyCode as Varchar(30),
@I as smallint, @HeadStr as Varchar(20)
DECLARE @TableNameStartPos SmallInt set @zeroStr = '000000000000000000000000000000'
Set @MaxRunningCode= '999999999999999999999999999999'
set @KeyName = Upper(@KeyName)
--set @HeadStr = upper(@HeadStr)
if not exists(Select * from KeyCode where KeyName = @KeyName)
Begin
If @IsReturn = 1
Begin
Select @KeyName keyname, '' KeyCode
Set @KeyCode = ''
Return
End
RaisError('%s is an invalid KeyName!',16,1,@KeyName)
Select @KeyCode = ''
Return
end Select @KeyTableName = IsNull(KeyTableName,''),
@KeyFieldName = IsNull(KeyFieldName,''),
@CodeLen = IsNull(CodeLen,0),
@CodeMin = Cast(IsNull(CodeMin,0) as Decimal(20,0)),
@CodeMax = cast(IsNull(CodeMax,99) as Decimal(20,0)),
@CodeType = IsNull(CodeType,'1'),
@CurrentCode = Cast(IsNull(CurrentCode,0) as Decimal(20,0)),
@FirstLoop = IsNull(FirstLoop,'0'),
@Prefix = IsNull(Prefix,''),
@Suffix = IsNull(Suffix,'')
From KeyCode Where KeyName = @KeyName -- To exclude alias from KeyTableName (:DELIBASE:DELORD --> DELORD)
set @tableNameStartPos=Patindex('%BASE:%',@KeyTableName)
if @tableNameStartPos>0
set @KeyTableName=subString(@KeyTableName,@TableNameStartPos+5,len(@KeyTableName)-@TableNameStartPos-4) Set @HeadStr = @Prefix Set @preFixStr='' if @CodeType = '1'
Begin
if @FirstLoop = '1'
set @tmpstr = Cast((@CurrentCode + 1) as Varchar(20))
else
begin
set @tmpstr = Cast((@CurrentCode+1) as Varchar(20))
set @CCode = @CurrentCode + 1
Update KeyCode set CurrentCode =Cast(@CCode as Decimal(20,0)) where keyname = @KeyName
set @TempStr1= @HeadStr + SubString(@zerostr,1,@CodeLen - len(@tmpstr))+ @tmpstr
if @CCode > @CodeMax ---Find from beginning
begin
If @IsReturn = 1
Begin
Select 'Error' KeyName, 'All numbers are occupied!' KeyCode
Set @KeyCode = ''
Return
End
raisError('All numbers are occupied!',16,1)
Select @KeyCode = ''
return
end
end --- FirstLoop <>'1'
set @retstr = @TmpStr
end --- CodeType ='1'
if @CodeType = '2' --- YY + XXXXXX
Begin
set @prefixStr = SUbString(Cast(year(GetDate()) as Varchar(4)),3,2)
if @KeyTableName <> @prefixStr
begin
set @CurrentCode = @COdeMin -1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CurrentCode
where KeyName = @KeyName
end
set @retStr=Cast((@CurrentCode+1) as Varchar(10))
end
if @CodeType = '3' --YY + MM + XXXXXX
begin
Set @PrefixStr = Cast(Month(GetDate()) as Varchar(2))
if len(@PrefixStr)=1
Set @PrefixStr = '0' + @PrefixStr
set @prefixStr = SUbString(Cast(year(GetDate()) as Varchar(4)),3,2) + @PrefixStr
if @KeyTableName <> @prefixStr
begin
set @CurrentCode=@CodeMin - 1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CurrentCOde
where KeyName = @KeyName
end
set @retStr=Cast((@CurrentCode+1) as Varchar(10))
end
if @CodeType = '4' -- YY+MM+DD+xxxxx
Begin
Set @TmpStr = Cast(Month(GetDate()) as Varchar(2))
if len(@tmpStr)=1
Set @tmpStr = '0' + @tmpStr
Set @tempStr1 = Cast(day(GetDate()) as Varchar(2))
if len(@tempStr1)=1
Set @tempStr1 = '0' + @tempStr1
set @prefixStr = SUbString(Cast(year(GetDate()) as Varchar(4)),3,2) + @tmpStr+@tempStr1
if @KeyTableName <> @prefixStr
begin
set @CurrentCode = @CodeMin -1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CurrentCode
where KeyName = @KeyName
end
set @retStr=Cast((@CurrentCode+1) as Varchar(10))
end
if @CodeType = '5' -- YYYY+MM+xxxxx
begin
Set @PrefixStr = Cast(Month(GetDate()) as Varchar(2))
if len(@PrefixStr)=1
Set @PrefixStr = '0' + @PrefixStr
set @prefixStr = Cast(year(GetDate()) as Varchar(4)) + @PrefixStr
if @KeyTableName <> @prefixStr
begin
set @CurrentCode=@CodeMin - 1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CurrentCOde
where KeyName = @KeyName
end
set @retStr=Cast((@CurrentCode+1) as Varchar(10))
end If @CodeType = '7' -- DDxxxx Moorthy 19/10/00
Begin
Set @tmpStr = Cast(Day(GetDate()) As Varchar(2))
set @PrefixStr = @PRefixStr + replicate('0',2-len(@TmpStr)) + @TmpStr
if @KeyTableName <> @Prefixstr
begin
set @CCode = @CodeMin - 1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CodeMin - 1
where KeyName = @KeyName
end
set @retStr = Cast((@CurrentCode+1) as Varchar(10))
End if @CodeType = '8' -- xYMxxxx ---- running number reset monthly Begin
Set @tmpStr = Cast(Right(Year(GetDate()),1) As Varchar(2))
If Cast(Month(GetDate()) As Varchar(2)) = '10'
Set @TmpStr = @TmpStr + 'A'
Else If Cast(Month(GetDate()) As Varchar(2)) = '11'
Set @TmpStr = @TmpStr + 'B'
Else If Cast(Month(GetDate()) As Varchar(2)) = '12'
Set @TmpStr = @TmpStr + 'C'
Else
Set @TmpStr = @TmpStr + Cast(Month(GetDate()) As Varchar(2)) set @PrefixStr = @PRefixStr + replicate('0',2-len(@TmpStr)) + @TmpStr
if @KeyTableName <> @Prefixstr
begin
set @CCode = @CodeMin - 1
Update KeyCode set KeyTableName = @prefixStr,CurrentCode = @CodeMin - 1
where KeyName = @KeyName
end
set @retStr = Cast((@CCode+1) as Varchar(10))
End if cast(@retstr as Decimal(20,0)) = @CodeMax -- modify CurrentCode
Update KeyCode set CurrentCode = @CodeMin -1 where KeyName = @KeyName
else
Update KeyCode set CurrentCode =Cast(@retstr as Decimal(20,0)) where keyname = @KeyName
Set @retstr = @HeadStr+ @PrefixStr + SubString(@zeroStr,1,@CodeLen-len(@retstr))+ @retstr+ @Suffix
Select @KeyCode = @RetStr If @IsReturn = 1
Begin
Select @KeyName KeyName, @KeyCode KeyCode
Set @KeyCode = ''
Return
End

SQL Server主键自动生成_表and存储过程的更多相关文章

  1. Oracle主键自动生成_表and存储过程

    -- Create table create table T_EB_SYS_DN_SEQUENCE_CONFIG ( sequence_id VARCHAR2(36) default sys_guid ...

  2. MySql主键自动生成,表、实体、C#调用方法

    1.表:mysql建表语句 DROP TABLE IF EXISTS `keycode`; CREATE TABLE `keycode` ( `Id` ) NOT NULL AUTO_INCREMEN ...

  3. hibernate主键自动生成

    Entity类中,主键尽量使用可以为null值的类型,比如Integer,Long,String等,不要用int,long等.因为如果主键为null,则表示该实体类还没有保存到数据库,是一个临时状态( ...

  4. Sql Server 主键 外键约束

    主键约束 表通常具有包含唯一标识表中每一行的值的一列或一组列. 这样的一列或多列称为表的主键 (PK),用于强制表的实体完整性. 由于主键约束可保证数据的唯一性,因此经常对标识列定义这种约束. 如果为 ...

  5. SQL Server 主键及自增长列的修改

    一.对主键的修改 主键值都会带有主键约束,当执行update操作或是其他操作的时候就会受到限制无法修改,解决的方法是:取消主键约束->删掉主键列->插入修改后的主键值. (1)取消主键约束 ...

  6. Mybatis「MySQL-Oracle」 中主键自动生成 <selectKey> 序列化

    有时候我们不仅仅是通过返回 int 影响行数来确定数据是否插入成功就行了,因为我们总是会用到这个刚刚插入的自增主键,比如主子表入库,子表需要主表的 id,那这个时候我们再去数据库查就显得有点 low ...

  7. Sql Server 主键由字母数字组成并按照数字自动增长

    在SQL SERVER 中如果我们想要使主键按照一定规则自动增长我们可以这样做: 这里我们新建一张研究表,里面有研究ID,研究人员姓名和研究医院. 我们使SicentificId 设为主键 并且从1开 ...

  8. 数据库开发基础-SQl Server 主键、外键、子查询(嵌套查询)

    主键 数据库主键是指表中一个列或列的组合,其值能唯一地标识表中的每一行.这样的一列或多列称为表的主键,通过它可强制表的实体完整性.当创建或更改表时可通过定义 PRIMARY KEY约束来创建主键.一个 ...

  9. Entity Framework Code First 遭遇主键自动生成问题

    4.0后就没有去跟踪后面的版本了.现在直接开始用5.0没想到在做User的GURD时就遭遇insert不进数据问题. ISet<User>.Add(user);_context.SaveC ...

随机推荐

  1. java 对象序列化 RMI

    对于一个存在于Java虚拟机中的对象来说,其内部的状态只保持在内存中.JVM停止之后,这些状态就丢失了.在很多情况下,对象的内部状态是需要被持久化下来的.提到持久化,最直接的做法是保存到文件系统或是数 ...

  2. codeforces C. Sereja and Swaps

    http://codeforces.com/contest/426/problem/C 题意:找出连续序列的和的最大值,可以允许交换k次任意位置的两个数. 思路:枚举区间,依次把区间内的比较小的数换成 ...

  3. Java实现二叉树的构建与遍历

    转载:http://ocaicai.iteye.com/blog/1047397 目录: 1.把一个数组的值赋值给一颗二叉树 2.具体代码 1.树的构建方法 2.具体代码 package tree; ...

  4. Java中的移位操作符

    记住所有的移动位数,针对的都是补码来讲的,所以要先将十进制整数转换成补码后,然后再来进行移位操作 移位操作 还要注意类型的约束条件,例如int,移动范围是0-31位,所以看补码只能看最后五位,这才是有 ...

  5. paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置..

    paip.提升用户体验-----c++ gcc 命令在notepad++扩展中的配置.. 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址: ...

  6. 【HDOJ】2645 find the nearest station

    裸BFS. /* 2645 */ #include <iostream> #include <queue> #include <cstdio> #include & ...

  7. super返回不过来

    class Fruit {     String color = "未确定颜色";     //定义一个方法,该方法返回调用该方法的实例     public Fruit getT ...

  8. sql server 2008有关SQL的模糊查询

    执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...

  9. Delhpi TdxComponentPrinter怎样联上dxdbgrid中的数据打印

    有谁知道TdxComponentPrinter怎样联上dxdbgrid中的数据打印?         有谁知道TdxComponentPrinter怎样联上dxdbgrid中的数据打印?   回复人: ...

  10. [转载]软件测试之Web测试经典总结

    转载自:软件测试之Web测试经典总结 基于Web的系统测试在基于Web的系统开发中,如果缺乏严格的过程,我们在开发.发布.实施和维护Web的过程中,可能就会碰到一些严重的问题,失败的可能性很大.而且, ...