简体   繁体   English

如何在SQL Server的sql表中获取没有一列的所有列名

[英]How to get all column names without one column in sql table in SQL Server

How to get all column names without one column in sql table in SQL Server. 如何在SQL Server的sql表中获取没有一列的所有列名。

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 我想要所有没有id列的数据

I want an alternative to this SQL code for this 我想要这个SQL代码的替代品

SELECT Status, Code, Name, Location, Address, Title, Category, Line, date, time, UserName 
FROM TEMP

Please try below query to select all column data without a column. 请尝试在下面的查询中选择所有没有列的列数据。 Variable @ColList gives the column names except column ID : 变量@ColList给出列名,但列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)

Unfortunately there is no "SELECT Everything except some columns" in SQL. 不幸的是,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. 如果这是一个临时表,我想您可以在选择结果之前尝试删除ID列。 This is not appropriate if you need it again though... 如果您再次需要它,这是不合适的。

ALTER TABLE Temp DROP COLUMN Id

Then

SELECT * FROM Temp

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. 您必须命名要选择的名称,或使用*来全选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM