Saturday, November 28, 2015

GO keyword in SQL

While generating the SQL scripts, you might have often seen the GO keyword. What it is ? Let's see what exactly it is.

At first look, it might seem to be a keyword of T-SQL like any other keyword like SELECT, DISTINCT, ORDER BY etc. However, it is not. Yes, it is not a T-SQL keyword. As per MSDN, this keyword is:

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.
SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

This means, it is not considered as a T-SQL keyword and is more of a command to SQL Server. This keyword basically act as a command for SQL to execute a group or batch of statements which are written before the GO keyword is encountered. 

An important point to note here is that the GO keyword limits the scope of the variables to the batch of statements on which this command is used. This means if we try to use a declared variable after the GO keyword is executed, it will throw an error i.e. the keyword will not be available after it. See the following sample code:



Here, the (1 row(s) affected) message refers to the first SELECT statement which returns the value. However, the second SELECT returns error as the variable cannot be accessed after the GO command is executed.

Happy Querying...!!!

Saturday, November 21, 2015

Generate UML Sequence diagram from Visual Studio

Yes it is correct, you can generate Sequence diagrams for your code, from within the Visual Studio and that too automatically.No need to create it your self. Let's see how we can do this.

Let's create a new sample project. Let's add a static classes Class1 and static method TestMethod1 in this class. Now let's create a test method on Default.aspx and call it on the Page_Load event. This function, in turn, will call TestMethod1 of Class1So our code will look like the following:



Now, right click on the Page_Load event, and select the option "Generate Sequence Diagram". 



This will open up a window to define the settings to be used for generating this diagram. You can set what level of code it should consider for generating the diagram, whether to include the code within the entire solution or current project only etc.

Also, you can specify whether to save the diagram in current project or not. So we increase the call depth to 2 and keep rest of the settings as it is. 



Click "Ok" and it will generate the sequence diagram for the code we have.  


So the sequence diagram is ready. Just increase the "Maximum Call Depth" property and create the diagram to understand the code in easily. Happy Coding...!!!

Friday, November 13, 2015

MS SQL Database backup using SQL Command

Use the following SQL command in order to generate the back-up of the MS SQL database.


BACKUP DATABASE Database_Name TO DISK 'Location_For_Backup'

For example, generate back-up of database SampleDB, we can use the following database;


BACKUP DATABASE SampleDB TO DISK 'D:\SampleDB.BAK' 


Happy Querying...!!!