Friday, May 12, 2017

Simple SQL Trips and Tricks (Formatting)

I found simple SQL Tips and Tricks which helped me to make the changes quickly and analyze issues.


  • Always give the database name in the script. It should be at the top of any SQL script file.
USE [DBName]
GO
;
  • Use the [SchemaName]  and [TableName] in your SQL commands with square brackets ("[]") for fields, schema and table. For example
SELECT [Field1] , [Field2], [Field3] FROM  [dbo].[Table1]; 
 UPDATE [dbo].[Table1]     SET [Field1]  = 'NewValue'   WHERE [Field2] = 'Condition';

  • Give Tab space between each word and also align vertically as shown below:
 UPDATE [dbo].[Table1]    SET [Field1]  = 'NewValue'   WHERE [Field2] = 'Condition';
 UPDATE [dbo].[Tab2]       SET [Field2]  = 'New'            WHERE [F1]       = 'Condition2'; 
 UPDATE [dbo].[Tab3]       SET [Field5]  = 'Update Val' WHERE [Fld1]    = 'Condition2';  
Above simple alignment steps help us in changing values horizontally (using Shift + Alt and then up or down arrow).
I used Microsoft SQL Server Management Studio (SQL Server Management Solutions, SQL Server Management Studio Projects).