如何在SQL SERVER 2005存储过程中,使用循环语句

复制代码 代码如下:

creat procedure tester  

as

begin

    set nocount on;

    declare @userid varchar(50)

    declare @count int

    set @count = 0

    select @count = count(*) from   userservice_user where account like ‘%111%’

   while @count > 0

    begin

        select @userid = id from   userservice_user where account like ‘%111%’

        exec userservice_removeuserbyuserid @userid

        set @count = @count -1

    end

end

说明:
1、此存储过程在sql server 2005上测试通过,值得注意的是,循环体中,语句是使用begin……end包括的,而不是网络上常说的while ……end while结构,其他的循环语句,如loop ……until……end loop也不能通过编译,也许是版本的问题,但在sql server2005中,循环体使用begin……end就可以,而不能使用网络上常说的while ……end while结构。

2、循环体中 userservice_removeuserbyuserid 是一个存储过程的名称,@userid为该存储过程的参数,如果有多个参数,使用“,”分开就可以了,这也是存储过程调用另一个存储过程的一种方法。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐