SQL exclude a column using SELECT * [except columnA] FROM tableA?

后端 未结 30 2554
花落未央
花落未央 2020-11-21 23:15

We all know that to select all columns from a table, we can use

SELECT * FROM tableA

Is there a way to exclude column(s) from a table witho

相关标签:
30条回答
  • 2020-11-21 23:53

    Like the others have said there is no way to do this, but if you're using Sql Server a trick that I use is to change the output to comma separated, then do

    select top 1 * from table
    

    and cut the whole list of columns from the output window. Then you can choose which columns you want without having to type them all in.

    0 讨论(0)
  • 2020-11-21 23:55

    Basically, you cannot do what you would like - but you can get the right tools to help you out making things a bit easier.

    If you look at Red-Gate's SQL Prompt, you can type "SELECT * FROM MyTable", and then move the cursor back after the "*", and hit <TAB> to expand the list of fields, and remove those few fields you don't need.

    It's not a perfect solution - but a darn good one! :-) Too bad MS SQL Server Management Studio's Intellisense still isn't intelligent enough to offer this feature.......

    Marc

    0 讨论(0)
  • 2020-11-21 23:57

    You can try it this way:

    /* Get the data into a temp table */
    SELECT * INTO #TempTable
    FROM YourTable
    /* Drop the columns that are not needed */
    ALTER TABLE #TempTable
    DROP COLUMN ColumnToDrop
    /* Get results and drop temp table */
    SELECT * FROM #TempTable
    DROP TABLE #TempTable
    
    0 讨论(0)
  • 2020-11-21 23:57

    If you want to exclude a sensitive case column like the password for example, I do this to hide the value :

    SELECT * , "" as password FROM tableName;

    0 讨论(0)
  • 2020-11-21 23:58

    You can get SQL Complete from devart.com, which not just expands the * wildcard just like SQL Prompt from Red Gate does (as described in cairnz's answer), but also provides a column picker drop down with checkboxes in which you can check all the columns that you want in the select list and they will be inserted automatically for you (and if you then uncheck a column it will be automatically removed from the select list).

    0 讨论(0)
  • 2020-11-21 23:59

    The automated way to do this in SQL (SQL Server) is:

    declare @cols varchar(max), @query varchar(max);
    SELECT  @cols = STUFF
        (
            ( 
                SELECT DISTINCT '], [' + name
                FROM sys.columns
                where object_id = (
                    select top 1 object_id from sys.objects
                    where name = 'MyTable'
                )
                and name not in ('ColumnIDontWant1', 'ColumnIDontWant2')
                FOR XML PATH('')
            ), 1, 2, ''
        ) + ']';
    
    SELECT @query = 'select ' + @cols + ' from MyTable';  
    EXEC (@query);
    
    0 讨论(0)
提交回复
热议问题