-2

我想为所有主行复制详细表中的相关行。

例如:
主表为:

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

4 回答 4

1

您在输出中给出的是交叉连接 (http://en.wikipedia.org/wiki/Join_%28SQL%29#Cross_join),通常是“一个坏主意”。

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

于 2012-08-13T12:13:34.323 回答
1

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

PK | FK | 数据
---------------
1 | 1 | AA
2 | 2 | BB
3 | 3 | 抄送

你可以这样查询:

SELECT detail.* FROM master, detail

看到这个 SQLFiddle

于 2012-08-13T12:17:01.577 回答
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>
于 2012-08-15T13:54:11.623 回答
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-数据

于 2012-08-13T12:56:29.373 回答