当前位置: 代码迷 >> Sql Server >> 关于sqlserver存储过程循环,该如何解决
  详细解决方案

关于sqlserver存储过程循环,该如何解决

热度:66   发布时间:2016-04-27 15:27:30.0
关于sqlserver存储过程循环
举个例子,用ASP写的
Set rs = conn.execute ("Select id,name from Table")
Do while not rs.eof
  Set rs2 = conn.execute ("Select userid,school from Table2 where userid = "&rs("id") )
  dim gets = ""
  Do while not rs2.eof
  gets = gets&rs2("school")&"<br>"
  rs2.movenext
  Loop
rs.movenext
loop

response.write gets

请问用存储过程怎么写?一定要用游标吗?

------解决方案--------------------
一个递规查询变量,连表解决问题.
SQL code
declare @str varchar(8000)select @str=isnull(@str,'') + "<br/>" + b.school     from [table] ainner join table2 b    on a.id=b.useridselect stuff(@str,1,5,'')
------解决方案--------------------
存储过程只能用游标了!!
  相关解决方案