複製數據我在SQL Server中的兩個表如下這是通過FK SOPGroupsID相關: -從一個表到另一個
CREATE TABLE [dbo].[SOPGroups](
[SOPGroupsID] smallint NOT NULL IDENTITY(1,1),
[SOP_Group] varchar(200) NOT NULL UNIQUE,
PRIMARY KEY CLUSTERED (SOPGroupsID));
GO
CREATE TABLE [dbo].[SOPTitle](
[SOPGroupsID] smallint DEFAULT 1 NOT NULL,
[SOPTitleID] smallint NOT NULL IDENTITY(1, 1),
[SOP_Title] varchar(300) NOT NULL UNIQUE,
PRIMARY KEY CLUSTERED (SOPTitleID));
GO
--add relationship between tables SOPGroups and SOPTitle
ALTER TABLE [dbo].[SOPTitle] ADD CONSTRAINT [R1] FOREIGN KEY(SOPGroupsID)
REFERENCES [dbo].[SOPGroups] (SOPGroupsID)
ON UPDATE Cascade
ON DELETE Set default;
GO
我從一個Excel文件,該文件的第一步是導入進口數據excel文件轉換爲SQL服務器中的臨時表,然後將數據導入上面的表中,刪除重複的條目。我插入SOP組數據如下:在表SOPGroups: -
INSERT INTO [dbo].[SOPGroups] (SOP_Group)
SELECT DISTINCT SOP_Group FROM [dbo].['Onthology Return$'] t
WHERE NOT EXISTS
(SELECT SOP_Group FROM [dbo].[SOPGroups] x WHERE x.SOP_Group = t.SOP_Group)
我的問題是如何從表SOPGroups基於匹配從獨特SOP_Title的SOP_Group名插入到表[SOPTitle],並得到SOPGroupID臨時表'Onthology返回$'。
INSERT INTO [dbo].[SOPTitle] (SOPGroupsID, SOP_Title)
SELECT DISTINCT SOP_Group, SOP_Title FROM [dbo].['Onthology Return$'] t