SQL 查询上级存储过程

让人想犯罪 __ 提交于 2020-01-22 01:17:09

------------------------SQL 查询上级存储过程----------------------------

create proc proc_treeUpQuery
@id varchar(20)
as
declare @count int
declare @sql varchar(5000)
declare @temp varchar(2000)
declare @tempCount nvarchar(2000)
set @sql = 'select superiorid from b_userlist where userid = ' + @id
set @temp = 'select superiorid from b_userlist where userid = ' + @id
while (1=1)
begin
  set @tempCount = 'select @count=count(superiorid) from b_userlist where userid in (' + @temp + ')'
  exec sp_executesql @tempCount,N'@count int output',@count output
  if (@count=0)
   begin
    break
   end
  else
   begin
    set @temp = 'select superiorid from b_userlist where userid in (' + @temp + ')'
    set @sql = @sql + ' union ' + @temp
   end
end
exec(@sql)
go

exec proc_treeUpQuery '0009'

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!