2017-07-03 117 views
0

我有一个主表部门。详细信息表Employees中的外键指向Departments表。当使用PATH选项将简单联接查询作为JSON返回时,它将列出Department的多个行。而使用AUTO选项时,它将返回独特的部门,但是我放弃了对模式的控制。我如何使用PATH选项,并仍然能够像AUTO选项一样返回唯一的部门。以下是代码:防止SQL“FOR JSON”导致重复

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" 
     } 
    } 
] 

如何在使用PATH模式时防止多个部门出现?

回答

0

没关系。必须先通过JSON化Employees表来修改源查询,然后用Departments表交叉应用,然后在最后再次对所有东西进行JSON化。

查询:

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" 
     } 
    } 
]