2

私は次の表を持っています

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'0123456789'

    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

次のエラーが表示されます。

ここに画像の説明を入力

4

2 に答える 2

2

プロシージャを使用する場合は、出力パラメータを定義できます

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

    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 @row=@row + 1
end
于 2013-07-21T12:14:43.403 に答える