Can we optionally use `JSON Path` in SQL Server 2016

70 Views Asked by At

I want to return JSON or a Table result set (normal) based on a parameter that I am passing to a SQL Server stored procedure.

SELECT  
    emp.Name,
    emp.EmpID 
FROM 
    Employee emp 
ORDER BY 
    emp.Name       
FOR JSON Path;

This will return one column with a string JSON result.

I want to make this optional based on a parameter. Basically I want to re-use the stored procedure for JSON as well as normal result.

1

There are 1 best solutions below

0
sandeep rawat On

Sql key words or objects name ie column name or table name can not be used directly as parameter,

You have and alternate write dynamic SQL in side SP.

Note: It will have performance hit as it is dynamic SQL.

suggestion: Write two sp .