存储过程生成复杂的随机编号

Posted chengjunde

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了存储过程生成复杂的随机编号相关的知识,希望对你有一定的参考价值。

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 [email protected] 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‘[email protected]
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 ‘%‘[email protected]+‘MS-SL2-IPQC-ZC-‘[email protected]+‘%‘

--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 ‘%‘[email protected]+‘MS-SL2-FQC-ZC-‘[email protected]+‘%‘

--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 ‘%‘[email protected]+‘MS-SL2-OQC-ZC-‘[email protected]+‘%‘


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-‘[email protected]+‘-‘+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-‘[email protected]+‘-‘+‘%‘)
then ‘IPQC‘+‘-ZC-‘[email protected]+‘-‘+‘001‘
else ‘IPQC‘+‘-ZC-‘[email protected]+‘-‘+replace(str(right(@InitValue_F,3), 3), space(1), ‘0‘)
end
else
‘IPQC‘+‘-ZC-‘[email protected]+‘-‘+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-‘[email protected]+‘-‘+‘%‘)
then ‘FQC‘+‘-ZC-‘[email protected]+‘-‘+‘001‘
else ‘FQC‘+‘-ZC-‘[email protected]+‘-‘+replace(str(right(@InitValue_F,3), 3), space(1), ‘0‘)
end
else
‘FQC‘+‘-ZC-‘[email protected]+‘-‘+replace(str(right(@InitValue_F,3), 3), space(1), ‘0‘)
end


--when checktype=6 then ‘OQC‘+‘-ZC-‘[email protected]+‘-‘+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-‘[email protected]+‘-‘+‘%‘)
then ‘OQC‘+‘-ZC-‘[email protected]+‘-‘+‘001‘
else ‘OQC‘+‘-ZC-‘[email protected]+‘-‘+replace(str(right(@InitValue_F,3), 3), space(1), ‘0‘)
end
else
‘OQC‘+‘-ZC-‘[email protected]+‘-‘+replace(str(right(@InitValue_O,3), 3), space(1), ‘0‘)
end
end
,
[email protected],
[email protected],
checktime=getdate()
where [email protected]
end
else
begin
update mes_Inspect set [email protected],[email protected],checktime=getdate() where [email protected] and checkman is null
--update mes_inspectsingle set Itemvalue=case when itemvalue=‘NG‘ then ‘OK‘ else Itemvalue end where [email protected]
--update MES_InspectGauge set CheckResult=case when CheckResult=‘NG‘ then ‘OK‘ else CheckResult end where [email protected]
end

end

以上是关于存储过程生成复杂的随机编号的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server 存储过程生成流水号

[终章]进阶20-流程控制结构--if/case/while结构 - 三个while的存储过程案例(批量生成表单数据) - 随机长度的随机字符串的存储过程案例

存储过程返回集合复杂类型对象生成器工具

oracle 随机生成12位不重复数据,求高手写个存储过程。高分!!!

存储过程中返回的地理列未显示在实体框架自动生成的复杂类型中

订单流水号(唯一编号)的生成