Question of the month April 2017 – Why does GO throw error in Dynamic SQL and not in static SQL?

Execute the following code

select 10 as number1
GO
select 11 as number2

It produces following two result sets


number1
-----------
10

(1 row(s) affected)

number2
-----------
11

(1 row(s) affected)

Now the same code executed as Dynamic SQL,

EXEC
(
'
select 10 as number1
GO
select 11 as number2
'
)

throws the following error.

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'GO'.

The Question is “Why does GO throw error in Dynamic SQL but not in static SQL?”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s