SQl选择特定的列,但使用*

我需要帮助,我的SQL Server select语句是:

select * from schematemplate.scanner

该表的列是:

id
Asset_Category
Asset_Classification
Brand
Model
Supplier
Color

除了Asset_CategoryAsset_Classification之外,我可以使用这个选择所有列:

Select id, brand, model, supplier, color 
from schematemplate.scanner

但我不想指定像上面的代码那样选择的列。

是否有可能SELECT * from schematemplate.scanner使用SELECT * from schematemplate.scanner并添加一个代码,如EXCEPT asset_category and asaset_classification


你可以动态地做到这一点,例如:

declare @s varchar(max) = 'select '

select @s=@s+name+',' from sys.columns
where object_id=object_id('schematemplate.scanner')
  and name not in ('asset_category','asset_classification')
order by column_id

set @s=substring(@s,1,len(@s)-1)+' from schematemplate.scanner'

exec(@s)

sqlfiddle


那些只有五列。 为什么不选择它?

无论如何,这里有一个你可能会采取的建议,

  • 创建一个视图和
  • 运行select就可以了,
  • CREATE VIEW viewScanner
    AS
    SELECT id, brand, model, supplier, color 
    FROM   schematemplate.scanner
    

    当你想选择记录时,

    SELECT * FROM viewScanner
    
    链接地址: http://www.djcxy.com/p/93855.html

    上一篇: SQl select specific column but with use of *

    下一篇: Paging in SQL Server problems