如何复制所有主记录的相关行

时间:2012-08-13 12:05:46

标签: sql tsql master-detail

我想在详细信息表中为master的所有行复制相关的行。

例如:
主表是:

PK   | Data
--------------
1    |  A
2    |  B
3    |  C

详细信息表是:

PK    | FK     | Data
---------------------
1     | 1      | AA
2     | 1      | BB
3     | 1      | CC

运行sql查询后:
详细信息表是:

PK    | FK     | Data
---------------------
1     | 1      | AA
2     | 1      | BB
3     | 1      | CC
1     | 2      | AA
2     | 2      | BB
3     | 2      | CC
1     | 3      | AA
2     | 3      | BB
3     | 3      | CC

4 个答案:

答案 0 :(得分:1)

您在输出中提供的是Cross Join(http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join),并且通常是“一个坏主意”。

通常,您希望至少有一个WHERE子句来限制返回的数据,因为表变得越大,您将获得的结果将快速增长。返回的行数将是两个表的大小的乘积。

答案 1 :(得分:1)

我认为你的详细信息表是这样的(FK数据应该是1,2,3而不是1,1,1

PK | FK | Data
---------------
1  | 1  | AA
2  | 2  | BB
3  | 3  | CC

您可以这样查询:

SELECT detail.* FROM master, detail

See this SQLFiddle

答案 2 :(得分:0)

insert into Detail(PK, FK, Data)
select D.PK, C.PK, D.Data
from Detail as D
  cross join (select PK
              from Master as M
              where M.PK <> 1) as C
where D.FK = 1

SE-Data

答案 3 :(得分:0)

</pre><code>
Create database:
USE [t]
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[detail](
    [PK] [int] IDENTITY(1,1) NOT NULL,
    [FK] [int] NULL,
    [Data] [varchar](20) NULL,
 CONSTRAINT [PK_detail] PRIMARY KEY CLUSTERED 
(
    [PK] 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 ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[detail]  WITH CHECK ADD  CONSTRAINT [FK_detail_master] FOREIGN KEY([FK])
REFERENCES [dbo].[master] ([PK])
GO

ALTER TABLE [dbo].[detail] CHECK CONSTRAINT [FK_detail_master]
GO

//Create master table:

USE [t]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[master](
    [PK] [int] IDENTITY(1,1) NOT NULL,
    [Data] [varchar](20) NULL,
 CONSTRAINT [PK_master] PRIMARY KEY CLUSTERED 
(
    [PK] 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 ANSI_PADDING OFF
GO


//Create detail table:

USE [t]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[douplicate]
 @source int,
 @des int
as
begin
insert into dbo.detail(FK,Data)
SELECT     @des,dbo.detail.Data
FROM         dbo.detail INNER JOIN
                      dbo.master ON dbo.detail.FK = dbo.master.PK
where fk=@source                  
end

//Insert values:

insert into master values 
  ('A'),
  ('B'),
 ('C')

insert into detail  values
  (1,'AA'),
  (2,'BB'),
  (3,'CC')

//Create procedure and solotioanswer:

USE [t]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[douplicate]
 @source int,
 @des int
as
begin
insert into dbo.detail(FK,Data)
SELECT     @des,dbo.detail.Data
FROM         dbo.detail INNER JOIN
                      dbo.master ON dbo.detail.FK = dbo.master.PK
where fk=@source                  
end
</code></pre>