How to select all the columns of a table except one column?

前端 未结 13 2398
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 05:43

How to select all the columns of a table except one column?

I have nearly 259 columns I cant mention 258 columns in SELECT statement.

Is there

相关标签:
13条回答
  • 2020-12-14 06:10

    You can get the column name details from sys.columns table

    Try the following query:

    SELECT * FROM SYS.COLUMNS 
    WHERE object_id = OBJECT_ID('dbo.TableName') 
    AND [Name] <> 'ColumnName'
    
    DECLARE @sql as VARCHAR(8000)
    SET @sql = 'SELECT '
    
    SELECT @sql += [Name] + ', ' FROM SYS.COLUMNS 
    WHERE object_id = OBJECT_ID('dbo.TableName') 
    AND [Name] <> 'ColumnName'
    
    SELECT @sql += ' FROM Dbo.TableName'
    
    EXEC(@sql)
    
    0 讨论(0)
  • 2020-12-14 06:11

    You can use this approach to get the data from all the columns except one:-

    1. Insert all the data into a temporary table
    2. Then drop the column which you dont want from the temporary table
    3. Fetch the data from the temporary table(This will not contain the data of the removed column)
    4. Drop the temporary table

    Something like this:

    SELECT * INTO #TemporaryTable FROM YourTableName
    
    ALTER TABLE #TemporaryTable DROP COLUMN Columnwhichyouwanttoremove
    
    SELECT * FROM #TemporaryTable 
    
    DROP TABLE #TemporaryTable 
    
    0 讨论(0)
  • 2020-12-14 06:11

    There are lot of options available , one of them is :

     CREATE TEMPORARY TABLE temp_tb SELECT * FROM orig_tb;
     ALTER TABLE temp_tb DROP col_x;
     SELECT * FROM temp_tb;
    

    Here the col_x is the column which u dont want to include in select statement.

    Take a look at this question : Select all columns except one in MySQL?

    0 讨论(0)
  • 2020-12-14 06:12

    In your case, expand columns of that database in the object explorer. Drag the columns in to the query area.

    And then just delete one or two columns which you don't want and then run it. I'm open to any suggestions easier than this.

    0 讨论(0)
  • 2020-12-14 06:13

    I just wanted to echo @Luann's comment as I use this approach always.

    Just right click on the table > Script table as > Select to > New Query window.

    You will see the select query. Just take out the column you want to exclude and you have your preferred select query.

    0 讨论(0)
  • 2020-12-14 06:16

    Create a view. Yes, in the view creation statement, you will have to list each...and...every...field...by...name.

    Once.

    Then just select * from viewname after that.

    0 讨论(0)
提交回复
热议问题