2

How to get all column names without one column in sql table in SQL Server.

I have a temp table. It has columns:

ID, Status, Code, Name, Location, Address, Title, Category, Line, date, time, UserName

I want all data without the id column

I want an alternative to this SQL code for this

SELECT Status, Code, Name, Location, Address, Title, Category, Line, date, time, UserName 
FROM TEMP
3
  • You cannot do this; if you want only some columns from a table, you must list them all. No way around this. Commented Nov 13, 2013 at 6:45
  • You can do this only if you are building dynamic SQL statement and using sp_executesql. Commented Nov 13, 2013 at 6:47
  • 1
    +1 for balancing inappropriate negative votes Commented Nov 13, 2013 at 6:56

3 Answers 3

3

Please try below query to select all column data without a column. Variable @ColList gives the column names except column ID:

DECLARE @ColList nvarchar(4000), @SQLStatment nvarchar(4000),@myval nvarchar(30)
SET @ColList = ''

select @ColList = @ColList + Name + ' , ' from syscolumns where id = object_id('TableName') AND Name != 'ID'
SELECT @SQLStatment = 'SELECT ' + Substring(@ColList,1,len(@ColList)-1) + ' From TableName'

EXEC(@SQLStatment)
Sign up to request clarification or add additional context in comments.

Comments

0

Unfortunately there is no "SELECT Everything except some columns" in SQL. You have to list out the ones you need.

If this is a temp table I guess you could try to drop the ID column before selecting the result. This is not appropriate if you need it again though...

ALTER TABLE Temp DROP COLUMN Id

Then

SELECT * FROM Temp

Comments

0

That is not possible.

You can't define wildcards in column names or select all but some. You have to name the ones you want to select or use * to select all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.