SQL exclude a column using SELECT * [except columnA] FROM tableA?

后端 未结 30 3391
花落未央
花落未央 2020-11-21 23:15

We all know that to select all columns from a table, we can use

SELECT * FROM tableA

Is there a way to exclude column(s) from a table witho

30条回答
  •  生来不讨喜
    2020-11-21 23:43

    I know this question is old, but I hope this can still be helpful.The answer is inspired by a discuss from SQL Server Forums. You can make this a stored procedure. It can also be modified to add more than one except fields.

    DECLARE @SQL NVARCHAR(MAX)
    SELECT @SQL = COALESCE(@SQL + ', ', ' ' ) + name from sys.columns where name not in ('colName1','colName2') and object_id = (Select id from sysobjects where name = 'tblName')
    SELECT @SQL = 'SELECT ' + @SQL + ' FROM ' + 'tblName'
    EXEC sp_executesql  @SQL
    

提交回复
热议问题