如何实现 OPENJSON 将 JSON 代码作为列中的文本。我如何在 AZURE SQL Dataware House 中的 table 列上使用 OPENJSON?
How to implement OPENJSON Having JSON code as a text inside a column. How do i use OPENJSON on the column of a table in AZURE SQL Dataware House?
例如:原始数据
| ORDER# | SUBORDER# | DISCOUNTS |
|------- |-----------| -------------------------------|
| 1 | 1-123 | '[{ discount:"1",amount:"1"}]' |
| 1 | 1-123 | '[{ discount:"2",amount:"2"}]' |
想要在折扣上应用 OPENJSON 并具有以下输出:
| ORDER# | SUBORDER# | discount | amount |
|------- |-----------| ---------|------------|
| 1 | 1-123 | 1 | 1 |
| 1 | 1-123 | 2 | 2 |
Msg 137, Level 15, State 2, Line 44
Must declare the scalar variable "@discounts".
我已经更新了我的答案,请在插入语句后添加;
。
- 创建 table 并插入两行:
create table dbo.test(
ORDER# varchar(255),
SUBORDER# varchar(255),
DISCOUNTS varchar(255)
);
insert into dbo.test values ('1','1-123','[{ "discount":"1","amount":"1"}]');
insert into dbo.test values ('1','1-123','[{ "discount":"2","amount":"2"}]');
- 然后我们可以使用下面的sql查询数据
select ORDER#,SUBORDER#,A.*
from dbo.test t
CROSS APPLY OPENJSON(t.DISCOUNTS)
WITH (
discount varchar(255),
amount varchar(255)
) A;
3.The结果如下:
例如:原始数据
| ORDER# | SUBORDER# | DISCOUNTS |
|------- |-----------| -------------------------------|
| 1 | 1-123 | '[{ discount:"1",amount:"1"}]' |
| 1 | 1-123 | '[{ discount:"2",amount:"2"}]' |
想要在折扣上应用 OPENJSON 并具有以下输出:
| ORDER# | SUBORDER# | discount | amount |
|------- |-----------| ---------|------------|
| 1 | 1-123 | 1 | 1 |
| 1 | 1-123 | 2 | 2 |
Msg 137, Level 15, State 2, Line 44
Must declare the scalar variable "@discounts".
我已经更新了我的答案,请在插入语句后添加;
。
- 创建 table 并插入两行:
create table dbo.test(
ORDER# varchar(255),
SUBORDER# varchar(255),
DISCOUNTS varchar(255)
);
insert into dbo.test values ('1','1-123','[{ "discount":"1","amount":"1"}]');
insert into dbo.test values ('1','1-123','[{ "discount":"2","amount":"2"}]');
- 然后我们可以使用下面的sql查询数据
select ORDER#,SUBORDER#,A.*
from dbo.test t
CROSS APPLY OPENJSON(t.DISCOUNTS)
WITH (
discount varchar(255),
amount varchar(255)
) A;
3.The结果如下: