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

GROUP BY 引用外部参数的问题
定义一个游标,@temp是外部参数
SQL code

                          declare YB1 CURSOR FOR 
                          select count(tjs6600),tjs6605,convert(varchar(100),@temp,23)
                          from tjs66
                          LEFT OUTER JOIN TRS14 ON TRS1400=TJS6605
                          left outer join tjs13 on tjs1300=tjs6602
                          where ( @temp<tjs6604 and tjs6613<dateadd(dd,1,@temp) and tjs6613 is not null ) or
                                ( @temp<tjs6604 and tjs6613 is null and tjs1319='G' and tjs6614<>6 ) or
                                ( tjs6604<@temp and tjs6613<dateadd(dd,1,@temp) and tjs6612 is not null) or
                                ( tjs6604<@temp and tjs6613 is null and tjs1319='G' and tjs6614<>6 )

                          group by tjs6605,convert(varchar(100),@temp,23)    ----问题在这



运行时出现“每个 GROUP BY 表达式都必须包含至少一个列引用”的问题,经查得知是GROUP BY不能引用外部参数,但不使用该参数无法分组,请问如何解决?

------解决方案--------------------
用exec 执行字符串