SQL Server table to json

后端 未结 3 712
说谎
说谎 2020-12-15 09:12

I am looking to pull some columns (Col1 and 2) of a table and put in JSON format and also write some hardcoded JSON in each node, like this.

{ \"col1

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 09:31

    Use from the magic words For JSON

    example:

     SELECT name, surname  
     FROM emp  
     FOR JSON AUTO  
    

    result:

    [{"name": "John"}, {"name": "Jane", "surname": "Doe"}]
    

    more info in:

    https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-2017&viewFallbackFrom=sql-server-2014

提交回复
热议问题