SQL Server中的JSON支持
1. 生成有效JSON的注意事项
在生成JSON时,ROOT选项和WITHOUT_ARRAY_WRAPPER选项不能一起使用。例如,若尝试同时使用这两个选项,会出现如下错误:
Msg 13620, Level 16, State 1, Line 113
ROOT option and WITHOUT_ARRAY_WRAPPER option cannot be used together in FOR JSON. Remove one of these options.
所以在使用时要谨慎,留意生成的JSON可能无效。
2. 数据类型转换
JSON和SQL Server的数据类型不同,当从关系数据生成JSON文本时,会进行数据类型转换。FOR JSON子句使用以下映射将SQL Server数据类型转换为JSON输出中的JSON类型:
| SQL Server数据类型 | JSON数据类型 |
| — | — |
| Char, Varchar, Nchar, NVarchar, Text, Ntext, Date, DateTime, DateTime2, DateTimeOffset, Time, UniqueIdentifier, Smallmoney, Money, XML, HierarchyId, Sql_Variant | string |
| Tinyint, Smallint, Int, Bigint, Decimal, Float, Numeric | number |
| Bit | true or
超级会员免费看
订阅专栏 解锁全文
890

被折叠的 条评论
为什么被折叠?



