SQL Server Listing all column names alphabetically

SQL Server Listing all column names alphabetically

In this tutorial i am going to explain about SQL Server Listing all column names alphabetically. In my previous tutorial i have explained about

Implementation :

Now we will see the implementation of SQL Server Listing all column names alphabetically.The default behaviour of sql server whenever you run the Select * from TableName is it will display the columns in the created order. To list or select the sql columns in alphabetical order there is no pre defined function or procedure available in sql server.

Let’s begin with a demo table. I have created a demo table named customer with below script.




Now i have inserted some sample data in the table using below script.

Now if i run the query to select all the columns and rows i am getting the below output.

Step 1 of SQL Server Listing all column names alphabetically

Now if you look into the output it is displayed the columns in the order we created. Now if i want the columns to display in alphabetical order i can manually specify the column name in the select statement like below.

Step 2 of SQL Server Listing all column names alphabetically

If you see the select query i am passing all the columns in the alphabetical order to get this result. This is cumbersome if i have more number of columns in the table. So  i have created a procedure which will accept the table name as input and give the select query result with columns in alphabetical order as a output. Below is the script of stored procedure i have created.

Output:

Now if you run the program you can see the output of SQL Server Listing all column names alphabetically. If you execute SelectColumnsAlphabeticalOrder procedure with the table name as parameter then you will get the alphabetical ordered columns in the output. Refer below screen.

Output of SQL Server Listing all column names alphabetically

If you check the messages window you can see the printed generated script.

Output 2 of SQL Server Listing all column names alphabetically

 Conclusion:

Hope you learned SQL Server Listing all column names alphabetically. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!