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

@requestcode以 NULL 开始(除非已分配),因此第一个 WHILE 条件检查始终为真,这至少提供一次迭代

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 回答