Technical Article

Keyboard shortcut to select top 1000

,

When testing a procedure for an error or creating an ad hoc query, the number one typed syntax is select top N * from table. I wanted to create a keyboard short-cut to help speed up this process and I came up with this. Once the stored procedure is deploy. Put a single tick on each side of a table, including schema and database, highlight the object and hit ctrl + 6 and SSMS will select top 1000 all from the table. To create a keyboard short cut please visit http://msdn.microsoft.com/en-us/library/ms174178(v=sql.105).aspx. Here is an example: 'Database.Schema.Table'

USE [database]
GO

/****** Object:  StoredProcedure [ACT].[ASDB_UTIL_Select_Top_1000_From_Table]    Script Date: 05/07/2015 08:19:59 ******/SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


/**************************************************************************************************
* DateAuthorPurpose
* 08/06/2014Tim HarmsCreated stored procedure to be used as a keyboard shortcut where
*you hightlight a fully qualified table, after putting a single ' 
*at the beginning and end of the fully qualified table and hit 
*the set keyboard shortcut and it will select the top 1000 *
*FROM the highlighted table.
*To set a keyboard shortcut goto Tools, Option, Environment, Keyboard
*and enter database.dbo.Select_Top_1000_From_Table under store procedure. 
*Create a keyboard accelerator for a stored procedure
*http://msdn.microsoft.com/en-us/library/ms174178(v=sql.105).aspx
*Test Example
*database.schema.tablename
*
*Prints SELECT TOP (1000) FROM database.schema.tablename
*'database.schema.tablename'

*
**************************************************************************************************/CREATE PROCEDURE [dbo].[UTIL_Select_Top_1000_From_Table]
(
@TableNameVARCHAR(100),
@DebugBIT = 0
)
AS

DECLARE
@sSQL VARCHAR(200)=''

IF OBJECT_ID(@TableName)IS NOT NULL 
SET@sSQL = 'SELECT TOP (1000)* FROM ' + @TableName + '(NOLOCK);'

IF OBJECT_ID(@TableName)IS NULL 
PRINT 'Table Not Found!'

IF @Debug = 1
      PRINT(@sSQL);
ELSE
      EXECUTE(@sSQL);


/**************************************************************************************************/ 

SET NOCOUNT OFF;

GO

Rate

2.2 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

2.2 (5)

You rated this post out of 5. Change rating