Query Syntax error

后端 未结 4 1674
渐次进展
渐次进展 2021-01-27 23:14

Please help me correct the following query:

SQL = \"insert into tblContract (Empid, Start1, Finish1, Store1, \" & _
                    \"Start2, Finish2, St         


        
相关标签:
4条回答
  • 2021-01-27 23:48

    Sub-queries should be enclosed in parentheses: (select max(testid) FROM tbltesting)

    Note that your SQL engine will probably not support sub-queries in the INSERT statement, so you should insert using a INSERT...SELECT query instead.

    0 讨论(0)
  • 2021-01-27 23:53

    You need to re-write your VALUES clause as a SELECT query.

    You have seven columns in your INSERT clause and eight in your VALUES clause. From the column names I guess your subquery

    select max(testid) FROM tbltesting
    

    is missing a destination. Guessing it may be called starting_testid; also guessing data types (Access database engine ANSI-92 Query Mode syntax):

    CREATE PROCEDURE AddContract
    (
     :Empid INTEGER, 
     :Start1 DATETIME, 
     :Finish1 DATETIME,
     :Store1 VARCHAR(20), 
     :Start2 DATETIME,
     :Finish2 DATETIME,
     :Store2 VARCHAR(20)
    )
    AS
    insert into tblContract 
    ( 
     Empid, starting_testid, 
     Start1, Finish1, Store1, 
     Start2, Finish2, Store2
    )
    SELECT :Empid, max(testid), 
           :Start1, :Finish1, :Store1, 
           :Start2, :Finish2, :Store2
      FROM tbltesting;
    
    0 讨论(0)
  • 2021-01-27 23:59

    Just remove VALUES:

    SQL =  "
           INSERT INTO tblContract (Empid, Start1, Finish1, Store1, Start2, Finish2, Store2)
           SELECT " & Me.txtEmpNo.Value & "', MAX(testid), '" &
           Me.txtContSunStart1.Value & "', '" & Me.txtContSunFinish1.Value & "','" & 
           Me.txtContSunStore1.Value & "','" & Me.txtContSunStart2.Value & "', '" & 
           Me.txtContSunFinish2.Value & "','" & Me.txtContSunStore2.Value & "' " &
           "FROM tbltesting'"
    

    Since you have 7 target fields and 8 SELECT list expressions, this won't compile until you provide an extra field in the INSERT clause where you want your MAX(testid) to go to.

    0 讨论(0)
  • 2021-01-27 23:59

    If anything is going to work, you will have to embed the sub-query inside an extra layer of parentheses:

    INSERT INTO SomeTable(Col1, Col2, Col3)
        VALUES(val1, (SELECT MAX(testid) FROM tbltesting), val3);
    

    Be aware that you are vulnerable to SQL Injection attacks.


    Using IBM Informix Dynamic Server 11.50 in a database with a 'table of elements', the following works:

    create temp table t(i integer, j integer, k integer);
    insert into t values(1, (select max(atomic_number) from elements), 2);
    select * from t;
    
    1   118     2
    

    This is correct given the current data in the table of elements.

    0 讨论(0)
提交回复
热议问题