日期:2014-05-19  浏览次数:20571 次

求助:取用户列表的存储过程,不知道哪错了?
写了一个存储过程,不知道哪错了!请高手指点一下吧,谢谢了!
CREATE   PROCEDURE   Role_GetMembers
@roleid   int
as

declare   @str   nvarchar(500)
declare   user_cursor   cursor   for  
select   userid   from   userinrole   where   roleid   =   @roleid

open   user_cursor

fetch   next   from   user_cursor   into   @str
while   @@fetch_status   =   0  
begin
set   @str   =   @str   +   ', '
end
close   user_cursor
deallocate   user_cursor
select   *   from   users   where   [id]   in   (@str)

go

其中,userid字段为INT类型。



------解决方案--------------------
另外, 其实这样就可以了, 不要写得那么复杂

CREATE PROCEDURE Role_GetMembers
@roleid int
as
select * from users where [id] in(
select userid from userinrole where roleid = @roleid)
------解决方案--------------------
CREATE PROCEDURE Role_GetMembers
@roleid int
as

declare @str nvarchar(500)
set @str= ',-1 '
declare @userid int
declare user_cursor cursor for
select userid from userinrole where roleid = @roleid

open user_cursor

fetch next from user_cursor into @userid
while @@fetch_status = 0
begin
set @str = @str + ', ' + @userid
fetch next from user_cursor into @userid
end
close user_cursor
deallocate user_cursor

set @str=stuff(@str, 1, 1, ' ')
select * from users where [id] in (@str)

go