Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Error Code 1166. Incorrect column name

Tags:

sql

mysql

When I try to run a routine I get the following error:

Error Code: 1166. Incorrect column name 'School.`School Name` = case when School.`Web School Name` is null then School.`School Name` els'

I have double checked the column name and there is in fact a School.School Name. There are no leading or trailing spaces. Here is the beginning of the routine that the error is referring to:

CREATE temporary TABLE tt_Step1
SELECT DISTINCT State.Code, State.Name, 
School.`School Name` = case 
    when School.`Web School Name` is null then School.`School Name`
    else School.`Web School Name`
    end,
School.`School ID` 
-- Into tt_Step1
FROM State LEFT JOIN School ON State.Code = School.State 
Where (School.`School ID` <> ...

I recently converted this code from MSSQL to MySQL so there may be something I missed but I cannot find any errors. The original MSSQL query runs fine in SQL Server Management Studio but the converted version of it in MySQL does not.

like image 644
Travis P Avatar asked Mar 28 '12 20:03

Travis P


People also ask

What does error code 1166 mean?

Error Code: 1166. Incorrect column name 'School.`School Name` = case when School.`Web School Name` is null then School.`School Name` els' I have double checked the column name and there is in fact a School.School Name. There are no leading or trailing spaces. Here is the beginning of the routine that the error is referring to:

How to fix error 1366 in MySQL?

To conclude, the ERROR 1366: Incorrect string value happens when MySQL can’t insert the value you specified into the table because of incompatible encoding. You need to modify or remove characters that have 4-bytes UTF-8 encoding, or you can change the encoding and collation used by MySQL. Note that utf8 in MySQL always refers to utf8mb3.

Why can't I use escaped characters in MySQL names?

You have spaces at the end of the names (for id and username ). This seems to be causing the problem, even though the escaped names are used consistently (this is documented in the MySQL documentation ). This works: I would expect the escaped characters to work. However, spaces at the end of the names is a bad idea, so remove them.


2 Answers

In my case, it was caused by an unseen extra space after the column name. I wrote promotion-ids instead of promotion-id.

like image 110
Aminah Nuraini Avatar answered Sep 18 '22 08:09

Aminah Nuraini


Give this a shot:

CREATE temporary TABLE tt_Step1
SELECT DISTINCT State.Code, State.Name, 
coalesce(School.`Web School Name`, School.`School Name`),
School.`School ID` 
-- Into tt_Step1
FROM State LEFT JOIN School ON State.Code = School.State 
Where (School.`School ID` <> ...
like image 31
robertvoliva Avatar answered Sep 19 '22 08:09

robertvoliva