I have 3 tables, All 3 tables have Auto-Increment primary key column with foreign key relationship.
My problem is that I want to copy the related data in same table.
And also to maintain the PK-->FK relationship.
For example I want to copy GoalID = 1. I have also created variable tables and data as my scenario.
Declare @tblCompanyGoal Table
(
GoalID int identity(1,1), --PKID
APID int,
Goal nvarchar(500)
)
Declare @tblPMCompanyObjectives Table
(
ObjectID int identity(1,1), --PKID
ApID int,
Objective nvarchar(500),
GoalID int --FK --> @tblCompanyGoal.GoalID
)
Declare @tblPMCompanyStrategies Table
(
StrgID int identity(1,1), --PKID
Strategies nvarchar(500),
ObjectID int --FK --> @@tblPMCompanyObjectives.ObjectID
)
Insert into @tblCompanyGoal (APID, Goal)Values(500, 'C-Goal1')
Insert into @tblCompanyGoal (APID, Goal)Values(600, 'C-Goal2')
insert into @tblPMCompanyObjectives (ApID, Objective, GoalID)values(500, 'BF', 1)
insert into @tblPMCompanyObjectives (ApID, Objective, GoalID)values(500, 'LF', 1)
insert into @tblPMCompanyObjectives (ApID, Objective, GoalID)values(500, 'BFA', 2)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('GTK', 1)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('GTK2', 1)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('ASK', 1)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('WER', 2)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('NFT', 2)
Insert into @tblPMCompanyStrategies(Strategies, ObjectID)Values('KRG', 3)
How can I accomplish this task? I have tried it with cursor but not succeeded.