AGx-07_162

Honorable
Sep 16, 2013
58
0
10,610
Can anyone help me understand why I get a "Syntax error in INSERT INTO statement" error with this query?

INSERT INTO Employees (Title, FirstName, LastName, Company, Department, Mailstop, Extension, City, State, Bldg, Floor, DirList, Prefix, Notes, Phone, importID, importDate, Desc)

SELECT Title, FirstName, LastName, Company, Department, Mailstop, Extension, City, State, Bldg, Floor, DirList, Prefix, Notes, Phone, importID, importDate, Desc

FROM dbo_Employees


Please take note of the column in blue, "Desc". This appears to be the problem column as if I remove it from the statement the query works fine. The confusing thing is that, with the exception of the importID column, every column in both tables are exactly the same. And I mean exactly the same. They are all Text columns with the exact same properties. I even looked specifically at the Desc column in both tables and made sure each of their properties were exactly the same. Same length, no default values, neither are required, indexed, both allow zero length. They are practically twins.

Any ideas why I'm getting this error?
 
Solution
Desc is a SQL keyword. It's bad practice to name a table column "Desc" or any other reserved SQL keywords. But if you want to stick with it, use backticks - `Desc`

ksham

Honorable
Mar 29, 2013
175
0
10,760
Desc is a SQL keyword. It's bad practice to name a table column "Desc" or any other reserved SQL keywords. But if you want to stick with it, use backticks - `Desc`
 
Solution

AGx-07_162

Honorable
Sep 16, 2013
58
0
10,610
Thank you. I wasn't aware of that. I'm certainly no expert but unfortunately I'm stepping into an environment that existed before me. I should be able to get around it if I just do [Desc] instead.

Thanks again.
 

AGx-07_162

Honorable
Sep 16, 2013
58
0
10,610


I'll try to do that when I can. I was able to make it work by just putting [ ] around the column name.

INSERT INTO Employees (Title, FirstName, LastName, Company, Department, Mailstop, Extension, City, State, Bldg, Floor, DirList, Prefix, Notes, Phone, importID, importDate, [Desc])

SELECT Title, FirstName, LastName, Company, Department, Mailstop, Extension, City, State, Bldg, Floor, DirList, Prefix, Notes, Phone, importID, importDate, [Desc]

FROM dbo_Employees

 

ksham

Honorable
Mar 29, 2013
175
0
10,760
Agreed; it's not recommended. Also, based on that query, the DB design is optimal. Company and Department should be an association table with Department being its own table. Address should be its own thing too IMO.
 

AGx-07_162

Honorable
Sep 16, 2013
58
0
10,610


There are a lot of things wrong with the database. The design doesn't bother me as much as the inconsistency in the data. Almost everything is a Text(255) field and users just jam whatever they want into whatever field they want.