views:

81

answers:

4
+1  Q: 

SQL insert error

Below code saying error

incorreect syntax near "Main"

  INSERT INTO tbl  (
 'Week', 
 Main, 
 a, 
 b, 
 c, 
 d, 
 e                     
 )
 Select  'Week', 
 Main, 
 a, 
 b, 
 c, 
 d,
 e   
 FROM  tbl_link
+2  A: 
INSERT INTO tbl  (
    'Week',

You should insert actual field name here.

If you field is called Week, just get rid of the quotes:

  INSERT INTO tbl  (
        Week, 
        Main, 
        a, 
        b, 
        c, 
        d, 
        e                     
        )
        Select  'Week', 
        Main, 
        a, 
        b, 
        c, 
        d,
        e   
        FROM    tbl_link

, otherwise substitute the field name.

Quassnoi
Week is the field name
A: 
  INSERT INTO tbl  (
     [Week], 
     Main, 
     a, 
     b, 
     c, 
     d, 
     e                     
     )
     Select  [Week], 
     Main, 
     a, 
     b, 
     c, 
     d,
     e   
     FROM  tbl_link
adatapost
Still its giving the same error
+2  A: 

To clarify the other answers:

SQL INSERT syntax with SELECT statements is as followings:

INSERT INTO Table(Column1Name, Column2Name, ...)
SELECT
Column1Data, Column2Data, ...
FROM Table

The parenthesized list after the table name you intend to update is the ordered list of columns on that table that you intend to populate. It does not represent data that will make its way to the table.

David Andres
A: 

there are two possible issues here:

  • Your column "week" is a reserved word, so use square braces: [week] when you refer to it in any query.
  • Based on your INSERT attempt, your tables tbl and *tbl_link* must should both have columns named: [week],Main,a,b,c,d, and e. If not you must rethink what you are trying to do. The first list of columns have to all exist within table tbl, and the second list of columns must all be within table *tbl_link*. If there are column in tbl that are not in *tbl_link*, you can just eliminate them from the INSERT:

    INSERT INTO tbl ( [Week], a, b, c ) Select [Week], a, b, c FROM tbl_link

KM