select case when States=1 then N'总合计' else pianqu end as pianqu,
  case when Citys=1 then N'合计' else fahuodanwei end as fahuodanwei,
  maozhong,pizhong,jingzhong,ykl,hangshu  
from (
select pianqu,fahuodanwei,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(jingzhong) as jingzhong,sum(ykl) as ykl ,sum(hangshu) as hangshu ,Grouping(pianqu) as States,Grouping(fahuodanwei) as Citys
from sysdb
where gbsj between '2012-03-01 00:00:00' and '2012-03-31 23:59:59'
group by pianqu,fahuodanwei with rollup) q
大家好,我想在程序里通过赋值的方式把上面的sql语句赋值给变量sqlstr,并且把两个时间段分别用变量ts1和ts2表示应该怎样改?
       ts1:=FormatDateTime('yyyy-mm-dd',DateTimePicker1.Date)+' '+FormatDateTime  ('hh:mm:ss',DateTimePicker3.Time);       ts2:=FormatDateTime('yyyy-mm-dd',DateTimePicker2.Date)+' '+FormatDateTime('hh:mm:ss',DateTimePicker4.Time);
         ADOQuery6.Close;
         ADOQuery6.SQL.Clear; 
         ADOQuery6.SQL.add(sqlstr)
........
我怎么改也改不好,

解决方案 »

  1.   

    sqlstr:='... gbsj between '+Quotedstr(ts1)+' and '+Quotedstr(ts2)+' ...';
    其它地方,1个单引号变2个
      

  2.   

    很简单把sqlstr拿到sql里执行,看看哪里出错,sql会告诉你哪行哪个地方有错误
      

  3.   

    adoquery.Sql.Text := 'select case when States=1 then N''总合计'' else pianqu end as pianqu, '
     +' case when Citys=1 then N''合计'' else fahuodanwei end as fahuodanwei, '
     +' maozhong,pizhong,jingzhong,ykl,hangshu  '
    + ' from (  '
    + ' select pianqu,fahuodanwei,sum(maozhong) as maozhong,sum(pizhong) as pizhong,sum(jingzhong) as jingzhong,sum(ykl) as ykl ,sum(hangshu) as hangshu ,Grouping(pianqu) as States,Grouping(fahuodanwei) as Citys '
    + ' from sysdb '
    + ' where gbsj between '+quotedStr(ts1)+' and '+quotedStr(ts2)
    + ' group by pianqu,fahuodanwei with rollup) q  ';
    adoquery.Open();