0

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.

wp78de
  • 18,207
  • 7
  • 43
  • 71
Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
  • 2
    Won't checking the parameter and duplicating the body work ..some thing like ..if @parameter is null begin normal result end if @paramerter=something begin json result end – TheGameiswar Oct 08 '17 at 09:53

1 Answers1

0

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 .

sandeep rawat
  • 4,797
  • 1
  • 18
  • 36