SQL Listing all column names alphabetically

后端 未结 8 1873
天命终不由人
天命终不由人 2020-12-16 11:39

I know that

SELECT * FROM Table

will list all columns in the table, but I am interested in listing the columns in alphabetical order.

8条回答
  •  不思量自难忘°
    2020-12-16 12:20

    SQL-92 Standard specifies that when using SELECT * the columns are referenced in the ascending sequence of their ordinal position within the table. The relevant sections are 4.8 (columns) and 7.9 (query specification). I don't know of any vendor extensions to the Standard that would allow columns to be returned in any other order, probably because use of SELECT * is generally discouraged.

    You can use SQL DDL to ensure that columns' ordinal positions match the desired alphabetical order. However, this will only work in the way you want when referening a sinlge table in the FROM clause. If two tables are referenced, SELECT * will return the columns from the first table in ordinal position order followed by the second table's columns in ordinal position, so the complete resultset's columns may not be in alphabetical order.

提交回复
热议问题