作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
鉴于以下脚本:
DECLARE @table1 TABLE (t1num int NOT NULL);
DECLARE @table2 TABLE (t2num int NOT NULL);
DECLARE @table3 TABLE (t3num int NOT NULL);
INSERT INTO @table1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table2 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table3 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
SELECT
t2num,
(
SELECT t1num AS [t1.num]
FROM @table1
FOR JSON PATH
) AS t1s
FROM @table2 [t2]
t1s
的输出列将如下所示:
[{"t1":{"num":1}},{"t1":{"num":2}},{"t1":{"num":3}},{"t1":{"num":4}},{"t1":{"num":5}},{"t1":{"num":6}},{"t1":{"num":7}},{"t1":{"num":8}},{"t1":{"num":9}},{"t1":{"num":10}}]
[{"t1_num":1},{"t1_num":2},{"t1_num":3},{"t1_num":4},{"t1_num":5},{"t1_num":6},{"t1_num":7},{"t1_num":8},{"t1_num":9},{"t1_num":10}]
Msg 13603, Level 16, State 1, Line 10
Property 't1..num' cannot be generated in JSON output due to invalid character in the column name or alias. Column name or alias that contains '..', starts or ends with '.' is not allowed in query that has FOR JSON clause.
[{"t1\\":{"num":1}},{"t1\\":{"num":2}},{"t1\\":{"num":3}},{"t1\\":{"num":4}},{"t1\\":{"num":5}},{"t1\\":{"num":6}},{"t1\\":{"num":7}},{"t1\\":{"num":8}},{"t1\\":{"num":9}},{"t1\\":{"num":10}}]
最佳答案
它并不优雅,但这是我思考的地方...我刚刚添加了一个 replace(...,'_','.')
DECLARE @table1 TABLE (t1num int NOT NULL);
DECLARE @table2 TABLE (t2num int NOT NULL);
DECLARE @table3 TABLE (t3num int NOT NULL);
INSERT INTO @table1 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table2 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
INSERT INTO @table3 VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);
SELECT
t2num,
replace((
SELECT t1num AS [t1_num]
FROM @table1
FOR JSON PATH
),'_','.') AS t1s
FROM @table2 [t2]
t2num t1s
1 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
2 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
3 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
4 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
5 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
6 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
7 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
8 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
9 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
10 [{"t1.num":1},{"t1.num":2},{"t1.num":3},{"t1.num":4},{"t1.num":5},{"t1.num":6},{"t1.num":7},{"t1.num":8},{"t1.num":9},{"t1.num":10}]
关于sql-server - 如何在带有 FOR JSON PATH 的列名中使用点/句点而不创建嵌套对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43870687/
我是一名优秀的程序员,十分优秀!