Viewing Individual Entry / Main
  ::   April 27, 2004

Back To Basics: An SQL Gotcha
I took a call from our help desk last night around midnight. One of our users in Asia had called in reporting an error in one of our applications. I checked our error logs, and sure enough there was an error - an SQL error: SQL0117 - Statement contains wrong number of values. This server happens to be an old ColdFusion server still running ODBC. We're in the process of upgrading t to MX, but that's another story. Anyhow, at first, I was puzzled by the error as we hadn't made any changes to the code in quite some time. Then it hit me. Earlier in the day, we added a new column to one of the database tables used by the application in order to facilitate the upgrade. Harmless enough, right? Wrong.

Turns out the application in question had an SQL insert that looked something like this:

INSER INTO myTable VALUES('#foo#', goo, '#boo#')

Now granted this code was written about 6 years ago, but the developer who wrote it took a shortcut that came back to bite us. Instead of explicitly declaring the fields to be inserted, he or she simply expressed the values to insert. This only works when the number of values exactly matches the number of fields in the database table. In my case, the extra field we added caused the application to break. Now granted this is something that could have been caught in testing/qa, but we didn't have that luxury in this particular instance.

The moral of the story here is that in order to future proof your application, it's best to always declare the columns you intend to insert:

INSER INTO myTable (foo, goo, boo) VALUES('#foo#', goo, '#boo#')



Calendar
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 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            

Recommended Reading
Nickel and Dimed: On (Not) Getting By in America

Archives by Subject
Announcements (28)
ColdFusion (61)
Database (12)
Dreamweaver (1)
Flash (2)
Flash Remoting (2)
Flex (3)
General Development (2)
Homesite + (1)
Mach-II (3)
MAX (9)
Mozilla (2)
Music (1)
Politics (1)
Portals (2)
Travel (2)

Search

Links
CFLib.org
Raymond Camden's Blog
Christian Cantrell's Blog
Sean Corfield's Blog
Nathan Dintenfass' Blog
Todd Rafferty's Blog
Steve Rittler's Blog
Cameron Childress's Blog

Credits
Based on blog.cfc by Raymond Camden

XML Feed