Using EXISTS
Jul 23, 2005I need to insert records into the table parSalesDetailModifier from
OLDparSalesDetailModifier where (1) those records DO NOT exit in
parSalesDetailModifier 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 parSalesDetailModifier. 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 ParSalesDetailModifier
(
parSalesDetailModifierID,
parSalesHdrID,
parSalesDetailID,
ModifierType,
POSModifier,
Condiment,
CondimentPrice,
UnitNumber,
BusinessDay
)
SELECT
OLD.parSalesDetailModifierID,
OLD.parSalesHdrID,
OLD.parSalesDetailID,
OLD.ModifierType,
OLD.POSModifier,
OLD.Condiment,
OLD.CondimentPrice,
OLD.UnitNumber,
OLD.BusinessDay
FROM OldParSalesDetailModifier OLD
WHERE
EXISTS
( SELECT DET.parSalesHdrID,
DET.parSalesDetailID,
DET.UnitNumber,
DET.BusinessDay
FROM ParSalesDetail DET
WHERE OLD.parSalesHdrID = DET.parSalesHdrIDAND
OLD.parSalesDetailID = DET.parSalesDetailIDAND
OLD.UnitNumber = DET.UnitNumberAND
OLD.BusinessDay = DET.BusinessDay
)
AND
NOT EXISTS
( SELECT NEW.parSalesHdrID,
NEW.parSalesDetailID,
NEW.parSalesDetailModifierID,
NEW.UnitNumber,
NEW.BusinessDay
FROM ParSalesDetailModifier NEW
WHERE OLD.parSalesHdrID = NEW.parSalesHdrIDAND
OLD.parSalesDetailID = NEW.parSalesDetailIDAND
OLD.parSalesDetailModifierID = NEW.parSalesDetailModifierIDAND
OLD.UnitNumber = NEW.UnitNumberAND
OLD.BusinessDay = NEW.BusinessDay
)
AND OLD.BusinessDay between @S and @E
-------------------------------
- END STORED PROCEDURE
-------------------------------
-------------------------------
- CREATE TABLES
-------------------------------
CREATE TABLE [parSalesDetailModifier] (
[ParSalesDetailModifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailID] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[CondimentPrice] [money] NOT NULL ,
[UnitNumber] [int] NOT NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDetailModifier] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailID],
[ParSalesDetailModifierID]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDetailModifier_parSalesDetail] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailID]
) REFERENCES [parSalesDetail] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailID]
)
) ON [PRIMARY]
GO
CREATE TABLE [OLDparSalesDetailModifier] (
[ParSalesDetailModifierID] [int] NOT NULL ,
[parSalesHdrID] [int] NOT NULL ,
[parSalesDetailID] [int] NOT NULL ,
[ModifierTYPE] [int] NULL ,
[POSModifier] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
,
[Condiment] [varchar] (20) COLLATE SQL_Latin1_General_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 ,
[parSalesDetailID] [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_General_CP1_CI_AS NULL ,
[UnitNumber] [int] NOT NULL ,
[Depleted] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[AmountTenderTime] [datetime] NULL ,
[BusinessDay] [datetime] NOT NULL ,
CONSTRAINT [PK_parSalesDetail] PRIMARY KEY CLUSTERED
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID],
[parSalesDetailID]
) WITH FILLFACTOR = 70 ON [PRIMARY] ,
CONSTRAINT [FK_parSalesDetail_parSalesHdr] FOREIGN KEY
(
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
) REFERENCES [parSalesHdr] (
[BusinessDay],
[UnitNumber],
[parSalesHdrID]
)
) ON [PRIMARY]
GO
-------------------------------
- END CREATE TABLES
-------------------------------
-------------------------------
- INSERT INTO TABLES
-------------------------------
insert into parSalesDetailmodifier values (1,2298561,10917332,2,'ADD
G-ON','ADD G-ON',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (1,2298561,10917332,2,'ADD
G-ON','ADD G-ON',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (2,2298561,10917332,2,'SUB
MAYO','SUB MAYO',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values
(3,2298561,10917332,2,'TBBS','TBBS',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (1,2298561,10917340,2,'SUB
MAYO','SUB MAYO',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (2,2298561,10917340,2,'NO
ONIN','NO ONIN',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values
(3,2298561,10917340,2,'TBBS','TBBS',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values
(4,2298561,10917340,2,'WELL','WELL',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (1,2298561,10917341,2,'ADD
G-ON','ADD G-ON',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values (2,2298561,10917341,2,'SUB
MAYO','SUB MAYO',.0000,2,'2003-12-01')
insert into oldparSalesDetailmodifier values
(3,2298561,10917341,2,'TBBS','TBBS',.0000,2,'2003-12-01')
insert into parSalesDetail
values(2298561,10917332,0,1,0,.0000,.0000,3.4900,. 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,.2500,.0 000,'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,'2PIE99',2,'N','2003-12-01
10:00:02.000','2003-12-01')
insert into parSalesDetail
values(2298561,10928911,0,1,0,.0000,.0000,.5900,.0000,'DECAF',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,'BOB-BAC',2,'N','2003-12-01
10:09:44.000','2003-12-01')
insert into parSalesDetail
values(2298561,10929376,0,1,0,.0000,.0000,.5900,.0000,'COFFEE',2,'N','2003-12-01
10:00:44.000','2003-12-01')
-------------------------------
- END INSERT INTO TABLES
-------------------------------