2013-07-21 62 views
2

我有如下表插入隨機數在SQL Server

create table MBR_IDS 
(
    MBR_SLNO int identity(1,1), 
    MBR_ID int 
) 
GO 

ALTER TABLE MBR_IDS 
    ADD CONSTRAINT UNIQUE_MBR_ID 
    UNIQUE NONCLUSTERED (MBR_ID) 

和下面的存儲過程創建一個隨機數

CREATE PROCEDURE USP_RANDOM_6 
AS 
BEGIN 
    DECLARE @chars NCHAR(36) 
    SET @chars = N'' 

    DECLARE @result NCHAR(6) 
    SET @result = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
      + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
      + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
      + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
      + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
      + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 

    SELECT @result 
END 
GO 

當我通過存儲過程中插入值:

declare @row int 
set @row =1 

while (@row <= 1000) 
begin 
    insert into MBR_IDS 
    values(USP_RANDOM_6) 

    set @row = @row + 1 
end 
GO 

我收到此錯誤:

enter image description here

回答

2

,如果你想使用過程中,您可以定義輸出參數

CREATE PROCEDURE USP_RANDOM_6 
(
    @result NCHAR(6) = null output 
) 
AS 
BEGIN 
    DECLARE @chars NCHAR(36) 
    SET @chars = N'' 

    SET @result = SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
       + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
       + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
       + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
    + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
    + SUBSTRING(@chars, CAST((RAND() * LEN(@chars)) AS INT) + 1, 1) 
END 
GO 

,然後用它

declare @row int, @rand nchar(6) 
set @row =1 
while (@row <= 1000) 
begin 
    exec USP_RANDOM_6 @result = @rand output 

    insert into MBR_IDS 
    values(@rand) 
set @[email protected] + 1 
end 
+0

感謝您幫助我很快..我不知道有關的輸出參數... – Nisar

4

一種選擇是創建一個函數,而不是存儲過程。

您可以通過在表中捕捉返回值做你想做的與你的存儲過程:

create table #retval (val nchar(6)); 
declare @row int = 1; 
while (@row <= 1000) 
begin 
    truncate table #retval; 

    insert into #retval 
     exec USP_RANDOM_6; 

    insert into MBR_IDS 
     select val 
     from #retval; 
    set @[email protected] + 1 
end; 

其實,如果你把它保存在一個表,你可以把一切都變成一個臨時表,做只是一個插入算賬:

create table #retval (val nchar(6)); 
declare @row int = 1; 
while (@row <= 1000) 
begin 
    insert into #retval 
     exec USP_RANDOM_6; 

    set @row = @row + 1; 
end; 

insert into MBR_IDS 
    select val 
    from #retval; 

編輯:

我不知道這是更快,存儲過程或此操作的功能。但是,我都不會使用。我只想做:

declare @row int = 1; 
while (@row <= 1000) 
begin 
    insert into MBR_IDS 
     select replace(str(checksum(newid())%1000000, 6, 0), ' ', '0') 
    set @[email protected] + 1 
end; 

這使用newid()來計算一個新的長字符串。然後它需要checksum(),這應該是一個隨機的4字節數模100萬。其餘的只是格式化它,所以它是一個以'0'爲前綴的6位字符串。

此外,我不知道當我在上述結構中引入一個臨時表時我在想什麼。該簡單的方法很簡單:

declare @row int = 1; 
while (@row <= 1000) 
begin 
    insert into MBR_IDS 
     exec USP_RANDOM_6;; 
    set @[email protected] + 1 
end; 
+0

這將是又好又快.. storeprocedure或功能... @gordon Linoff – Nisar