我们可以从表格中选择比所有列少的一列

时间:2018-07-10 09:51:08

标签: sql-server plsql

请帮助我: 我在表中有100列,我想选择99列,但我不想写99列。有什么办法。

预先感谢, Nishant Khanna

4 个答案:

答案 0 :(得分:1)

在MSSQL中:

declare @schema varchar(128)
declare @table varchar(128)
declare @exceptedColumn varchar(128)
declare @sql nvarchar(max)

set @schema = 'dbo'
set @table = 'MyTable'
set @exceptedColumn = 'DontWantThat'

set @sql = 'SELECT ' + (
select STUFF((select ',' + '[' + columns.name + ']' from sys.columns
join sys.tables on tables.object_id = columns.object_id
join sys.schemas on schemas.schema_id = tables.schema_id
where schemas.name = @schema and tables.name = @table and columns.name <> @exceptedColumn
for xml path('')), 1, 1, '')
) + ' FROM [' + @schema + '].[' + @table + ']'

exec sp_executesql @sql

答案 1 :(得分:0)

你可以试试吗?

select * into [#my_table] from [my_table];
alter table [#my_table] drop column [my_column];
select * from [#my_table];

答案 2 :(得分:0)

  Replace the **TableName** with yours and Column name that you don't want in your query.

这肯定会起作用

     SELECT left(replace((
                        SELECT COLUMN_NAME + ','
                        FROM INFORMATION_SCHEMA.COLUMNS
                        WHERE TABLE_NAME = '**TableName**'
                        FOR XML path('')
                        ), '**Column_Name_That_You_Don't want in your query**' + ',', ''), (
                    len(replace((
                                SELECT COLUMN_NAME + ','
                                FROM INFORMATION_SCHEMA.COLUMNS
                                WHERE TABLE_NAME = '**TableName**'
                                FOR XML path('')
                                ), '**Column_Name_That_You_Don't want in your query**' + ',', ''))
                    ) - 1)

答案 3 :(得分:0)

创建一个包含99列的视图,并在代码中而不是实际的表中使用它。