我有一张N:N关系表:
MyTable(IDTable1, IDTable2)
我必须具有ID的集合,一个具有表1的ID的集合,另一个具有表2的ID的集合,我必须将集合1中的每个ID与集合2中的所有ID关联起来。
是否可以通过一个T-Sql查询来完成,或者我需要为每个关系创建一个T-Sql查询?
谢谢。
编辑:我添加了脚本以生成3个表。具有三行的Table01,具有6行的table02和具有6条记录的N:N表。
USE [Dummy01]
GO
/****** Object: Table [dbo].[Table01] Script Date: 15/07/2018 16:43:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table01](
[IDTable01] [bigint] IDENTITY(1,1) NOT NULL,
[Description] [varchar](50) NOT NULL,
CONSTRAINT [PK_Tabl01] PRIMARY KEY CLUSTERED
(
[IDTable01] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Table01_Table02_Relationship] Script Date: 15/07/2018 16:43:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table01_Table02_Relationship](
[IDTable01] [bigint] NOT NULL,
[IDTable02] [bigint] NOT NULL,
CONSTRAINT [PK_Table01_Table02_Relationship] PRIMARY KEY CLUSTERED
(
[IDTable01] ASC,
[IDTable02] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Table02] Script Date: 15/07/2018 16:43:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table02](
[IDTable02] [bigint] IDENTITY(1,1) NOT NULL,
[Description] [varchar](50) NOT NULL,
CONSTRAINT [PK_Table02] PRIMARY KEY CLUSTERED
(
[IDTable02] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[Table01] ON
INSERT [dbo].[Table01] ([IDTable01], [Description]) VALUES (1, N'Description 01')
INSERT [dbo].[Table01] ([IDTable01], [Description]) VALUES (2, N'Description 02')
INSERT [dbo].[Table01] ([IDTable01], [Description]) VALUES (3, N'Description 03')
INSERT [dbo].[Table01] ([IDTable01], [Description]) VALUES (4, N'Description 04')
SET IDENTITY_INSERT [dbo].[Table01] OFF
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (2, 4)
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (2, 5)
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (2, 6)
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (3, 4)
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (3, 5)
INSERT [dbo].[Table01_Table02_Relationship] ([IDTable01], [IDTable02]) VALUES (3, 6)
SET IDENTITY_INSERT [dbo].[Table02] ON
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (1, N'Description 01')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (2, N'Description 02')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (3, N'Description 03')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (4, N'Description 04')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (5, N'Description 05')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (6, N'Description 06')
INSERT [dbo].[Table02] ([IDTable02], [Description]) VALUES (7, N'Description 07')
SET IDENTITY_INSERT [dbo].[Table02] OFF
答案 0 :(得分:2)
您可以通过单个查询来实现:
String id=databaseUser.push().getKey();