Optimize your SQL Query – Replace Star (*) in Select Statement – MS SQL Server

Introduction

Sometime we may need almost all the columns from a table result. In this scenario we use Star(*) in our select statement i.e. as SELECT * FROM TableName. Using Star(*) in the select statement is BAD. The best approach is selecting the table with column names. In this article we will learn how quickly we can replace Star(*) with column names.

Replace Star(*) in SELECT Statement

It is always a best practice to select the required columns from the table.

Syntax
SELECT COLUMN1,COLUMN2, COLUMN3......COLUMN(N) FROM TableName
Example
SELECT [WorkId],
      [WorkItem],
      [WorkItemStatus]
FROM [ToDoApp].[ToDoList].[WorkItems]

If the table has less columns, then we can easily mention the column names, but what if the table has more columns to select?. There is a quick approach to replace star(*) with column names in SSMS. SSMS makes it very simple for us.

If we look into the object explorer, under the table name we have Columns folder. Just DRAG and DROP the Columns Folder to the query editor, right next to the SELECT clause Refer(Fig 3.).

Fig 1. Object Explorer with Columns in SSMS
Fig 2. After Drag and Drop – Columns Folder in Query Window

After the Drag and Drop, all the columns in the table will be present in the our SELECT Statement. This is how we can select all the columns in a table quickly.

Pic 3. Drag and Drop the Column Folder in SSMS

Conclusion

I assume you all found this Tip more helpful. Please feel free to share your feedbacks in the comment section.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: