Click to See Complete Forum and Search --> : How to exclude columns from a SQL result set?


monocongo
12-26-2006, 03:38 PM
I have a table which contains about 20 columns, and I want my result set to include all of them except two. Rather than include all the columns that I want in the SQL statement I would rather just specify the two that I want excluded. How would I do that?

Thanks in advance for any assistance!


--James

russell
12-26-2006, 03:44 PM
dont select *

select the columns u want:

SELECT name, address, city, state, zip
FROM Customers
NOT

SELECT * FROM Customers

monocongo
12-26-2006, 03:56 PM
Thanks for your response.

Do I understand you correctly in that you mean that there's no way to do what I want to do, and I'm forced to specify exactly which columns I want in my result set?

If I have a table with 20 columns and I want 18 of them then I would need to specify all 18 columns that I want, rather than somehow specifying the 2 columns that I don't want?

For example if I have a table with 6 columns and I only want columns 1-4 then I want to do something likeselect * from my_table exclude column_5,column_6 rather than something likeselect column_1, column_2, column_3, column_4 from my_table
Is there nothing like this available in SQL?


--James

NogDog
12-26-2006, 04:31 PM
...
Is there nothing like this available in SQL?...
Nope.

chazzy
12-26-2006, 05:13 PM
Nope.

Not true!

What DBMS are you using? Using stored procedures has this as one of the many benefits.

Edit: To clarify, excluding a column isn't necessarily built in, but it is possible to give in to a stored procedure a list of columns you don't want. (Though I do have to wonder why you think this will be any more efficient)

russell
12-26-2006, 06:14 PM
well, as much as i know this sounds like i'm getting on my "high horse" usually, the right way to do it is to specify the columns you WANT.

there are lots of reasons for this, but the main one is specifying ones u DONT want immediately breaks when someone modifies the schema.

SELECT * is a bad practice in general. should always specify what u r trying to retrieve.

chazzy
12-26-2006, 07:23 PM
i agree w/ russell 100%, i just want to point out that a stored procedure that is specific to a certain table/query can be written to return all data except the columns specified.