Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 When select record from table,why not use '*'?

Author  Topic 

debradeng
Starting Member

46 Posts

Posted - 2006-10-24 : 00:15:27
I am wondering when select record from table,can I use SELECT * FROM... instead of listing all the colunms? I have created some SPs based on given dynamic SQL queries,and none of them use '*'.They list all the colunms' name. what advantages can get by using this way?

pootle_flump

1064 Posts

Posted - 2006-10-24 : 03:40:46
http://weblogs.sqlteam.com/brettk/archive/2004/04/22/1272.aspx

HTH
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-10-24 : 05:31:08
1) SQL has to convert "*" to the column list every time it executes it. Takes time and probably prevents the query plan being cached.

2) Additional columns may be added to the table in the future, which aren't needed by the logic in the current application (of course the column may be being added for this part of your application logic, but then stuff elsewhere doesn't need it - and if it does it needs reviewing / re-coding / etc.

I've made good money from consultancy after clients added TEXT columns to tables and called me in to figure out why everything had slowed hugely. Their telesales people were using the new TEXT columns for their comments, which I'm sure made a big improvement to their jobs, but sadly everything else was doing a SELECT * and sucking huge amounts of data from the server - that the application never used.

There are lots of easy ways to get a decent list of the column names - so no excuse for using SELECT * from a "shorter to type" argument!

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-10-24 : 11:35:19
Here, put this in your pocket for future use


USE Northwind
GO

DECLARE @TABLE_NAME sysname
SET @TABLE_NAME = 'Orders'

SELECT SQL
FROM (
SELECT TABLE_NAME, 1 AS SQL_GROUP, 1 AS ROW_ORDER
, ' SELECT '+COLUMN_NAME AS SQL
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TABLE_NAME
AND ORDINAL_POSITION=1
UNION ALL
SELECT TABLE_NAME, 2 AS SQL_GROUP, ORDINAL_POSITION AS ROW_ORDER
, ' , '+COLUMN_NAME AS SQL
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TABLE_NAME
AND ORDINAL_POSITION<>1
UNION ALL
SELECT TABLE_NAME, 3 AS SQL_GROUP, ORDINAL_POSITION AS ROW_ORDER
, ' FROM '+TABLE_NAME AS SQL
FROM INFORMATION_SCHEMA.Columns
WHERE TABLE_NAME = @TABLE_NAME
AND ORDINAL_POSITION=1) AS XXX
ORDER BY TABLE_NAME, SQL_GROUP, ROW_ORDER







Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

debradeng
Starting Member

46 Posts

Posted - 2006-10-25 : 00:10:26
Thank you everyone,very helpful!!!
Go to Top of Page
   

- Advertisement -