日期:2014-05-18  浏览次数:20451 次

关于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] a
inner join table2 b
    on a.id=b.userid
select stuff(@str,1,5,'')

------解决方案--------------------
存储过程只能用游标了!!