Saturday, October 22, 2005

SQL Server 2005 and GO operator ...

In this article we would see an interesting feature which was introduced in SQL Server 2005. Do you know that you can execute a T-SQL batch "any number" of times just by providing an integer value along with GO.

As we all know we used to end a command or a script block by typing a command terminator. i.e, GO.

GO :: Signals the end of a batch of Transact-SQL statements to the MS SQL Server utilities.

Extract from BOL:

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 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. In SQL Server 2005, we can follow the command terminator (GO) with an integer value to specify how many times the command should be run. Cool isn't it?

Test it for yourself:

Print getDate()
Go 10

If you execute the above script it would print the current date 10 times.

No comments: