I need to insert records into the table parSalesDetailM odifier from
OLDparSalesDeta ilModifier where (1) those records DO NOT exit in
parSalesDetailM odifier and (2) those records have a parent record in
parSalesDetail.
When I run the below query I get the error message that I am violating
the Primary Key Constraint for parSalesDetailM odifier. In other words,
it's trying to insert a record that does exist.
Also posted below are create and insert startements for thte tables.
If someone would be kind enough to show me what I am doing wrong, I'd
really appreciate it.
Thanks,
Jennifer
-------------------------------
- STORED PROCEDURE
-------------------------------
CREATE Proc LoadModifier2
@S datetime,
@E datetime
AS
INSERT INTO ParSalesDetailM odifier
(
parSalesDetailM odifierID,
parSalesHdrID,
parSalesDetailI D,
ModifierType,
POSModifier,
Condiment,
CondimentPrice,
UnitNumber,
BusinessDay
)
SELECT
OLD.parSalesDet ailModifierID,
OLD.parSalesHdr ID,
OLD.parSalesDet ailID,
OLD.ModifierTyp e,
OLD.POSModifier ,
OLD.Condiment,
OLD.CondimentPr ice,
OLD.UnitNumber,
OLD.BusinessDay
FROM OldParSalesDeta ilModifier OLD
WHERE
EXISTS
( SELECT DET.parSalesHdr ID,
DET.parSalesDet ailID,
DET.UnitNumber,
DET.BusinessDay
FROM ParSalesDetail DET
WHERE OLD.parSalesHdr ID = DET.parSalesHdr ID AND
OLD.parSalesDet ailID = DET.parSalesDet ailID AND
OLD.UnitNumber = DET.UnitNumber AND
OLD.BusinessDay = DET.BusinessDay
)
AND
NOT EXISTS
( SELECT NEW.parSalesHdr ID,
NEW.parSalesDet ailID,
NEW.parSalesDet ailModifierID,
NEW.UnitNumber,
NEW.BusinessDay
FROM ParSalesDetailM odifier NEW
WHERE OLD.parSalesHdr ID = NEW.parSalesHdr ID AND
OLD.parSalesDet ailID = NEW.parSalesDet ailID AND
OLD.parSalesDet ailModifierID = NEW.parSalesDet ailModifierID AND
OLD.UnitNumber = NEW.UnitNumber AND
OLD.BusinessDay = NEW.BusinessDay
)
AND OLD.BusinessDay between @S and @E
-------------------------------
- END STORED PROCEDURE
-------------------------------
-------------------------------
- CREATE TABLES
-------------------------------
CREATE TABLE [parSalesDetailM odifier] (
[ParSalesDetailM odifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[CondimentPrice] [money] NOT NULL ,
[UnitNumber] [int] NOT NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDeta ilModifier] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D],
[ParSalesDetailM odifierID]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDeta ilModifier_parS alesDetail] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
) REFERENCES [parSalesDetail] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
)
) ON [PRIMARY]
GO
CREATE TABLE [OLDparSalesDeta ilModifier] (
[ParSalesDetailM odifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[CondimentPrice] [money] NOT NULL ,
[UnitNumber] [int] NOT NULL ,
[BusinessDay] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [parSalesDetail] (
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[Before] [int] NOT NULL ,
[Quantity] [int] NOT NULL ,
[After] [int] NOT NULL ,
[Promo] [money] NOT NULL ,
[PromoBefore] [money] NOT NULL ,
[ItemPrice] [money] NOT NULL ,
[PromoAfter] [money] NOT NULL ,
[POSItem] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[UnitNumber] [int] NOT NULL ,
[Depleted] [char] (1) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[AmountTenderTim e] [datetime] NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDeta il] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDeta il_parSalesHdr] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
) REFERENCES [parSalesHdr] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
)
) ON [PRIMARY]
GO
-------------------------------
- END CREATE TABLES
-------------------------------
-------------------------------
- INSERT INTO TABLES
-------------------------------
insert into parSalesDetailm odifier values (1,2298561,1091 7332,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7332,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7332,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7332,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7340,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7340,2,'NO
ONIN','NO ONIN',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7340,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(4,2298561,1091 7340,2,'WELL',' WELL',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7341,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7341,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7341,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917332,0,1,0, .0000,.0000,3.4 900,.0000,'DM', 2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917340,0,1,0, .0000,.0000,.25 00,.0000,'JALA' ,2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917341,0,1, 0,.0000,.0000,1 .3400,.0000,'MD-DP',2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928910,0,1, 0,.0000,.0000,. 9900,.0000,'2PI E99',2,'N','200 3-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928911,0,1, 0,.0000,.0000,. 5900,.0000,'DEC AF',2,'N','2003-12-01
10:09:44.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928912,0,1, 0,.0000,.0000,1 .6900,.0000,'BO B-BAC',2,'N','200 3-12-01
10:09:44.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10929376,0,1, 0,.0000,.0000,. 5900,.0000,'COF FEE',2,'N','200 3-12-01
10:00:44.000',' 2003-12-01')
-------------------------------
- END INSERT INTO TABLES
-------------------------------
OLDparSalesDeta ilModifier where (1) those records DO NOT exit in
parSalesDetailM odifier and (2) those records have a parent record in
parSalesDetail.
When I run the below query I get the error message that I am violating
the Primary Key Constraint for parSalesDetailM odifier. In other words,
it's trying to insert a record that does exist.
Also posted below are create and insert startements for thte tables.
If someone would be kind enough to show me what I am doing wrong, I'd
really appreciate it.
Thanks,
Jennifer
-------------------------------
- STORED PROCEDURE
-------------------------------
CREATE Proc LoadModifier2
@S datetime,
@E datetime
AS
INSERT INTO ParSalesDetailM odifier
(
parSalesDetailM odifierID,
parSalesHdrID,
parSalesDetailI D,
ModifierType,
POSModifier,
Condiment,
CondimentPrice,
UnitNumber,
BusinessDay
)
SELECT
OLD.parSalesDet ailModifierID,
OLD.parSalesHdr ID,
OLD.parSalesDet ailID,
OLD.ModifierTyp e,
OLD.POSModifier ,
OLD.Condiment,
OLD.CondimentPr ice,
OLD.UnitNumber,
OLD.BusinessDay
FROM OldParSalesDeta ilModifier OLD
WHERE
EXISTS
( SELECT DET.parSalesHdr ID,
DET.parSalesDet ailID,
DET.UnitNumber,
DET.BusinessDay
FROM ParSalesDetail DET
WHERE OLD.parSalesHdr ID = DET.parSalesHdr ID AND
OLD.parSalesDet ailID = DET.parSalesDet ailID AND
OLD.UnitNumber = DET.UnitNumber AND
OLD.BusinessDay = DET.BusinessDay
)
AND
NOT EXISTS
( SELECT NEW.parSalesHdr ID,
NEW.parSalesDet ailID,
NEW.parSalesDet ailModifierID,
NEW.UnitNumber,
NEW.BusinessDay
FROM ParSalesDetailM odifier NEW
WHERE OLD.parSalesHdr ID = NEW.parSalesHdr ID AND
OLD.parSalesDet ailID = NEW.parSalesDet ailID AND
OLD.parSalesDet ailModifierID = NEW.parSalesDet ailModifierID AND
OLD.UnitNumber = NEW.UnitNumber AND
OLD.BusinessDay = NEW.BusinessDay
)
AND OLD.BusinessDay between @S and @E
-------------------------------
- END STORED PROCEDURE
-------------------------------
-------------------------------
- CREATE TABLES
-------------------------------
CREATE TABLE [parSalesDetailM odifier] (
[ParSalesDetailM odifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[CondimentPrice] [money] NOT NULL ,
[UnitNumber] [int] NOT NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDeta ilModifier] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D],
[ParSalesDetailM odifierID]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDeta ilModifier_parS alesDetail] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
) REFERENCES [parSalesDetail] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
)
) ON [PRIMARY]
GO
CREATE TABLE [OLDparSalesDeta ilModifier] (
[ParSalesDetailM odifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[CondimentPrice] [money] NOT NULL ,
[UnitNumber] [int] NOT NULL ,
[BusinessDay] [datetime] NULL
) ON [PRIMARY]
GO
CREATE TABLE [parSalesDetail] (
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailI D] [int] NOT NULL ,
[Before] [int] NOT NULL ,
[Quantity] [int] NOT NULL ,
[After] [int] NOT NULL ,
[Promo] [money] NOT NULL ,
[PromoBefore] [money] NOT NULL ,
[ItemPrice] [money] NOT NULL ,
[PromoAfter] [money] NOT NULL ,
[POSItem] [varchar] (20) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[UnitNumber] [int] NOT NULL ,
[Depleted] [char] (1) COLLATE SQL_Latin1_Gene ral_CP1_CI_AS NULL ,
[AmountTenderTim e] [datetime] NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDeta il] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailI D]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDeta il_parSalesHdr] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
) REFERENCES [parSalesHdr] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
)
) ON [PRIMARY]
GO
-------------------------------
- END CREATE TABLES
-------------------------------
-------------------------------
- INSERT INTO TABLES
-------------------------------
insert into parSalesDetailm odifier values (1,2298561,1091 7332,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7332,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7332,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7332,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7340,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7340,2,'NO
ONIN','NO ONIN',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7340,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(4,2298561,1091 7340,2,'WELL',' WELL',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values (1,2298561,1091 7341,2,'ADD
G-ON','ADD G-ON',.0000,2,'20 03-12-01')
insert into oldparSalesDeta ilmodifier values (2,2298561,1091 7341,2,'SUB
MAYO','SUB MAYO',.0000,2,' 2003-12-01')
insert into oldparSalesDeta ilmodifier values
(3,2298561,1091 7341,2,'TBBS',' TBBS',.0000,2,' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917332,0,1,0, .0000,.0000,3.4 900,.0000,'DM', 2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917340,0,1,0, .0000,.0000,.25 00,.0000,'JALA' ,2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10917341,0,1, 0,.0000,.0000,1 .3400,.0000,'MD-DP',2,'N','2003-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928910,0,1, 0,.0000,.0000,. 9900,.0000,'2PI E99',2,'N','200 3-12-01
10:00:02.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928911,0,1, 0,.0000,.0000,. 5900,.0000,'DEC AF',2,'N','2003-12-01
10:09:44.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10928912,0,1, 0,.0000,.0000,1 .6900,.0000,'BO B-BAC',2,'N','200 3-12-01
10:09:44.000',' 2003-12-01')
insert into parSalesDetail
values(2298561, 10929376,0,1, 0,.0000,.0000,. 5900,.0000,'COF FEE',2,'N','200 3-12-01
10:00:44.000',' 2003-12-01')
-------------------------------
- END INSERT INTO TABLES
-------------------------------
Comment