Working With Bad SQL Column Names (Even If It's Your Fault)

It must be SQL day at cfsilence. This is really obvious stuff, but even supreme coders like myself make mistakes now and then (and if you really think I believe that I am a supreme coder, I have some land in Florida I'd like to sell you). I just pulled an Excel file into my SQL Server 2000 database at work to use as a temp table to update an existing table with a new 'rank' for 2007. Well, it just so happened that the rank column was named '2007_mw_rank' - not the best column name, but it was there so I went with it since it was only a temp table. Apparently column names in SQL Server can not begin with a number (as I said, I normally wouldn't have done it). So when I went to query the table with the following query:

select *
FROM temp_2007_priority
WHERE 2007_mw_rank IS NULL

I received the following error:

Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '_mw_rank'.

Like all poorly named columns, surrounding the column name with [] was a temp fix:

select *
FROM temp_2007_priority
WHERE [2007_mw_rank] IS NULL

But the best solution would have been to rename the column so that it did not begin with a number at all!

Comments
It also works for column names with spaces and reserverd words.
# Posted By Jorrit | 12/5/06 3:12 AM
Note that you can escape column names in other databases, but the syntax varies from database to database.

Access uses brackets like SQL Server: [my column name]
MySQL uses ticks: `my column name`
Oracle uses quotes: "my column name"
PostGreSQL uses quotes: "my column name"
# Posted By Steve Bryant | 12/5/06 9:57 AM
Thanks for your information, i was working with a qeury with numbers as table names, but got wrong results so this helps alot.. and i will need to rename then :)

Regards, Aislin
# Posted By hypotheekrente | 5/19/08 11:42 AM

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn slidesix sql

Recent Comments

More CF+Java: Compiling Classes And Persisting Objects
Getburl said: I have been attempting to get Db4o working in my CF application and I have not succeeded. I would lo... [More]

Thoughts On Ajax Frameworks And ColdFusion/Adobe
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

Extending Ext With Ext Extensions
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

CF Needs An Open Source Contact List Importer
Kay Smoljak said: Heh, the fact that sites DO it doesn't mean they SHOULD. To us it's ok, but to a non-tech-savvy user... [More]

A Few Project Updates
Helena said: Now punctually what is the situation ? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com