How to convert data to json format in SQL Server 2008?

后端 未结 3 603
抹茶落季
抹茶落季 2021-01-12 14:26

I am using SQL Server 2008 and want to convert table data into json format. Can I convert it directly through firing query?

相关标签:
3条回答
  • 2021-01-12 14:43

    The above solutions seem unnecessarily complicated; starting with 2008, SQL Server supports the following syntax (answer from DBA StackExchange)

    SELECT * FROM dbo.x FOR JSON AUTO;
    

    The above query returns a single JSON-formatted column that looks like this (assuming dbo.x contains columns col1~col4of corresponding types):

    [{"col1":"val1","col2":"val2","col3":5,"col4":"2019-02-11"}]
    

    More details/options here: https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server

    0 讨论(0)
  • 2021-01-12 14:45

    I have created a stored procedure that can take your table and output JSON. You can find it at

    • GitHub - SQLTableOrViewToJSON

    Once you run the stored procedure, you can output your table by making a call like the following:

    EXEC SQLTableOrViewToJSON 'MyTable', 'C:\WhereIStowMyJSON\'
    
    0 讨论(0)
  • 2021-01-12 15:03

    Built in support for formatting query results is added in SQL Server 2016 and it will be available in Azure Database. In older versions you would need to use CLR or some heavy TSQL like:

    • Producing JSON Documents from SQL Server queries via TSQL
    0 讨论(0)
提交回复
热议问题