Free Consulting Free Consulting - 3 months ago 28
SQL Question

Sudden "Must declare the scalar variable" when running example snippets

I was exploring new Transact SQL JSON capabilities by running various example snippets in my regular SQL Server 2014 Management Studio against clean install of SQL Server 2016 Express. I've started to understand how it works, but suddenly got bitten by


Must declare the scalar variable


For example, this loading snippet worked before:

SELECT @json = BulkColumn
FROM OPENROWSET (BULK 'C:\JSON\Books\book.json', SINGLE_CLOB) as j


but now it spews:


Msg 137, Level 15, State 1, Line 1

Must declare the scalar variable "@json".


There are much more snippets from MSDN which either ceased to work or don't work when pasted to query window.

Why this happened? Did I break something? Is it related to older Management Studio and newer database engine?

Answer

You need to Declare variables,those may be just examples to show how it works..

Declare @json nvarchar(max)
SELECT @json = BulkColumn
 FROM OPENROWSET (BULK 'C:\JSON\Books\book.json', SINGLE_CLOB) as j