[Webinar] Learn how to a build a cloud-first strategyRegister Now


SELECT * FROM TABLE    minus certain columns

Posted on 2006-05-19
Medium Priority
Last Modified: 2012-08-13

I've got a dynamic evolving Table
I't can get other columnnames, other amount of column names all the time.
1 column is a certain asset of the table and that is the ID

Now I need a SQL Query that gives me ALL the Columns of that Table WITHOUT getting the ID column and whitout using the columnnames

Can somebody help me with this? this is urgent.
Question by:John Claes
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16718926
> WITHOUT getting the ID column and whitout using the columnnames
i dont think it is possible .. , using column names its ok
LVL 11

Expert Comment

ID: 16718947
SELECT [name]
FROM [database_name].[dbo].[syscolumns]
where [id] = (select [id] from [database_name].[dbo].[sysobjects] WHERE name = 'table_name')
and [name] != 'ID'
LVL 11

Accepted Solution

deroby earned 750 total points
ID: 16719649
You'll need two steps then I suppose, one that queries the meta-data, and one that does the actual select.

Something like this I guess :

DECLARE @sql varchar(8000)

SELECT @sql = ''
SELECT @sql = @sql + '[' + col.[name] + '], '
  FROM syscolumns col
  JOIN sysobjects obj
    ON obj.[id] = col.[id]
   AND obj.[name] = 'YourTableName'
 WHERE col.[name] NOT IN ('list', 'of', 'not', 'wanted', 'columns', 'here')
 ORDER BY colid

IF @sql = ''
        SELECT 'Nothing to report'
        SELECT @sql = RTrim(@sql)
        SELECT @sql = Left(@sql, Len(@sql) - 1)

        SELECT @sql = 'SELECT ' + @sql + ' FROM YourTableName' -- add where/order by etc as needed

        -- test : PRINT @sql
        EXEC (@sql)

LVL 10

Author Comment

by:John Claes
ID: 16720115

i've made a stored procedure of it.
I'ts a good one. i thought something like that but I couldn't combine the sys Query with the normal Query

With Deroby's answer I now can do this Dynamicaly.

Thx Alot.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question