0

我有以下 3 个表格和示例数据:

员工、地址、州

/****** Object:  Table [AzureSearch].[Address]    Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[Address](
    [EmployeeId] [int] NULL,
    [Address] [nvarchar](60) NULL,
    [City] [nvarchar](30) NULL
) ON [PRIMARY]
GO
/****** Object:  Table [AzureSearch].[Employee]    Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[Employee](
    [EmployeeId] [int] NOT NULL,
    [FirstName] [nvarchar](200) NOT NULL,
    [LastName] [nvarchar](200) NOT NULL
) ON [PRIMARY]
GO
/****** Object:  Table [AzureSearch].[States]    Script Date: 11/2/2021 2:16:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [AzureSearch].[States](
    [City] [varchar](50) NULL,
    [StateName] [varchar](50) NULL
) ON [PRIMARY]
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (1, N'4350 Minute Dr.', N'Newport Hills')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (2, N'7559 Worth Ct.', N'Renton')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (3, N'2137 Birchwood Dr', N'Redmond')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (4, N'5678 Lakeview Blvd.', N'Minneapolis')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (5, N'9435 Breck Court', N'Bellevue')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (6, N'5670 Bel Air Dr.', N'Renton')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (7, N'1873 Lion Circle', N'Bothell')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (7, N'3858 Vista Diablo', N'Issaquah')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (8, N'4912 La Vuelta', N'Bothell')
GO
INSERT [AzureSearch].[Address] ([EmployeeId], [Address], [City]) VALUES (8, N'4039 Elkwood Dr.', N'Ballard')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (1, N'Ken', N'Sánchez')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (2, N'Terri', N'Duffy')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (3, N'Roberto', N'Tamburello')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (4, N'Rob', N'Walters')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (5, N'Gail', N'Erickson')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (6, N'Jossef', N'Goldberg')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (7, N'Osarumwense', N'Agbonile')
GO
INSERT [AzureSearch].[Employee] ([EmployeeId], [FirstName], [LastName]) VALUES (8, N'Karl', N'Xie')
GO
INSERT [AzureSearch].[States] ([City], [StateName]) VALUES (N'Bothell', N'Washington')
GO
INSERT [AzureSearch].[States] ([City], [StateName]) VALUES (N'Ballard', N'Utah')
GO

当我运行以下查询时,我在一个 JSON 对象中获取 JSON 详细信息

SELECT e.EmployeeId
        ,(
            SELECT a.Address
                ,a.City
            --  ,s.StateName AS StateName
            FROM AzureSearch.[Address] AS a
            --  ,AzureSearch.States AS s
            WHERE a.EmployeeId = e.employeeId
            --  AND a.City = s.City
            FOR json auto
            ) AS AddressDetails
FROM azuresearch.Employee AS e
 WHERE e.EmployeeId = 8

AddressDetails 列中的结果:

[
    {
     "Address": "4912 La Vuelta",
     "City": "Bothell"
    },
    {
        "Address": "4039 Elkwood Dr.",
        "City": "Ballard"
    }
]

但是,如果您取消注释上述 SQL 查询中的注释行以包含 States 表中的列,我会得到如下结果:

在此处输入图像描述

为什么StateName细节进入数组?

我可以通过调整查询将其与地址和城市详细信息一起放在外面吗?

4

1 回答 1

2

FOR JSON AUTO根据语句的结构自动格式化FOR JSON子句的输出SELECT文档说明:当您指定 AUTO 选项时,JSON 输出的格式将根据 SELECT 列表中的列顺序及其源表自动确定。您无法更改此格式

该语句连接表(尽管它使用旧样式的 JOIN 语法),因此表中的列States是作为嵌套 JSON 数组的属性生成的,使用表别名作为该嵌套数组的名称。

在您的情况下,更好的选择是FOR JSON PATH

SELECT 
   e.EmployeeId,
   (
        SELECT 
            a.Address
           ,a.City
           ,s.StateName
        FROM 
            [Address] AS a
           ,[States] AS s
        WHERE 
            a.EmployeeId = e.employeeId
            AND a.City = s.City
        FOR JSON PATH
   ) AS AddressDetails
FROM [Employee] AS e
WHERE e.EmployeeId = 8

结果:

EmployeeId AddressDetails
8          [
           {"Address":"4912 La Vuelta","City":"Bothell","StateName":"Washington"},
           {"Address":"4039 Elkwood Dr.","City":"Ballard","StateName":"Utah"}
           ]
于 2021-11-02T09:01:05.463 回答