1. 程式人生 > >SQL SERVER 產生隨機數的幾種辦法

SQL SERVER 產生隨機數的幾種辦法

--產生隨機數的幾種辦法:
SELECT  CHAR(65 + CEILING(RAND() * 25))
SELECT  RIGHT(RAND(), 1)
SELECT  CHAR(65 + CEILING(RAND() * 25))
SELECT  UPPER(CHAR(65 + CEILING(RAND() * 25)))
SELECT  RIGHT(RAND(), 1)
SELECT  LOWER(CHAR(65 + CEILING(RAND() * 25))) + UPPER(CHAR(65
                                                            + CEILING(RAND()
                                                              * 25)))
        + RIGHT(RAND(), 1)
-----------------------------------------------------------------
--通過函式返回指定長度的隨機數

ALTER VIEW v_yhj_rand
AS
    SELECT  RAND() AS v_yhj_randA;
GO
CREATE FUNCTION fn_gen_Round ( @len INT  -- 隨機數長度
                               )
RETURNS VARCHAR(20)
AS
    BEGIN
        DECLARE @retValue DATETIME;
    --Set @retValue = '2016-07-01 08:00'

/*
select char(65+ceiling(rand()*25))   --隨機字母(大寫)
select char(97+ceiling(rand()*25))   --隨機字母(小寫)
select cast(ceiling(rand()*9) as varchar(1))   --隨機數字 1至9的隨機數字(整數)
*/

        DECLARE @i INT;           
        DECLARE @flag INT;
        DECLARE @ra VARCHAR(20);  ---最後會生成的隨機碼
        DECLARE @ratemp VARCHAR(20);  ---最後會生成的隨機碼

--初始化設定
        SET @i = 1;
        SET @ra = '';

--生成指定位數位隨機碼
        WHILE @i < CAST(@len AS NVARCHAR(2)) + 1
            BEGIN
--設定隨機,這個隨機會選擇字母(大小寫)還是數字
--set @flag=ceiling(v_yhj_randA() * 3)  
                SELECT  @flag = CEILING(v_yhj_randA * 3)
                FROM    v_yhj_rand;
                IF @flag = 1
                    BEGIN
--隨機字母(大寫)

                        SELECT  @ratemp = CHAR(65 + CEILING(v_yhj_randA * 25))
                        FROM    v_yhj_rand;
-- select @
[email protected]
+char(65+ceiling(rand()*25)) SELECT @ra = @ra + @ratemp; END; ELSE IF @flag = 2 BEGIN --隨機字母(小寫) SELECT @ratemp = CHAR(97 + CEILING(v_yhj_randA * 25)) FROM v_yhj_rand; --select @
[email protected]
+char(97+ceiling(rand()*25)) SELECT @ra = @ra + @ratemp; END; ELSE BEGIN --隨機數字 1至9的隨機數字(整數) --select @[email protected]+cast(ceiling(rand()*9) as varchar(1)) SELECT @ratemp = CAST(CEILING(v_yhj_randA * 9) AS VARCHAR(1)) FROM v_yhj_rand; SELECT @ra = @ra + @ratemp; END; SET @i = @i + 1; END; --print('隨機碼:'
[email protected]
) RETURN @ra; END; --SELECT dbo.fn_gen_Round(12) --測試函式效果 ----------------------------------------------------------------- --通過儲存過程計算 隨機數 CREATE PROCEDURE [dbo].p_gen_Round @len int -- 隨機數長度 AS ---- BEGIN /* select char(65+ceiling(rand()*25)) --隨機字母(大寫) select char(97+ceiling(rand()*25)) --隨機字母(小寫) select cast(ceiling(rand()*9) as varchar(1)) --隨機數字 1至9的隨機數字(整數) */ DECLARE @i int DECLARE @flag int DECLARE @ra varchar(20) ---最後會生成的隨機碼 --初始化設定 SET @i = 1 SET @ra = '' --生成指定位數位隨機碼 WHILE @i < CAST(@len AS NVARCHAR(2)) + 1 BEGIN --設定隨機,這個隨機會選擇字母(大小寫)還是數字 SET @flag = CEILING(RAND() * 3) IF @flag = 1 BEGIN --隨機字母(大寫) SELECT @ra = @ra + CHAR(65 + CEILING(RAND() * 25)) END ELSE IF @flag = 2 BEGIN --隨機字母(小寫) SELECT @ra = @ra + CHAR(97 + CEILING(RAND() * 25)) END ELSE BEGIN --隨機數字 1至9的隨機數字(整數) SELECT @ra = @ra + CAST(CEILING(RAND() * 9) AS varchar(1)) END SET @i = @i + 1 END PRINT ( '隨機碼:' + @ra ) --測試結果 /* 隨機碼:S33V95xMbw89 */ END --EXEC p_gen_Round 4 測試 -----------------------------------------------------