procedure form1.button1Click(Sender: TObject);
var
  T1,T2:string;
begin
  T1:=ComboBox1.Text;//=2004
  T2:=ComboBox2.Text;//=11      with adoquery1 do
         begin
           Close;
           SQL.Clear;
           SQL.Add('select Employees.EmployeeID,Employees.Name,Employees.Department,book.bookID,book.bookcaption,LendBook.Lenddate,LendBook.Returndate');
           SQL.Add('FROM book INNER JOIN (Employees INNER JOIN LendBook ON Employees.EmployeeID=LendBook.EmployeeID) ON book.bookID=LendBook.bookID');
           SQL.Add('WHERE datepart(year,LendBook.Lenddate)='+T1+' AND datepart(month,LendBook.Lenddate)='+T2);
           Open;
         end;
end;三个表,分别是:Employees : EmployeeID, Name, Departmentbook : bookID, bookcaptionLendBook : EmployeeID, bookID, Lenddate, Returndate运行时显示错误:  "year"没有默认值!数据库的三个表里有相关数据!请问要怎么处理这个错误?还有当连接两个表时,在Where条件中使用 {fn Now()} 会出错!请问是什么原因?但是连接单个表时,在SQL语句中的Where条件中使用 {fn Now()}, datepart(year,date) 就没有问题!谢谢!

解决方案 »

  1.   

    SQL.Add('WHERE datepart(year,LendBook.Lenddate)='''+T1+''' AND datepart(month,LendBook.Lenddate)='''+T2+'''');
      

  2.   

    建议你设上断点,然后取出最后拼好的Sql串,在数据库工具(你用的是Sql Server吧)中排查错误
      

  3.   

    建议先用查询分析器测试后再加到Delphi里,这样差错比较容易点。
      

  4.   

    zhlwyy(LOVEYANSH) 你那方法我早就试过了!问题依然!谢谢关注!!!
      

  5.   

    'WHERE Lenddate between '''+T1+'-'+T2+'-01''  and  DATEADD (month,1,'''+T1+'-'+T2+'-01'')'看看
      

  6.   

    datepart(year,LendBook.Lenddate)返回的是整型,而T1是string
      

  7.   

    procedure form1.button1Click(Sender: TObject);
    var
      T1,T2:string;
      sSql : String;   // add by pdbird for test 20041118
    begin
      T1:=ComboBox1.Text;//=2004
      T2:=ComboBox2.Text;//=11  sSql := 'select   mployees.EmployeeID,Employees.Name,Employees.Department,book.bookID,book.bookcaption,LendBook.Lenddate,LendBook.Returndate '
         + ' FROM book INNER JOIN (Employees INNER JOIN LendBook ON  Employees.EmployeeID=LendBook.EmployeeID) ON book.bookID=LendBook.bookID '
         + 'WHERE Lenddate between '''+T1+'-'+T2+'-01''  and  DATEADD (month,1,'''+T1+'-'+T2+'-01'')'      with adoquery1 do
          begin
            Close;
            SQL.Clear;
            SQL.Add(sSql)
            Open;
          end;
    end;你写代码要写的清晰一点才好看,试试这个行不?