Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
近年來前端開發技術大量使用 JSON 的格式在交換資料, 以往我們想要由程式將 JSON 格式的資料放到 SQL Server 中操作, 會需要自行外掛 SQL CLR 才可以較方便操作. 在 SQL Server 2016 已原生支援 JSON 的格式, 且有提供相關的函式來操作.
來看一下範例:
1. 在查詢的語法最後 加上 FOR JSON AUTO 就會回傳 JSON 格式的資料.
2. 使用函數操作 JSON 的資料
DECLARE @json NVARCHAR(4000)
SET @json = N'{
"info":{
"type":1,
"address":{
"town":"Bristol",
"county":"Avon",
"country":"England"
},
"tags":["Sport", "Water polo"]
},
"type":"Basic"
}'
SELECT * FROM OPENJSON(@json, N'lax $.info')
更多資訊:
JSON Data (SQL Server) https://msdn.microsoft.com/en-us/library/dn921897.aspx
Enjoy. Jacky