Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / SQL

The Evil That is Select *

4.90/5 (36 votes)
10 Aug 2011CPOL2 min read 96.4K  
Just say no to SELECT * in your SQL
There I was working on a web page, when I decided that I needed to add some new columns to an existing table. So I did. Forty-five minutes later, or system admin calls, and says a bunch of people are having problems wiht the web sight (and a related desktop app). A co-worker asked if anyone had added new columns to any of the tables, and I raised my hand.

It seems that SOMEONE had used SELECT * in one or more SQL queries into the affected table, and the act of adding a column completely hosed up the code as a result of my adding the new columns.

Nobody here really knows where the offending code is, or even if it might be in a stored procedure, but the fastest way out of the mess was to remove the columns from the existing table, and create a new table to hold them.

This is PRECISELY how spaghetti code is propagated throughout a project, along with apparently pointless tables in the database.

For the record, we don't know exactly why we're having problems because we don't have time to find out (because we wouldn't fix it if we did find out). We suspect someone wrote an INSERT or UPDATE statement that tries to do a SELECT * to move some column data around between tables. I can think of only one word to describe this, and the first syllable is "cluster".

The tip:

DO NOT USE SELECT * IN YOUR SQL QUERIES - FOR ANY REASON.

EDIT ================

For those of you who felt the need to tell me that the word "cluster" has two syllables - I already know that, and I have no idea where you people got the idea that I couldn't count. That particular statement is part of my endearing yet sardonic wit.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)