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

前端 未结 13 2396
被撕碎了的回忆
被撕碎了的回忆 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 05:54

    Try the following query:

    DECLARE @Temp NVARCHAR(MAX); 
    DECLARE @SQL NVARCHAR(MAX);
    
    SET @Temp = '';
    SELECT @Temp = @Temp + COLUMN_NAME + ', ' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='Person' AND COLUMN_NAME NOT IN ('Id')  
    
    SET @SQL = 'SELECT ' + SUBSTRING(@Temp, 0, LEN(@Temp)) +' FROM [Person]';
    EXECUTE SP_EXECUTESQL @SQL;
    
    0 讨论(0)
  • 2020-12-14 05:55

    Without creating new table you can do simply (e.g with mysqli):

    1. get all columns
    2. loop through all columns and remove wich you want
    3. make your query

    $r = mysqli_query('SELECT column_name FROM information_schema.columns WHERE table_name = table_to_query');
    
    $c = count($r); while($c--) if($r[$c]['column_name'] != 'column_to_remove_from_query') $a[] = $r[$c]['column_name']; else unset($r[$c]);
    
    $r = mysqli_query('SELECT ' . implode(',', $a) . ' FROM table_to_query');
    
    0 讨论(0)
  • 2020-12-14 06:02

    Only one way to achieve this giving column name. There is no other method found. You must have to list all column name

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

    If you are using DataGrip you can do the following:

    1. Enter your SELECT statement SELECT * FROM <your_table>;
    2. Put your cursor over * and press Alt+Enter
    3. You will get pop up menu with Expand column list option
    4. Click on it and it will convert * with full list of columns
    5. Now you can remove columns that you don't need

    Here is a link for an example on how to do it.

    0 讨论(0)
  • 2020-12-14 06:03
    1. Generate your select query with any good autocomplete editor of your database.
    2. copy and paste your select query in a reserved method (function) and return its ResultSet to your main program or do your stuff in the same method.
    3. call this method each time whenever you require
    0 讨论(0)
  • 2020-12-14 06:08

    You can retrieve the list of column name by simple query and then remove those column by apply where query like this.

    SELECT * FROM (
    SELECT COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = N'TableName'
    ) AS allColumns
    WHERE allColumns.COLUMN_NAME NOT IN ('unwantedCol1', 'unwantedCol2')
    
    0 讨论(0)
提交回复
热议问题