How to exclude a column from SELECT query?

后端 未结 5 1275
借酒劲吻你
借酒劲吻你 2020-12-03 23:01

I have 100 columns in a table and I want to list 99 columns except a particular one.

How to exclude that columns name?

5条回答
  •  暖寄归人
    2020-12-03 23:46

    Much faster query solution! In bold text, add your table name and column name to be remove. You can add as many fields as you want to remove.

    DECLARE @query nvarchar(max)=(SELECT string_agg(COLUMN_NAME,',') FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='YOURTABLENAME'
    
    AND COLUMN_NAME NOT IN ('REMOVEDCOLOUMN',.........))
    
    SET @query ='select '+@query+' FROM YOURTABLENAME' EXEC sp_executesql @query;
    

提交回复
热议问题