1

これで私を助けてください、ストアドプロシージャで再帰ステートメントを実行するにはどうすればよいですか。ここに私が欲しいものがあります

-- @requestcode will genereate some random string i have already the code below

set @requestcode = (SELECT substring(@username,0,3)+'-'+SUBSTRING(CONVERT(varchar(255), NEWID()), 0, 9))

-- then i want to check if the string generated is existing to 'sampletable'
select @requestcode from sampletable

-- if it is existing back to the @requestcode query until it is not existing

前もって感謝します

4

1 に答える 1

3

@requestcodeNULL として開始するため (既に割り当てられていない限り)、最初の WHILE 条件チェックは常に true であり、少なくとも 1 回の反復が行われます

WHILE @requestcode IS NULL OR
        EXISTS (SELECT * FROM sampletable WHERE requestcode = @requestcode)
BEGIN
    SELECT @requestcode = substring(@username,0,3) + '-' + 
           SUBSTRING(CONVERT(varchar(255), NEWID()), 0, 9));
END
于 2013-06-13T07:22:13.770 に答える