现在的位置: 首页 > 综合 > 正文

如下这段代码怎么传递动态参数比较合适?(问题有点长~谢谢)

2012年04月29日 ⁄ 综合 ⁄ 共 3014字 ⁄ 字号 评论关闭
如下这段代码怎么传递动态参数比较合适?(问题有点长~谢谢) Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061224164816130.html
源代码如下(不用细看):  
  代码在SQL的查询分析器中运行是成功的,改动语法方面的细节后得到以下代码!  
  select   a.company   AS   company   ,b.total_suttle   AS   last_left,this_in   ,this_p   ,this_t   ,e.total_suttle   AS   this_left  
  from   gongsi   a    
  left   join    
                  (  
                    SELECT   kehe,total_suttle                                                  
                    FROM    
                            (  
                              SELECT   kehe   as   lcompany,maxdate=MAX(c_date)      
                              FROM   storagetotal    
                              WHERE   c_date<=:starttime    
                              GROUP   BY   kehe  
                            )Kehe_and_Date,storagetotal    
                    WHERE   kehe=lcompany   AND   c_date=maxdate  
                  )   b    
  on   a.company=b.kehe    
  left   join  
                  (                                                                                                
                    SELECT   kehe,SUM(s_weight)   AS   this_in                        
                    FROM   total    
                    WHERE   s_time   BETWEEN   :starttime   and   :endtime    
                    GROUP   BY   kehe  
                  )c    
  on   a.company=c.kehe  
  left   join    
                (                                                                                            
                    SELECT   kehe,SUM(p_weight)   AS   this_p,SUM(t_weight)   AS   this_t  
                    FROM   total  
                    WHERE   alter_time   BETWEEN   :starttime   and   :endtime    
                    GROUP   BY   kehe    
                )d      
  on   a.company=d.kehe  
  left   join                                                                                        
              (    
                  SELECT   kehe,total_suttle  
                  FROM  
                            (  
                              SELECT   kehe   as   lcompany,maxdate=MAX(c_date)    
                              FROM   storagetotal    
                              WHERE   c_date<=:endtime    
                              GROUP   BY   kehe  
                            )Kehe_and_Date,storagetotal    
                  WHERE   kehe=lcompany   AND   c_date=maxdate  
              )e  
  on   a.company=e.kehe  
   
  其中的动态参数是:startime和endtime,他们分别来自界面上的两个TDateTimePicker控件starttime1和endtime1。  
  我试过好几种方法  
  1、把以上代码写到存储过程中,用ADOStoredProc组件连接,然后的的代码是:  
  with   ADOStoredProc1   do  
          begin  
                  Paramaters.ParamByName('@starttime').Value=DateTimePicker1.date;  
                  Paramaters.ParamByName('@endtime').Value=DateTimePicker2.date;  
                  ExecProc;  
          end;  
  这个方法的结果是程序成功运行,但是在DBGrid中却什么都没有。有人说存储过程可以返回数据集,有人又说不行,到底行还是不行啊?行的话又该如何返回显示呢?  
  2、在Object   inspector下使用ADOQuery控件的SQL属性,直接在其下的List   editor下粘贴以上代码,从其Parameters属性下看到了六个变量参数  
  starttime  
  starttime  
  endtime  
  starttime  
  endtime  
  endtime  
  然后我在程序中写入了  
  with   ADOQuery2   do  
          begin  
            Parameters.ParamByName('starttime').Value:=starttime1.date;  
            Parameters.ParamByName('endtime').Value:=endtime1.date;  
            ExecSQL;  
          end;  
  运行启动该程序的按钮后系统报错,说“不正常的定义参数对象,提供了不一致或不完整的信息”  
            然后我又按Parameters属性中六个参数的排列顺序又写了一遍,即写了六条之前的传递参数值的语句,得到的结果还是一样!  
  3、我把以上查询语句全部以字符串形式给一个SQLstr的字符型变量的参数,然后用ADOquery控件的SQL.Add方法加载,然后运行得到的结果仍然是那样~  
   
  我还试了其他方法,很多时候又报错说找不到starttime这个参数!  
   
  我所以现在的疑问是:这么长而又复杂的一段select查询语句,还有动态参数,要用什么样的方法来实现最后的显示数据集的功能比较合适?因为复杂,所以细节都比较琐碎,不容易注意到,所以能找尽量直接的方法!  
 

希望能得到一些具体的建议吧~因为这个问题困扰了我几天了,总找不到合适的解决方法~  
  存储过程能解决的话,用存储过程是比较方便的。但是具体怎么用呢?  
 

你的问题只有一个:  
  :starttime   和   :endtime   不能重复使用在同一个adoquery的参数表里。  
   
  处理办法:  
  重新命名:  
  :starttime1~:starttime3  
  :endtime1~:endtime3  
  重新赋值  
   
  再运行  
   
   
 

谢谢,我试一下看看!

果然如你所说!!  
  万分感谢了!!  
  呵呵~~  
  八十分请您笑纳~~

抱歉!评论已关闭.