读书人

取用户列表的存储过程不知道哪错了

发布时间: 2012-01-18 00:23:26 作者: rapoo

求助:取用户列表的存储过程,不知道哪错了?
写了一个存储过程,不知道哪错了!请高手指点一下吧,谢谢了!
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

读书人网 >SQL Server

热点推荐