关于sql server中用游标动态拼写sql语句 财富值22

2016-10-27 10:59发布

因为sql语句很庞大,都是用union连接的,所以我需要的sql语句中选择的条件格式是这样的

...num as areaname1,' as areaname2,' as areaname3,' as areaname4,' as areaname5... union ...' as areaname1,num as areaname2,' as areaname3,' as areaname4,' as areaname5... union ...' as areaname1,' as areaname2,num as areaname3,' as areaname4,' as areaname5... union ...' as areaname1,' as areaname2,' as areaname3,num as areaname4,' as areaname5... union ...' as areaname1,' as areaname2,' as areaname3,' as areaname4,num as areaname5...

以上看到了,就是每个列都要循环一遍,并且num的位置是变化的.

这是我声明的游标:

declare _cursor CURSOR forward_only FOR--声明游标 select areacode,areaname from #temp DECLARE @areacode varchar(20)--游标变量 declare @areaname varchar(20)--游标变量

现在就是要将由表中的areaname都要循环一遍拼成选择条件,然后num位置也要变化。

友情提示: 问题已经关闭,关闭后问题禁止继续编辑,回答。