USE [ChangHong_612]
GO
/****** Object: StoredProcedure [dbo].[st_MES_GetCodeRule] Script Date: 09/10/2015 17:44:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[st_MES_GetCodeRule] (
@in_CodeRuleName varchar(50) = '' --规则名称
, @in_BillNO varchar(50) = '' --传入的单据编码
, @in_User varchar(20) = ''
, @OutCode varchar(50) = '' output
, @ShowRs bit = 1
)
/*************
exec [st_MES_GetCodeRule] 'MouldNO',''
select * from Sys_CodeRuleChild
creator: Wuchun
create date: 2012.11.06
Remark: 按编码规则产生相应编码
**************/
as
begin
declare @code varchar(200), @DeptCode varchar(20), @SNO varchar(20)
declare @RuleID int, @CodeRuleNo varchar(50), @InitValue varchar(50), @length int , @Level int
select @RuleID = RuleID from Sys_CodeRule where CodeRuleEName = @in_CodeRuleName and state = 1
select @Code = '', @DeptCode = '', @SNO = ''
declare cur cursor for
select CodeRuleNo, InitValue, length, level
from Sys_CodeRuleChild
where RuleID = @RuleID
order by level
open cur
fetch next from cur into @CodeRuleNo, @InitValue, @length, @Level
while(@@fetch_Status = 0)
begin
if(@CodeRuleNO = 'Date')
select @Code = @Code+convert(varchar(8),getdate(),112)
else if(@CodeRuleNO = 'BillNO')
select @Code = @Code+ rtrim(@in_BillNO)
else if(@CodeRuleNO = 'SN') --流水号
begin
exec st_MES_GetCodeInitValue @SNO output, @RuleID, @Level, @CodeRuleNO, @Length, @InitValue
select @Code = @Code+ rtrim(@SNO)
end
else if(@CodeRuleNO = 'Define')
select @Code = @Code + rtrim(@InitValue)
else if(@CodeRuleNO = 'EmpID')
select @Code = @Code + rtrim(@in_user)
else if(@CodeRuleNO = 'DeptCode')
begin
select @DeptCode = DeptCode from sys_user where UserCode = @in_User
select @Code = @Code + @DeptCode
end
fetch next from cur into @CodeRuleNo, @InitValue, @length, @Level
end
close cur
deallocate cur
--Output
select @OutCode = @Code
if @ShowRs = 1
select Code = @Code
end
----------------------------------------------------------------
USE [ChangHong_612]
GO
/****** Object: StoredProcedure [dbo].[st_MES_GetCodeInitValue] Script Date: 09/10/2015 17:46:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[st_MES_GetCodeInitValue] (
@ret varchar(20) output
, @in_RuleID int
, @in_Level int
, @in_CodeRuleNO varchar(20) = ''
, @in_Length int
, @in_InitValue varchar(20) = ''
)
/*************
creator: Wuchun
create date: 2012.11.06
Remark: 按编码规则流水号值
**************/
as
begin
begin tran
if (not exists(select 1 from Sys_CodeRuleValue
where RuleId = @in_RuleID and level = @in_Level and CodeRuleNo = @in_CodeRuleNO))
begin
if @in_InitValue = ''
select @in_InitValue = '1'
else
begin
if isnumeric(@in_InitValue) = 1
select @in_InitValue = convert(int,@in_InitValue)+1
else
select @in_InitValue = '1'
end
insert into Sys_CodeRuleValue(RuleID,CodeRuleNO, Level,value,updateTime)
select @in_RuleID, @in_CodeRuleNO, @in_Level, @in_InitValue, getdate()
end
else
begin
select @in_InitValue = value
from Sys_CodeRuleValue with(rowlock,xlock)
where RuleId = @in_RuleID and level = @in_Level
and CodeRuleNo = @in_CodeRuleNO
select @in_InitValue = convert(int,@in_InitValue)+1
update Sys_CodeRuleValue with (rowlock) set value = @in_InitValue, updateTime = getdate()
where RuleId = @in_RuleID and level = @in_Level
and CodeRuleNo = @in_CodeRuleNO
end
commit tran
select @ret = replace(str(right(@in_InitValue,@in_Length), @in_Length), space(1), '0')
end