包含空值

FOR JSON 子句忽略單元格中的 NULL 值。如果要為包含 NULL 值的單元格生成 key:null 對,請在查詢中新增 INCLUDE_NULL_VALUES 選項:

SELECT top 3 object_id, name, type, principal_id
FROM sys.objects
FOR JSON PATH, INCLUDE_NULL_VALUES

將生成 principal_id 列中的 NULL 值:

[
  {"object_id":3,"name":"sysrscols","type":"S ","principal_id":null},
  {"object_id":5,"name":"sysrowsets","type":"S ","principal_id":null},
  {"object_id":6,"name":"sysclones","type":"S ","principal_id":null}
]