Tag Archives: Sql

SLEEP Command in Sql Server

In Sql Server to PAUSE OR SLEEP OR WAIT the execution of the script for specified period of time say 10 hours or 10 minutes or 10 seconds or 10 millisecond etc we can use the WAITFOR DELAY command.

SYNTAX
waitfor-delay-in-syntax

Let us understand WAITFOR DELAY command with extensive list of examples:

Example 1: WAIT or SLEEP for 10 MilliSeconds

To wait for 10 MilliSeconds, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '00:00:00.010'

RESULT:
wait-for-10-milliseconds-in-sql-server

Example 2: WAIT or SLEEP for 10 Seconds

To wait for 10 Seconds, we can write the WAITFOR DELAY statement like below

 
WAITFOR DELAY '00:00:10.000'

RESULT:
wait-for-10-seconds-in-sql-server

Example 3: WAIT or SLEEP for 10 Minutes

To wait for 10 Minutes, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '00:10:00.000'

RESULT:
wait-for-10-minutes-in-sql-server

Example 4: WAIT or SLEEP for 10 Hours

To wait for 10 Hours, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '10:00:00.000'

Example 5: WAIT or SLEEP for 90 Seconds or 1 Minute and 30 Seconds

To wait for 90 Seconds or 1 Minute and 30 Seconds, we can write the WAITFOR DELAY statement like below:

WAITFOR DELAY '00:01:30.000'

RESULT:
wait-for-1-minute-and-30-seconds-in-sql-server

Example 6: Passing Variable for the WAITFOR DELAY statement

We can pass a local variable to the WAITFOR DELAY control flow statement as shown in the below example

DECLARE @WaitForTime AS VARCHAR(12) = '00:00:10.000'
WAITFOR DELAY @WaitForTime

RESULT:
passing-local-variable-as-a-parameter-for-the-waitfor-delay-statement

Sql Server Error: A fatal scripting error occurred. Incorrect syntax was encountered while parsing GO

ERROR SCENARIO

We can get exception like this if we try to execute the following statement where T-SQL statement like SELECT is on the Same line as GO Statement

GO SELECT 'Basavaraj Biradar'

RESULT:

A fatal scripting error occurred.
Incorrect syntax was encountered while parsing GO.

WHY THIS EXCEPTION?

GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio, sqlcmd and osql utilities. These utilities send all statements after the previous GO statement and before the current GO statement as one Batch to the Sql Server engine for execution. These utilities mandates that a Transact-SQL statement cannot be on the same line as a GO command.

WHAT IS THE SOLUTION?

To resolve this issue we can move the Transact SQL Statement here the SELECT statement to the next line from the one on the GO statement line as shown in the below script:

GO 
SELECT 'Basavaraj Biradar'

RESULT:
transact-sql-statement-shouldnt-be-on-the-same-line-as-go-statement

GO Statement in Sql Server

GO statement is used as a Batch separator in Sql Server. Batch is nothing but one or more Sql Server statements sent to the Sql Server engine as one set of statements.

GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio (i.e. SSMS), SQLCMD and OSQL utilities. These utilities send all statements after the previous GO statement and before the current GO statement as one Batch to the Sql Server engine for execution. So, it means everything in that batch is local to that batch. In other words any variables declared in the current batch will not be visible in the next batch (i.e. variables declared before the GO statement are not accessible after the GO statement).

GO Statement can also be used to execute batch of T-Sql statement multiple times. Let us understand GO statement with extensive list of examples:

Example 1: GO Statement the Batch Separator example

DECLARE @Name NVARCHAR(50) = 'Basavaraj Biradar'
SELECT @Name AS 'Name'
GO
DECLARE @Name NVARCHAR(50) = 'Shreeganesh Biradar'
SELECT @Name AS 'Name'

RESULT:
sql-go-statement-batch-separator

Even though here we see that the same variable @Name is declared twice, but it is still working. Reason is between these two variable declarations we have a batch separator GO statement. Variables declared before the GO statement are not accessible after the GO statement. Basically SSMS sends the first batch (i.e. Batch 1) of statements to the SQL Engine first, once its execution is over it sends the second batch of statements (i.e. Batch 2) after the GO statement to the SQL Engine for execution.

Example 2: Execute batch of T-Sql statements multiple times in SSMS

GO statement also has an integer optional parameter, this parameter value signals Sql Server to execute the batch of T-Sql Statement prior to the GO statement to be executed for the specified number of times. Let us understand this with following example:

PRINT 'Hello'
GO 5

RESULT:
sql-go-to-execute-batch-of-t-sql-statements-multiple-times

Example 3: T-Sql statements shouldn’t be on the same line as that of the GO Statement

GO SELECT 'Basavaraj Biradar'

RESULT:

A fatal scripting error occurred.
Incorrect syntax was encountered while parsing GO.

To avoid this issue, we can re-write the above example as below:

GO 
SELECT 'Basavaraj Biradar'

RESULT:
transact-sql-statement-shouldnt-be-on-the-same-line-as-go-statement

Example 4: Comment can be on the same line as GO statement

We can write comment on the same line as that of the GO statement

GO  --Comment can be on the same line as GO

RESULT:
sql-go-comment-can-be-on-the-same-line-as-go-statement

Example 5: GO statement can’t be part of the definition of the Stored Procedure, Function, View etc

We can’t add a GO statement in the definition of the Stored Procedure/Function/View etc

CREATE PROCEDURE GOStatementDemo
AS
BEGIN
	SELECT 'Basavaraj Biradar'
	GO	
	SELECT 'Shreeganesh Biradar'
END

RESULT:

Msg 102, Level 15, State 1, Procedure GOStatementDemo, Line 4 [Batch Start Line 0]
Incorrect syntax near ‘Basavaraj Biradar’.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ‘END’.