0

I need to get result of sql query in JSON format via MSSQL 2014. I have wrote this code:

SELECT bc.Id, bc.CityName, bc.ProvinceId FROM BusCity AS bc WHERE bc.Country=2 FOR JSON AUTO

But I am getting an error:

Incorrect syntax near 'JSON'.

Is this possible? How to do this?

hmahdavi
  • 2,250
  • 3
  • 38
  • 90
  • 1
    `FOR JSON` is not available with 2014. Here is an answer with an alternative approach https://stackoverflow.com/questions/6818441/sql-server-select-to-json-function – Ric Oct 23 '17 at 10:37

1 Answers1

1

You running 2016 or higher?

FOR JSON was introduced then.

Type

SELECT @@Version

To find out what version you're running.

https://learn.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
  • Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64) Jun 17 2016 19:14:09 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 6.3 (Build 9600: ) (Hypervisor) – hmahdavi Oct 23 '17 at 10:40
  • Yup. It's not available on your version of SQL. – Paul Alan Taylor Oct 23 '17 at 10:41