Quantcast
Viewing all articles
Browse latest Browse all 84

SQL SERVER – Shortcut to SELECT only 1 Row from Table

If you watch any SQL Server Developer, you will notice one particular task them doing every day frequently. It is they select the row from the table to see what are the various kinds of data it contains. Most of the tables are very big so it is always advisable to retrieve only a single row from the table. It is very cumbersome for developers to continuously write following code to retrieve a single row to see what the table contains.

SELECT TOP 1 * FROM TableName

I suggest you try to write above code and there is good chance that one has to look at the keyboard to type above code as it requires numbers. Even expert typist often has to look at the keyboard when they have to deal with Special Symbole (in our case *) and number (in our case 1) when typing subsequently. The counter argument is to not use above command but use SP_HELP or any other SP which describes the schema of the table. Well, this may be a good idea but we developer like to do things our own way. Just to be clear it is never a good idea to do any development on production server.

There is a shortcut how we can make our life easier if we use SQL Server Management Studio Shortcut. You can see following image where I am configuring SSMS shortcut.

Image may be NSFW.
Clik here to view.

Go to SSMS Menu >> Tools >> Options >> Environment >> Keyboard >> Query Shortcuts.

Now on the right side click on the preferred shortcut (in this case I am selecting CTRL + 3) and now in the column of the Stored Procedure type following statement

SELECT TOP 1 * FROM

Pay special attention that I have not mentioned the name of the table. The syntax is incomplete as we will SELECT the name of the table later on of SSMS Query Editor.

Once that is done, now go to SSMS and select any preferred table name. While the table name is selected type CTRL + 3 and you will notice that the query will run and will return a single row as a resultset. Now developer just has to select the table name and click on CTRL + 3 or your preferred shortcut key and you will be able to see a single row from your table.

Image may be NSFW.
Clik here to view.

This trick can be very helpful when a developer is debugging a long stored procedure with lots of a table name or while writing complex query.

Reference: Pinal Dave (http://blog.SQLAuthority.com)


Filed under: PostADay, SQL, SQL Authority, SQL Query, SQL Server, SQL Server Management Studio, SQL Tips and Tricks, T SQL, Technology Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.


Viewing all articles
Browse latest Browse all 84

Trending Articles