exec ('select ProductCatalogName,productname'+@sql+' from exportBillSaleView 
          where CreateTime between '+'''@startTime'''+' and '+'''@endTime'''+' group by ProductCatalogName,productname order by ProductCatalogName')
 这么写为什么不可以?

解决方案 »

  1.   

    把SQL语句PRING帖出来看一下先~~~print('select ProductCatalogName,productname'+@sql+' from exportBillSaleView 
              where CreateTime between '+'''@startTime'''+' and '+'''@endTime'''+' group by ProductCatalogName,productname order by ProductCatalogName')
      

  2.   

    时间值里面带有:-之类的符号,两边加上[ ]
    exec ('select ProductCatalogName,productname'+@sql+' from exportBillSaleView 
              where CreateTime between '+'''[@startTime]'''+' and '+'''[@endTime]'''+' group by ProductCatalogName,productname order by ProductCatalogName')
    你再试一试,我感觉变量不用双引号。
      

  3.   

    报错"从字符串转换为 datetime 时发生语法错误。"
      

  4.   

    exec ('select ProductCatalogName,productname'+@sql+' from exportBillSaleView 
              where CreateTime between '+'''convert(datetime,@startTime)'''+' and '+'''convert(datetime,@endTime)'''+' group by ProductCatalogName,productname order by ProductCatalogName')建议用datediff
      

  5.   

    你加入变量的引号位置有问题!
    看看这样行么?
    exec ('select ProductCatalogName,productname'+@sql+' from exportBillSaleView 
              where createtime between '''+@startTime+''' and '''+@endTime+''' group by ProductCatalogName,productname order by ProductCatalogName')