Back
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 without specifying all the columns?
SELECT * [except columnA] FROM tableA
The only way that I know is to manually specify all the columns and exclude the unwanted column. This is really time consuming so I'm looking for ways to save time and effort on this, as well as future maintenance should the table has more/less columns.
Thanks!
It’s recommendable to do it in this way:
SELECT * INTO #Temp_Table/* Get the data into a temp1 table(Here temp1 is a temporary table which is used to store the temporary data) */FROM Table /* Drop those columns which are not needed */ALTER TABLE #Temp_TableDROP COLUMN ColumnToDrop /* Get the results and drop temporary table */SELECT * FROM #Temp_TableDROP TABLE #Temp_Table
SELECT * INTO #Temp_Table
/* Get the data into a temp1 table(Here temp1 is a temporary table which is used to store the temporary data) */
FROM Table /* Drop those columns which are not needed */
ALTER TABLE #Temp_Table
DROP COLUMN ColumnToDrop /* Get the results and drop temporary table */
SELECT * FROM #Temp_Table
DROP TABLE #Temp_Table
SQL does not have an instruction to exclude a column such as SELECT * EXCEPT columnA FROM tableA but rather requires columns you will include in your SELECT clause and exclude all those unwanted columns.
Example
SELECT col1, col2, col3
FROM tableX;
31k questions
32.8k answers
501 comments
693 users