Monday, October 18, 2010

SQL SERVER: Problem with SELECT *

Its common practise to user SELECT * when we want to query data from a db table or underlying view or inserting data into another db table as below.

--Assumption: tableA , tableB exists
INSERT INTO tableB
SELECT * FROM tableA.
GO

The above statement works very well as long as both the db tables have the same structure.
But if the db structure for any of the above db changes, then the above query would fail.

Solution: Always use explicit column names in the SQL queries

INSERT INTO tableB(col1,col2,col3)
SELECT col1,col2,col3 FROM tableA
GO

Reference: Durga Palepu (http://blogwithdp.blogspot.com/)

No comments:

Post a Comment