SQL Server:复杂的插入

时间:2013-11-06 02:57:13

标签: sql sql-server tsql

我有两个表,我想用数据填充。这些表格为ThreadsPosts。我还有一个名为Source的表,其中包含数据。

ThreadsPosts包含很多要填充的列,因此为了简单起见,我不会将它们粘贴到此处,但大多数都可以是一些固定值。 Source表包含以下列:title(进入Threads.title),postContent(进入Posts.content

为了复制数据:

  1. 我需要将title列中的Source列复制到Threads表中,然后在其中添加一些固定日期和作者用户名(我希望作者有些不变)字符串,以及从某些T-SQL函数自动生成的日期DateTime

  2. 现在,当创建Threads行时,我需要获取它的ID,并创建新的Posts行,其中包含ID个新主题,来自Source.postContent的内容以及其他一些固定值

  3. 我知道这可能很复杂,但你可以在这里给我一些指导吗?我该怎么办?这里的主要问题是需要先创建Threads,然后在Posts中使用它的ID。

1 个答案:

答案 0 :(得分:1)

请尝试使用以下代码段。

创建表并添加虚拟数据

/****** Object:  Table [dbo].[Threads]    Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Threads](
    [ThreadID] [int] IDENTITY(1,1) NOT NULL,
    [ThreadTitle] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Threads] PRIMARY KEY CLUSTERED 
(
    [ThreadID] 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].[SourceTable]    Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[SourceTable](
    [SourceTableID] [int] IDENTITY(1,1) NOT NULL,
    [SourceTitle] [nvarchar](50) NULL,
    [SourceContent] [nvarchar](50) NULL,
 CONSTRAINT [PK_SourceTable] PRIMARY KEY CLUSTERED 
(
    [SourceTableID] 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].[Posts]    Script Date: 11/06/2013 13:57:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Posts](
    [PostID] [int] IDENTITY(1,1) NOT NULL,
    [PostContent] [nvarchar](50) NULL,
    [ThreadID] [int] NOT NULL,
 CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED 
(
    [PostID] 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:  ForeignKey [FK_Posts_Threads]    Script Date: 11/06/2013 13:57:51 ******/
ALTER TABLE [dbo].[Posts]  WITH CHECK ADD  CONSTRAINT [FK_Posts_Threads] FOREIGN KEY([ThreadID])
REFERENCES [dbo].[Threads] ([ThreadID])
GO
ALTER TABLE [dbo].[Posts] CHECK CONSTRAINT [FK_Posts_Threads]
GO


SET IDENTITY_INSERT [dbo].[SourceTable] ON
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (1, N'blog1', N'blogdesc1')
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (2, N'blog2', N'blogdesc2')
INSERT [dbo].[SourceTable] ([SourceTableID], [SourceTitle], [SourceContent]) VALUES (3, N'blog3', N'blogdesc3')
SET IDENTITY_INSERT [dbo].[SourceTable] OFF

查询以在表格中插入数据

CREATE TABLE #SummaryOfChanges(actionType NVARCHAR(50),ThreadID NVARCHAR(40),SourceContent NVARCHAR(40))

MERGE INTO  Threads AS d
USING (SELECT SourceTableID,SourceTitle,SourceContent FROM SourceTable) AS s
ON 1 = 2 
WHEN NOT MATCHED THEN
    INSERT (ThreadTitle)
    VALUES (s.SourceTitle)
    OUTPUT $action, Inserted.ThreadID, s.SourceContent INTO #SummaryOfChanges;

MERGE INTO  Posts AS d
USING (SELECT ThreadID,SourceContent FROM #SummaryOfChanges) AS s
ON d.ThreadID = s.ThreadID 
WHEN MATCHED THEN 
            UPDATE SET  d.PostContent= s.SourceContent
WHEN NOT MATCHED THEN
    INSERT (ThreadID,PostContent)
    VALUES (ThreadID,s.SourceContent);

DROP TABLE #SummaryOfChanges

如果有任何疑虑,请告诉我。