在SQL Server 2016中,如何使用示例代码返回“ PageSize”,“ Total”和“ NumberOfPages”值?我没有数据的列标题。
我想看:
代码:
DECLARE @jsonData NVARCHAR(MAX)
SET @jsonData = N'{
"PageSize": 500,
"PageNumber": 2,
"Total": 994,
"Items": [
{
"Id": 23004801096147,
"StatusName": "Open",
"CompanyName": "Microsoft",
"JobTitle": "Project Manager"
},
{
"Id": 23004801096147,
"StatusName": "Closed",
"CompanyName": "Salesforce",
"JobTitle": "Marketing Manager"
}
],
"NumberOfPages": 1024
}'
-尝试1
SELECT
JSON_VALUE(fh.value, '$.PageSize') AS [PageSize],
JSON_VALUE(fh.value, '$.Total') AS [Total],
JSON_VALUE(fh.value, '$.NumberOfPages') AS [NumberOfPages]
FROM OPENJSON(@jsonData, '$.FileHeader') as fh
-尝试2
SELECT 'PageSize' as 'PageSize', 'Total' as 'Total', 'NumberOfPages' as
'NumberOfPages'
FROM OPENJSON(@jsonData, '$.FileHeader') as fh
-尝试3
SELECT 'PageSize' as 'PageSize', 'Total' as 'Total', 'NumberOfPages' as
'NumberOfPages'
FROM OPENJSON(@jsonData) as fh
-尝试4
SELECT 'PageSize' as 'PageSize', 'Total' as 'Total', 'NumberOfPages' as
'NumberOfPages'
FROM OPENJSON(@jsonData)
答案 0 :(得分:0)
SQL Server 2016+ JSON: https://docs.microsoft.com/en-us/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017
以下是使用您的数据的示例:
DECLARE @jsonData NVARCHAR(MAX)
SET @jsonData = N'{
"PageSize": 500,
"PageNumber": 2,
"Total": 994,
"Items": [
{
"Id": 23004801096147,
"StatusName": "Open",
"CompanyName": "Microsoft",
"JobTitle": "Project Manager"
},
{
"Id": 23004801096147,
"StatusName": "Closed",
"CompanyName": "Salesforce",
"JobTitle": "Marketing Manager"
}
],
"NumberOfPages": 1024
}'
--Using OPENJSON and "WITH"
SELECT
[PageSize]
, [PageNumber]
, [Total]
, [NumberOfPages]
FROM OPENJSON(@jsonData)
WITH(
[PageSize] BIGINT '$.PageSize'
,[PageNumber] BIGINT '$.PageNumber'
,[Total] BIGINT '$.Total'
,[NumberOfPages] BIGINT '$.NumberOfPages'
)
--This is how you could accomplish the same with JSON_VALUE
SELECT
JSON_VALUE(@jsonData, '$.PageSize') AS [PageSize],
JSON_VALUE(@jsonData, '$.PageNumber') AS [PageNumber],
JSON_VALUE(@jsonData, '$.Total') AS [Total],
JSON_VALUE(@jsonData, '$.NumberOfPages') AS [NumberOfPages]