简体   繁体   中英

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

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM