将SQL Server中的表转换为JSON字符串以便迁移到DocumentDB

时间:2017-07-19 17:38:32

标签: sql sql-server json azure-cosmosdb

我在SQL Server中有一个名为DimCompany的表,如下所示:

+----+---------+--------+
| id | Company | Budget |
+----+---------+--------+
|  1 | abc     |    111 |
|  2 | def     |    444 |
+----+---------+--------+

我想将此表格转换为json文件,如下所示:

{
"DimCompany":{
    "id":1,
    "companydetails": [{
            "columnid": "1",
            "columnfieldname": "Company",
            "columnfieldvalue: "abc"
            }
            {
            "columnid": "2",
            "columnfieldname": "Budget",
            "columnfieldvalue: "111"
            }]
    }
},
{
"DimCompany":{
    "id":2,
    "companydetails": [{
            "columnid": "1",
            "columnfieldname": "Company",
            "columnfieldvalue: "def"
            }
            {
            "columnid": "2",
            "columnfieldname": "Budget",
            "columnfieldvalue: "444"
            }]
    }
}

其中columnid是sys.columns中针对列字段名称的值。我尝试通过取消对表的连接并在fieldname上连接sys.objects.name = DimCompany的sys.columns并将其放在视图中,然后在视图上查询以获取json输出以便迁移到DocumentDB中。

但是我不想使用unpivot而只是直接形成一个查询来获得所需的输出。

我只是好奇这是否可以在SQL服务器或任何其他工具中使用。

1 个答案:

答案 0 :(得分:1)

不使用UNPIVOT并自行执行以下SQL:

if object_id(N'dbo.DimCompany') is not null drop table dbo.DimCompany;
create table dbo.DimCompany (
    id int not null identity(1,1),
    Company nvarchar(50) not null,
    Budget float not null
);
insert dbo.DimCompany (Company, Budget) values
    ('abc', 111),
    ('def', 444);
go
select  id as 'DimCompany.id',
    (
        select columnid=cast(sc.column_id as nvarchar), columnfieldname, columnfieldvalue
        from (
            select  N'Company', Company from dbo.DimCompany DC2 where DC2.id = DC1.id
            union
            select  N'Budget', cast(Budget as nvarchar) from dbo.DimCompany DC2 where DC2.id = DC1.id
        ) keyValues (columnfieldname, columnfieldvalue)
        join sys.columns sc on sc.object_id=object_id(N'dbo.DimCompany') and sc.name=columnfieldname
        for json path
    ) as 'DimCompany.companydetails'    
from    dbo.DimCompany DC1
for json path, without_array_wrapper;

根据您的示例生成以下JSON:

{
    "DimCompany": {
        "id": 1,
        "companydetails": [
            {
                "columnid": "2",
                "columnfieldname": "Company",
                "columnfieldvalue": "abc"
            },
            {
                "columnid": "3",
                "columnfieldname": "Budget",
                "columnfieldvalue": "111"
            }
        ]
    }
},
{
    "DimCompany": {
        "id": 2,
        "companydetails": [
            {
                "columnid": "2",
                "columnfieldname": "Company",
                "columnfieldvalue": "def"
            },
            {
                "columnid": "3",
                "columnfieldname": "Budget",
                "columnfieldvalue": "444"
            }
        ]
    }
}

注意事项:

  • sys.columns列的columnid 1值从dbo.DimCompany.id开始。如果需要,请在强制转换之前减去1
  • 根据您的示例,使用without_array_wrapper会删除周围的[]字符,但实际上并不是有效的JSON。
  • 我怀疑这对于具有大量列的表是否可以扩展。