</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>