Search this blog

Tuesday, June 2, 2009

Purpose of GO Statement in SQL

GO

This statement is very simple, but it helps lot, also helps to do the repeated works.

I hope that everybody knows of GO statement in SQL.

GO sends the Signals the end of a batch of T-SQL statements to the SQL Server utilities or client tool. Through this, we can find that requested processes are done.

Also this helps to reduce deadlock or auto object locking. For example, when you send a Data request from front end Apps to SQL, and wait for response from SQL. If you use GO Statement in your SQL Query, it will signal to the requester that process has been done. So it will come out immediately, if you are not using GO, and then waiting for the response some moments.

* Now we will look into the other usage of GO Statement.

If you want repeat the same operation for N – number of times.

For Example,

GO N

Select * from Table1

Go 10

If you execute the above Query, it repeats the same process 10 times.

Also have a look at other similar example of same operation.

CREATE TABLE [dbo].[Table1](

[ID] [int] IDENTITY(1,1) NOT NULL,

[Col1] [varchar](50) NOT NULL

) ON [PRIMARY]

GO

Now I want to insert the same value 10 times, based on this scenario, we can use the following sort of query with GO statement

Insert into Table1(Col1) Values('Test Values')

GO 10

When You Execute this Query, Here go will act as Loop, and repeats the same process N – Number times. If you run this Query in SQL Server Management studio, you can see the Execution process as follows,

Beginning execution loop

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

(1 row(s) affected)

Batch execution completed 10 times.

If you check the data, you will get the output as follows

select * from Table1

ID Col1

-----------------------

1 Test Values

2 Test Values

3 Test Values

4 Test Values

5 Test Values

6 Test Values

7 Test Values

8 Test Values

9 Test Values

10 Test Values

No comments:

Post a Comment