with TADOQuery.Create(self) do
  begin
    Connection := DataModuleMain.ADOConnection;
    SQL.Clear;
    SQL.Add('Insert Into SB_T_Electric_Revenue(Corporation_ID,Year, Item_ID)');
    SQL.Add('Select ' + QuotedStr(g_s_Corporation_ID));
    SQL.Add(', ''' + QuotedStr(EditYear.Text) + ', Item_ID');
    SQL.Add('From SB_T_Electric_Item_Set');
    SQL.Add('Where Item_Type_Code=' + QuotedStr('1'));
    SQL.Add('Order By Order_ID;');
    try
      ExecSQL;
    finally
      Free;
    end;为什么提示Insert Into语句语法错误???(数据库为Access,把该语句直接在Delphi的SQL Expolrer中执行没问题,是不是在Delphi中不支持在Select中带变更或常量值,因为我如果只在Select中取Item_ID字段值,则不会提示语法错误)

解决方案 »

  1.   

    SQL.Add(' Select ' + QuotedStr(g_s_Corporation_ID));
        SQL.Add(', ''' + QuotedStr(EditYear.Text) + ', Item_ID ');
        SQL.Add(' From SB_T_Electric_Item_Set');
        SQL.Add(' Where Item_Type_Code=' + QuotedStr('1'));
        SQL.Add(' Order By Order_ID;');请注意换行时在关键字前加上空格
    否则关键字会与前面的内容练成一个字符串
    当然就出错了
      

  2.   

    你用一个字符串变量将其保存起来再......
    var
      strsql :string;strsql := 'Insert Into SB_T_Electric_Revenue(Corporation_ID,Year, Item_ID)......';
    sql.add(strsql);这样可以showmessage(strsql)就可以看看是什么错误了
      

  3.   

    这是语句生成的真实SQL语句,Insert Into SB_T_Electric_Revenue(Corporation_ID,Year, Item_ID)
     Select '123123'
     ,'2004', Item_ID 
     From SB_T_Electric_Item_Set
     Where Item_Type_Code='1'
     Order By Order_ID;
      

  4.   

    SQL.Add(', ''' + QuotedStr(EditYear.Text) + ', Item_ID');
    写错了,应该是:SQL.Add(', ' + QuotedStr(EditYear.Text) + ', Item_ID');
    但错误依旧, lufancy(聆雨)所说的并没用
      

  5.   

    换行前,是不用加空格的
    你最好换成变量形式的
    var
      strsql :string;strsql := 'Insert Into SB_T_Electric_Revenue(Corporation_ID,Year, Item_ID)......';
    sql.add(strsql);
    应该就没问题啦,试试把!!!!
      

  6.   

    你或者用Format
    SQL.Add(Format('Select %s ' ,[g_s_Corporation_ID]));
    下边仿照此写法
    若你的语句没错,肯定行的
      

  7.   

    select后的字段是不能使用变量字段的,你一定要用Format或直接用语句变量
      

  8.   

    楼上的方法也不行,估计是Select出的字段不能是变量或常量!!!
      

  9.   

    我发现如果通过ODBC链接ACCESS则以上语句完全正确,如果通过JET链接ACCESS则不行,不知是怎回事估计是引擎的瓿
      

  10.   

    我也碰到过类似的问题,但是我后来select 出来字段指定,insert的字段指定 ,就可以正确执行了
      

  11.   

    现在终于明白了,原来的我Access数据库中一表字段为Year,那么在delphi中:
    insert into test(id,year) value(:id,:year)在Access中会把Year当作Acces的Year涵数
    改成insert into test(id,[year]) value(:id,:year)问题解决