zoukankan      html  css  js  c++  java
  • 存储过程生成复杂的随机编号

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author: luoxf
    -- Create date: 20141023
    -- Description: 审核检测
    --exec [dbo].[st_MES_UpdateInspectResult]
    -- =============================================
    ALTER PROCEDURE [dbo].[st_MES_UpdateInspectResult] (
    @BillNo nvarchar(50)='Bill2015101505232300',
    @CheckMan nvarchar(20)='superadmin',
    @Result nvarchar(10)='NG'
    )
    AS
    BEGIN

    --select abnormalno,* from mes_Inspect where AbnormalNO is not null and checkman is not null order by CheckTime desc

    if exists(select * from mes_Inspect where billno=@billno and checkman is not null)
    return

    if (@Result='NG')
    begin

    declare @InitValue_IP varchar(50),@Month varchar(10),@InitValue_F varchar(50),@InitValue_O varchar(50),@year varchar(20)
    set @Month=month(getdate())
    if (len(@Month)=1)
    set @Month ='0'+@Month
    set @year=CONVERT(varchar(4),GETDATE(),112)



    --select convert(int,SUBSTRING('2015MS-SL2-IPQC-ZC-10-0001',23,4))
    --select convert(int,SUBSTRING('2015MS-SL2-FQC-ZC-10-0010',22,4))
    --select convert(int,SUBSTRING('2015MS-SL2-OQC-ZC-10-1000',22,4))
    --select @InitValue_IP = isnull(max(substring(abnormalno,len(abnormalno)-2,len(abnormalno))),0)from MES_Inspect

    select @InitValue_IP = isnull(max(substring(abnormalno,23,3)),0)from MES_Inspect
    where abnormalno<> '' and substring(abnormalno,1,4)=convert(varchar(4),getdate(),112)
    and abnormalno like '%'+@year+'MS-SL2-IPQC-ZC-'+@Month+'%'

    --select @InitValue_F = isnull(max(substring(abnormalno,len(abnormalno)-2,len(abnormalno))),0)from MES_Inspect
    select @InitValue_F = isnull(max(substring(abnormalno,22,3)),0)from MES_Inspect
    where abnormalno<> '' and substring(abnormalno,1,4)=convert(varchar(4),getdate(),112)
    and abnormalno like '%'+@year+'MS-SL2-FQC-ZC-'+@Month+'%'

    --select @InitValue_O = isnull(max(substring(abnormalno,len(abnormalno)-2,len(abnormalno))),0)from MES_Inspect
    select @InitValue_O = isnull(max(substring(abnormalno,22,3)),0)from MES_Inspect
    where abnormalno<> '' and substring(abnormalno,1,4)=convert(varchar(4),getdate(),112)
    and abnormalno like '%'+@year+'MS-SL2-OQC-ZC-'+@Month+'%'


    select @InitValue_IP

    select @InitValue_IP = convert(int,@InitValue_IP)+1
    select @InitValue_F = convert(int,@InitValue_F)+1
    select @InitValue_O = convert(int,@InitValue_O)+1


    update mes_Inspect
    set abnormalno=convert(varchar(4),getdate(),112)+'MS-SL2-'+
    case
    --when checktype in(1,2,3,4) then 'IPQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_IP,4),4), space(1), '0')
    when checktype in(1,2,3,4) then
    case when CONVERT(nvarchar,DateName(DAY,GETDATE()))='1'
    then
    case
    when not exists(select * from mes_Inspect where abnormalno like '%'+convert(varchar(4),getdate(),112)+'MS-SL2-'+'IPQC'+'-ZC-'+@Month+'-'+'%')
    then 'IPQC'+'-ZC-'+@Month+'-'+'001'
    else 'IPQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_F,3), 3), space(1), '0')
    end
    else
    'IPQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_IP,3), 3), space(1), '0')
    end


    when checktype=5 then
    case when CONVERT(nvarchar,DateName(DAY,GETDATE()))='1'
    then
    case
    when not exists(select * from mes_Inspect where abnormalno like '%'+convert(varchar(4),getdate(),112)+'MS-SL2-'+'FQC'+'-ZC-'+@Month+'-'+'%')
    then 'FQC'+'-ZC-'+@Month+'-'+'001'
    else 'FQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_F,3), 3), space(1), '0')
    end
    else
    'FQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_F,3), 3), space(1), '0')
    end


    --when checktype=6 then 'OQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_O,3), 3), space(1), '0')
    when checktype=6 then
    case when CONVERT(nvarchar,DateName(DAY,GETDATE()))='1'
    then
    case
    when not exists(select * from mes_Inspect where abnormalno like '%'+convert(varchar(4),getdate(),112)+'MS-SL2-'+'OQC'+'-ZC-'+@Month+'-'+'%')
    then 'OQC'+'-ZC-'+@Month+'-'+'001'
    else 'OQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_F,3), 3), space(1), '0')
    end
    else
    'OQC'+'-ZC-'+@Month+'-'+replace(str(right(@InitValue_O,3), 3), space(1), '0')
    end
    end
    ,
    checkResult=@result,
    checkman=@checkMan,
    checktime=getdate()
    where billno=@billno
    end
    else
    begin
    update mes_Inspect set checkResult=@result,checkman=@checkMan,checktime=getdate() where billno=@billno and checkman is null
    --update mes_inspectsingle set Itemvalue=case when itemvalue='NG' then 'OK' else Itemvalue end where billno=@BillNo
    --update MES_InspectGauge set CheckResult=case when CheckResult='NG' then 'OK' else CheckResult end where billno=@BillNo
    end

    end

  • 相关阅读:
    织梦内容模型自定义字段设置一个随机数
    网页禁止右键查看源码屏蔽键盘事件
    面试官:如何防止 Java 源码被反编译?我竟然答不上来。。
    Elastic Job 同城主备、同城双活,高可用必备~
    再见,Spring Security OAuth!!
    怎么让 Linux 进程在后台运行?
    30 个 ElasticSearch 调优知识点,都给你整理好了!
    Spring Boot 2.5.4 发布,2.2.x 正式结束使命!
    移动端与服务器端之间的 token 怎么设计?
    最新数据库排行出炉,SQL Server 暴跌。。
  • 原文地址:https://www.cnblogs.com/chengjun/p/5244352.html
Copyright © 2011-2022 走看看