MSSQL 生成有意义的PROC

MSSQL 生成有意义的PROC

--MSSQL  用PROC 生成有意义的单号:如WP200011101

GO
/****** 对象:  Table [dbo].[tbl_SequenceNum]    脚本日期: 01/20/2011 10:13:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_SequenceNum](
 [ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL CONSTRAINT [DF_CM_SequenceNumber_ID]  DEFAULT (newid()),
 [SequenceNumTypeCode] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
 [CurrNumber] [int] NOT NULL,
 CONSTRAINT [PK_CM_SequenceNumber] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNum', @level2type=N'COLUMN',@level2name=N'ID'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'生成序列号的类型(CHG,SRV,CHW)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNum', @level2type=N'COLUMN',@level2name=N'SequenceNumTypeCode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'当前流水号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNum', @level2type=N'COLUMN',@level2name=N'CurrNumber'
GO

GO
/****** 对象:  Table [dbo].[tbl_SequenceNumType]    脚本日期: 01/20/2011 10:15:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tbl_SequenceNumType](
 [SequenceNumTypeCode] [nvarchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
 [SequenceNumTypeName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL,
 [IncreaseLength] [int] NOT NULL,
 [Prefix] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NOT NULL,
 [initNum] [int] NOT NULL,
 [Length] [int] NULL,
 CONSTRAINT [PK_tbl_SequenceNumType] PRIMARY KEY CLUSTERED 
(
 [SequenceNumTypeCode] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'生成序列号的类型(CHG,SRV,CHW)' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'SequenceNumTypeCode'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'生成流水号类型的名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'SequenceNumTypeName'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'流水号自增量' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'IncreaseLength'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'流水号前缀' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'Prefix'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'流水号初始值' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'initNum'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'生成ID除字母前缀外的总长度' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'tbl_SequenceNumType', @level2type=N'COLUMN',@level2name=N'Length'

GO

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[ufn_getSpecialTypeID]
(@SequenceNumTypeCode nvarchar(20))
RETURNS nvarchar(20)
AS
BEGIN
 DECLARE @returnValue nvarchar(20)
 DECLARE @CurrNumber int
 DECLARE @IncreaseLength int
 DECLARE @initNum int
 DECLARE @Prefix varchar(10)
 DECLARE @Length int

SET @CurrNumber = 0

SELECT @IncreaseLength = IncreaseLength,
  @initNum = initNum,
  @Prefix = Prefix,
  @Length = Length
 FROM tbl_SequenceNumType
 WHERE SequenceNumTypeCode = @SequenceNumTypeCode

SELECT @CurrNumber = CurrNumber
 FROM tbl_SequenceNum
 WHERE SequenceNumTypeCode = @SequenceNumTypeCode

IF @CurrNumber = 0 
  SET @CurrNumber = @initNum

SET @CurrNumber = @CurrNumber + @IncreaseLength

SET @returnValue = '00000000' + CONVERT(nvarchar(10), @CurrNumber)
 SET @returnValue = RIGHT(@returnValue, @Length)
 SET @returnValue = @Prefix + CONVERT(nvarchar(20),@returnValue)

RETURN @returnValue

END

GO

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER PROCEDURE [dbo].[usp_WO_UpdateCurrNumBySequenceNumTypeCode]
@SequenceNumTypeCode nvarchar(20)
AS
SET NOCOUNT ON

DECLARE @CurrNumber nvarchar(20)

DECLARE @Prefix varchar(10)
SELECT @Prefix = Prefix
FROM tbl_SequenceNumType
WHERE SequenceNumTypeCode = @SequenceNumTypeCode

SET @CurrNumber = dbo.ufn_getSpecialTypeCurrNum(@SequenceNumTypeCode)

IF EXISTS(SELECT 1 FROM tbl_SequenceNum WHERE SequenceNumTypeCode = @SequenceNumTypeCode)
BEGIN
 --UPDATE
 UPDATE tbl_SequenceNum
 SET CurrNumber = @CurrNumber
 WHERE SequenceNumTypeCode = @SequenceNumTypeCode
END
ELSE
BEGIN
 --INSERT
 INSERT INTO tbl_SequenceNum
 (
  ID,
  SequenceNumTypeCode,
  CurrNumber
 )
 VALUES
 (
  NEWID(),
  @SequenceNumTypeCode,
  @CurrNumber
 )
END

SET NOCOUNT OFF

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER FUNCTION [dbo].[ufn_IsID]
(@Param nvarchar(50), @Prefix nchar(3))
RETURNS bit
AS
/*
返回 1是数字  0不是数字
*/
BEGIN
 DECLARE @IsID bit
 SET @IsID = 0

IF LEN(@Param) = LEN(@Prefix) + 9
 BEGIN
  IF ISNUMERIC(RIGHT(@Param, LEN(@Param) - LEN(@Prefix))) = 1 AND LEFT(@Param, LEN(@Prefix)) = @Prefix
  BEGIN
   SET @IsID = 1
  END
 END
 ELSE
 BEGIN
  IF ISNUMERIC(@Param) = 1
  BEGIN
   SET @IsID = 1
  END  
 END

RETURN @IsID
END
GO

--具体事例
--生成文件编号
DECLARE @DocNo NVARCHAR(100) 
DECLARE @SequenceNumTypeCode_Req nchar(4)
SET @SequenceNumTypeCode_Req = 'DOC'
SET @DocNo = dbo.ufn_getSpecialTypeID(@SequenceNumTypeCode_Req)
EXEC usp_WO_UpdateCurrNumBySequenceNumTypeCode @SequenceNumTypeCode = @SequenceNumTypeCode_Req
GO

上一篇:解决移动端300ms延迟fastclick


下一篇:C# 使用ZXing.NET生成一维码、二维码