USE [TEST]GO/****** Object: UserDefinedFunction [dbo].[RANDTENNUMS] Script Date: 2019/7/23 15:40:16 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ON ...
USE [TEST]
GO
/****** Object: UserDefinedFunction [dbo].[RANDTENNUMS] Script Date: 2019/7/23 15:40:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE function [dbo].[RANDTENNUMS]()
returns varchar(10)
as
begin
DECLARE @i int
DECLARE @flag int
DECLARE @SerialNumber nvarchar(20)
DECLARE @RANDS Numeric(38,38)
--初始化設定
SET @i=1
SET @SerialNumber = ''
--生成10位隨機碼
WHILE @i<11
BEGIN
--設置隨機,這個隨機會選擇字母(大小寫)還是數字
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
SET @flag=ceiling(@RANDS *3)
IF @flag=1
BEGIN
--隨機字母(大寫去除大寫的O)
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
WHILE char(65+ceiling(@RANDS * 25)) = 'O'
BEGIN
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
END
SELECT @SerialNumber=@SerialNumber+char(65+ceiling(@RANDS * 25))
END
else if @flag=2
begin
--隨機字母(小寫去除小寫的o)
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
WHILE char(97+ceiling(@RANDS * 25)) = 'o'
BEGIN
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
END
select @SerialNumber=@SerialNumber+char(97+ceiling(@RANDS * 25))
end
else begin
--隨機數字 1至9的隨機數字(整數)
WHILE cast(ceiling(@RANDS * 9) as varchar(1)) = '0'
BEGIN
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
END
SELECT @RANDS = RAND_A FROM [dbo].[VW_RAND]
select @SerialNumber=@SerialNumber+cast(ceiling(@RANDS * 9) as varchar(1))
end
--進行下一個迴圈
SET @i=@i+1
END
RETURN @SerialNumber;
END