tags:

views:

523

answers:

6
+7  Q: 

SELECT * EXCEPT

Is there any RDBMS that implements something like SELECT * EXCEPT? What I'm after is getting all of the fields except a specific TEXT/BLOB field, and I'd like to just select everything else.

Almost daily I complain to my coworkers that someone should implement this... It's terribly annoying that it doesn't exist.

Edit: I understand everyone's concern for SELECT *. I know the risks associated with SELECT *. However, this, at least in my situation, would not be used for any Production level code, or even Development level code; strictly for debugging, when I need to see all of the values easily.

As I've stated in some of the comments, where I work is strictly a commandline shop, doing everything over ssh. This makes it difficult to use any gui tools (external connections to the database aren't allowed), etc etc.

Thanks for the suggestions though.

+7  A: 

Stay away from SELECT *, you are setting yourself for trouble. Always specify exactly which columns you want. It is in fact quite refreshing that the "feature" you are asking for doesn't exist.

Otávio Décio
Luckily, our tables almost never change. I'm thinking of this specifically for debugging problems, where I need all of the other fields, except for the BLOB.
gms8994
in that specific case, I agree that it'd be nice to have.
Michael Haren
The operating word here is *almost*. Also, any good sql tool will allow you to click and choose what you want without polluting the language with such contraption.
Otávio Décio
@gms8994, in the case of debugging, most enterprise RDBMSs will automatically script out the select statement for you, which includes all columns. Then just remove your 'yucky' column and you're good to go.
Kon
ocdecio/fallen888: that implies that you're using some gui tool, which we do not. strictly commandline here.
gms8994
+3  A: 

I believe the rationale for it not existing is that the author of a query should (for performance sake) only request what they're going to look at/need (and therefore know what columns to specify) -- if someone adds a couple more blobs in the future, you'd be pulling back potentially large fields you're not going to need.

Rowland Shaw
+1  A: 

As others are saying: SELECT * is a bad idea.

Some reasons:

  1. Get only what you need (anything more is a waste)
  2. Indexing (index what you need and you can get it more quickly. If you ask for a bunch of non-indexed columns, too, your query plans will suffer.
Michael Haren
+10  A: 

Create a view on the table which doesn't include the blob columns

Paul Dixon
+1: this is quite reasonable - and no SELECT * allowed here as well.
Otávio Décio
Requires modifying the view if the source table changes. If you're going to use SELECT * on that, might as well just select the columns that you really want to begin with.
Brian Knoblauch
It saves you from typing those columns over and over though. For a developer's use in debugging it's not a bad idea.
Tom H.
I have cut and paste on my fancy new computer. :-)
Brian Knoblauch
That's great, if you always want to keep that select statement (and any others like it) sitting around. I have enough clutter on my desktop. It's also more work to open a file, copy, and paste then it is to do a simple select,
Tom H.
+5  A: 

As others have said, it is not a good idea to do this in a query because it is prone to issues when someone changes the table structure in the future. However, there is a way to do this... and I can't believe I'm actually suggesting this, but in the spirit of answering the ACTUAL question...

Do it with dynamic SQL... this does all the columns except the "description" column. You could easily turn this into a function or stored proc.

declare @sql varchar(8000),
    @table_id int,
    @col_id int

set @sql = 'select '

select @table_id = id from sysobjects where name = 'MY_Table'

select @col_id = min(colid) from syscolumns where id = @table_id and name <> 'description'
while (@col_id is not null) begin
    select @sql = @sql + name from syscolumns where id = @table_id and colid = @col_id

    select @col_id = min(colid) from syscolumns where id = @table_id and colid > @col_id and name <> 'description'
    if (@col_id is not null) set @sql = @sql + ','
    print @sql
end

set @sql = @sql + ' from MY_table'

exec @sql
Jasmine
And in the spirit of answering the actual question, you win the prize.
gms8994
I can actually think of several reasons why you might need to do this without being insane. Plus it was an interesting question, regardless of the issues, it was just fun to figure out :)
Jasmine
A: 
declare @sql nvarchar(max)
        @table char(10)
set @sql = 'select '
set @table = 'table_name'

SELECT @sql = @sql + '[' + COLUMN_NAME + '],'
FROM   INFORMATION_SCHEMA.Columns
WHERE  TABLE_NAME = @table
   and COLUMN_NAME <> 'omitted_column_name'
SET    @sql = substring(@sql,1,len(@sql)-1) + ' from ' + @table

EXEC (@sql);
kzh