zoukankan      html  css  js  c++  java
  • 利用SQL存储过程生成程序编号的一种方法

    在程序设计中,尤其是针对企业应用的开发,不可以避免的会要生成大量的编号,比如订单编号、入库编号等。现在利用SQL的存储过程可以方便的实现自动编号,可以大大的提高程序的复用和减少代码的编写。

            主要是利用SQL中的CONVERT函数来对日期进行格式化。
            比如要做这样的一个编号结构:
            标识(2位)           日期时间(14位)            流水号(4位)
            BH                             20070227160954               1001

            程序代码如下:   
    DECLARE @myval nvarchar(20),
     
    @maxval nvarchar(4)

    select @maxval=max(right(InEquipNum,4))+1 from InEquip  --InEquipNum 为编号字段,取最后4位并加1

    set @myval=CONVERT(varchar(12), getdate(),112)+           --取日期组合
    (substring(convert(varchar(20),getdate(),120),12,2)) +        --取小时
    (substring(convert(varchar(20),getdate(),120),15,2)) +        --取分钟
    (substring(convert(varchar(12),getdate(),108),7,2)) +          --取秒
    (select case  
        
    when @maxval is null then '1000'                                           --如果编号为空,则先给出一个值
        else @maxval
        
    end
    )

    select @myval 编号
    运行后结果如:BH200702271609541001

           附CONVERT函数使用说明:
           使用 CONVERT:
    CONVERT (data_type[(length)], expression [, style])

    select CONVERT(varchar, getdate(), 120 )
    2004-09-12 11:06:08
    select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
    20040912110608
    select CONVERT(varchar(12) , getdate(), 111 )
    2004/09/12
    select CONVERT(varchar(12) , getdate(), 112 )
    20040912
    select CONVERT(varchar(12) , getdate(), 102 )
    2004.09.12
    select CONVERT(varchar(12) , getdate(), 101 )
    09/12/2004
    select CONVERT(varchar(12) , getdate(), 108 )
    11:06:08
  • 相关阅读:
    [51nod1299]监狱逃离
    [51nod1206]Picture
    noi 2016 游记
    [Codeforces 696D] Legen...
    [bzoj2574] [Poi1999]Store-Keeper
    [bzoj1227] [SDOI2009]虔诚的墓主人
    [bzoj3979] [WF2012]infiltration
    Docker
    SpringBoot实现登录
    SpringBoot第一次案例
  • 原文地址:https://www.cnblogs.com/wucf2004/p/768808.html
Copyright © 2011-2022 走看看