How to select all the columns of a table except one column?

前端 未结 13 2450
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 05:43

How to select all the columns of a table except one column?

I have nearly 259 columns I cant mention 258 columns in SELECT statement.

Is there

13条回答
  •  被撕碎了的回忆
    2020-12-14 06:10

    You can get the column name details from sys.columns table

    Try the following query:

    SELECT * FROM SYS.COLUMNS 
    WHERE object_id = OBJECT_ID('dbo.TableName') 
    AND [Name] <> 'ColumnName'
    
    DECLARE @sql as VARCHAR(8000)
    SET @sql = 'SELECT '
    
    SELECT @sql += [Name] + ', ' FROM SYS.COLUMNS 
    WHERE object_id = OBJECT_ID('dbo.TableName') 
    AND [Name] <> 'ColumnName'
    
    SELECT @sql += ' FROM Dbo.TableName'
    
    EXEC(@sql)
    

提交回复
热议问题