Sql server 防止SQL";对于JSON“;避免造成重复

Sql server 防止SQL";对于JSON“;避免造成重复,sql-server,json,path,sql-server-2016,Sql Server,Json,Path,Sql Server 2016,我有一个主表部门。详细信息表Employees的外键指向Departments表。当使用PATH选项将简单连接查询返回为JSON时,它将列出部门的多行。然而,当使用AUTO选项时,它返回的是唯一的部门,但我失去了对模式的控制。如何使用PATH选项并仍然能够像自动选项一样返回唯一的部门。代码如下: Declare @Departments as table (DeptName varchar(100), Location varchar(100) ) insert @Departments se

我有一个主表部门。详细信息表Employees的外键指向Departments表。当使用PATH选项将简单连接查询返回为JSON时,它将列出部门的多行。然而,当使用AUTO选项时,它返回的是唯一的部门,但我失去了对模式的控制。如何使用PATH选项并仍然能够像自动选项一样返回唯一的部门。代码如下:

Declare @Departments as table (DeptName varchar(100), Location varchar(100) )
insert @Departments 
select 'IT', 'San Francisco'
union
select 'Sales', 'Miami'
union
select 'Finance', 'NYC'

Declare @Employees as table (DeptName varchar(100) , EmployeeName varchar(100), Salary Decimal(7,2))
insert @Employees 
select 'Finance', 'Ponzi', 1000
union
select 'Finance', 'Madoff', 10000
union
select 'IT' , 'Bill', 20000
union
select 'IT', 'Steve', 100

select D.DeptName [Department.Name], D.Location [Department.Location], E.EmployeeName [Employee.Name], E.Salary [Employee.Salary]
from
    @Departments D
    left join @Employees E on E.DeptName = D.DeptName
for JSON Auto
自动模式返回以下结果。请注意,每个部门只出现一次:

[{
        "Department.Name": "Finance",
        "Department.Location": "NYC",
        "E": [{
                "Employee.Name": "Madoff",
                "Employee.Salary": 10000.00
            }, {
                "Employee.Name": "Ponzi",
                "Employee.Salary": 1000.00
            }
        ]
    }, {
        "Department.Name": "IT",
        "Department.Location": "San Francisco",
        "E": [{
                "Employee.Name": "Bill",
                "Employee.Salary": 20000.00
            }, {
                "Employee.Name": "Steve",
                "Employee.Salary": 100.00
            }
        ]
    }, {
        "Department.Name": "Sales",
        "Department.Location": "Miami",
        "E": [{}
        ]
    }
]
PATH选项返回以下结果。请注意,每个部门都多次出现:

[{
        "Department": {
            "Name": "Finance",
            "Location": "NYC"
        },
        "Employee": {
            "Name": "Madoff",
            "Salary": 10000.00
        }
    }, {
        "Department": {
            "Name": "Finance",
            "Location": "NYC"
        },
        "Employee": {
            "Name": "Ponzi",
            "Salary": 1000.00
        }
    }, {
        "Department": {
            "Name": "IT",
            "Location": "San Francisco"
        },
        "Employee": {
            "Name": "Bill",
            "Salary": 20000.00
        }
    }, {
        "Department": {
            "Name": "IT",
            "Location": "San Francisco"
        },
        "Employee": {
            "Name": "Steve",
            "Salary": 100.00
        }
    }, {
        "Department": {
            "Name": "Sales",
            "Location": "Miami"
        }
    }
]

使用路径模式时,如何防止部门多次出现?

没关系。必须修改源查询,首先对Employees表进行JSonification,然后与Departments表交叉应用,最后再次对整个查询进行JSonification

查询:

Declare @Departments as table (DeptName varchar(100), Location varchar(100) )
insert @Departments 
select 'IT', 'San Francisco'
union
select 'Sales', 'Miami'
union
select 'Finance', 'NYC'

Declare @Employees as table (DeptName varchar(100) , EmployeeName varchar(100), Salary Decimal(7,2))
insert @Employees 
select 'Finance', 'Ponzi', 1000
union
select 'Finance', 'Madoff', 10000
union
select 'IT' , 'Bill', 20000
union
select 'IT', 'Steve', 100

select D.DeptName [Department.Name], D.Location [Department.Location], jsonEmployees.Employees
from
    @Departments D
    cross apply (
        select EmployeeName [Employee.Name], Salary [Employee.Salary]
        from @Employees Employee 
        where Employee.DeptName = D.DeptName 
        For JSON path
    ) JsonEmployees(Employees)

for JSON path
结果:

[{
        "Department": {
            "Name": "Finance",
            "Location": "NYC"
        },
        "Employees": [{
                "Employee": {
                    "Name": "Madoff",
                    "Salary": 10000.00
                }
            }, {
                "Employee": {
                    "Name": "Ponzi",
                    "Salary": 1000.00
                }
            }
        ]
    }, {
        "Department": {
            "Name": "IT",
            "Location": "San Francisco"
        },
        "Employees": [{
                "Employee": {
                    "Name": "Bill",
                    "Salary": 20000.00
                }
            }, {
                "Employee": {
                    "Name": "Steve",
                    "Salary": 100.00
                }
            }
        ]
    }, {
        "Department": {
            "Name": "Sales",
            "Location": "Miami"
        }
    }
]