Posts

Showing posts from December, 2009

SSMS handy short cut keys

Image
In Microsoft SQL Server Management Studio, its handy to have some short cut keys to do common tasks. One handy one I always use is to " select top 100 * from " as I like to highlight a table name and press CTRL+4 to get the top 100 rows instantly. To set this up, go to Tools->Options->Keyboard. And type "select top 100 * from " without the quotes and making sure you add a space after the from. Very important to leave a space at the end. Highlight a table and press Ctrl+4 and the top 100 rows will be shown. SSMS will run your command and append the highlighted text. If your table is called USERS, it would run "select top 100 * from USERS". If you forgot to leave a space at the end, it would try and run "select top 100 * fromUSERS" which won't work.

table-values functions. Make sure you write them as inline.

Here is a multi-line statement (slow): CREATE FUNCTION dbo.fnSomeReport ( @someParam INT , @field_company_id INT = NULL ) RETURNS @result TABLE ( [field_company_name] VARCHAR (255), [engineer] VARCHAR (100), [sku] VARCHAR (30) ) AS BEGIN INSERT INTO @result ( [field_company_name], [engineer], [sku] ) SELECT field_company_name, engineer, sku FROM ... WHERE ID > someParam AND (@field_company_id IS NULL OR field_company_id = @field_company_id) RETURN END GO Now as an inline statement (fast) CREATE FUNCTION dbo.fnSomeReport ( @someParam INT , @field_company_id INT = NULL ) RETURNS TABLE AS RETURN ( SELECT field_company_name, engineer, sku FROM ... WHERE ID > someParam AND (@field_company_id IS NULL OR field_company_id = @field_company_id) ) GO Right then whats going on and what the hell is all the fuss about anyway? The query optimizer expands the function as if it was a macro, and generates the plan as if you had provided the expanded query . Thus, there is no performance cost ...