tsql从无服务器SQL池中的JSON数据创建提取键和值

wj8zmpe1  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(114)

如何在Azure Synapse Analytics中使用tsql使用无服务器SQL池创建外部表:
我在名为“companyDetail”的表中有一个“employeeInfo”列。这是创建“companyDetail”表的查询:

CREATE EXTERNAL TABLE companyDetail
(
  companyName varchar(100),
  employeeInfo varchar(2048) 
) 
WITH
(
    LOCATION = '/all_parquet_files/*.parquet',
    DATA_SOURCE = parquet_datasource,
    FILE_FORMAT = parquet
)

companyDetail表数据:

companyName| employeeInfo
----------------------------
ABC        | {name: Ramesh, age:32 years}
ABC        | {name: Mohan, experience:2 years}
DEF        | {name: Dinesh, age:39, experience:5 years}
HIJ        |
DEF        | {name: Mohit}

我必须从这个数据创建一个外部表'employee',它应该有这样的结果:

companyName| employeeKey | employeeValue
------------------------------------------
ABC        | name        | Ramesh
ABC        | age         | 32 years
ABC        | name        | Mohan
ABC        | experience  | 2 years
ABC        | name        | Dinesh
ABC        | age         | 39
ABC        | experience  | 2 years
HIJ        |             |
DEF        | name        | Mohit
elcex8rz

elcex8rz1#

一种方法是将数据从外部表导入到临时表或表变量中,然后对导入的数据使用OPENJSON函数将数据翻转到列中。例如:

SELECT companyName, employeeInfo
INTO #tempTable
FROM companyDetail
-- WHERE ...

SELECT companyName, [Key] as employeeKey, [Value] as employeeValue
FROM #tempTable
CROSS APPLY OPENJSON(employeeInfo)

参见:OPENJSON

相关问题