Subject Re: Still have problems updating many-to-many in SP
Author Alan J Davies
Hi
Thanks to Set
Thanks for the prompt - and working - response to the problem I outlined.
It seems like I have put the wrong heading on the email.
After your reply I have thought about the problem and it is logically a
one-to-many relationship. A record is added to the Invhead table by a
Trigger which adds the next Inv_Nmbr and DNote_Nmbr. There are many
components - DelItems - being manufactured and packed in different
departments, but all components for any one customer are combined on to one
Delivery Note for that production batch ( by ACNO). After printing the
Delivery Notes, the DELIVERED field is set to 'Y' to exclude the record
from future processing, and the process begins again.
There will always be an Invhead record, created with this SP.

CREATE PROCEDURE DNOTE_INSERT
(
INV_DATE TIMESTAMP
)
AS
AS
Begin
/* Create New Delivery Note - DNote_Nmbr & Inv_Nmbr from Generator & Set by
Insert Trigger */
Insert into InvHead(Acno,No_Boxes,Inv_Date)
Select Distinct(Acno),0,:Inv_Date
from OrdItems
Where OrdItems.Deliver_It='Y';
Update InvHead /* Ready for Invoicing */
Set No_Boxes=(Select sum(No_Boxes)
from OrdItems
Where OrdItems.Acno=InvHead.Acno
And OrdItems.Deliver_It='Y'),
Ordno=(Select min(Ordno)
from OrdItems
Where OrdItems.Acno=InvHead.Acno
And OrdItems.Deliver_It='Y');
Update Paramfil
Set Delivered='Y';
end

It's only to do with stored procedures because I am using the code within
an SP and I can see that my Subject heading is misleading, as follows:-

CREATE PROCEDURE DNOTE_COMPLETION
AS
Begin
/* Update Delitems Version 3 */
Update Delitems DI
Set Inv_Nmbr=(Select First 1 InvHead.Inv_Nmbr From InvHead
Where DI.Acno=InvHead.Acno
And InvHead.Delivered='N'),
DNote_Nmbr=(Select First 1 InvHead.DNote_Nmbr From InvHead
Where DI.Acno=InvHead.Acno
And InvHead.Delivered='N')
Where DI.Delivered='N'
And Exists(Select 1 from InvHead
Where DI.Acno=InvHead.Acno
And InvHead.Delivered='N');
/* Insert into InvItems */
Insert into InvItems
Select Acno,Ordno,Ref,DNote_Nmbr,Inv_Nmbr,Partno,
Inv_Qty,Del_Date,0,0,'','Y',0
from Delitems
Where Delitems.Delivered='N';
/* Update Price, Unit from Orditems */
Update InvItems
Set InvItems.Sell_Price=(Select OrdItems.Sell_Price from OrdItems
Where InvItems.Acno=OrdItems.Acno
And InvItems.Ordno=OrdItems.Ordno
And InvItems.Partno=OrdItems.Partno
And OrdItems.Delivered='N' ),
InvItems.Unit=(Select OrdItems.Unit from OrdItems
Where InvItems.Acno=OrdItems.Acno
And InvItems.Ordno=OrdItems.Ordno
And InvItems.Partno=OrdItems.Partno
And OrdItems.Delivered='N' );
/* Set Delivered Flag */
Update Delitems
Set Delivered='Y'
Where Delivered='N';
/* Set Delivered Flag */
Update InvHead
Set Delivered='Y'
Where Delivered='N';
/* Allow new Delivery Notes */
Update Orditems
Set Delivered='N',Deliver_It='N',No_Boxes=0
Where Deliver_It='Y';
/* Allow new Delivery Notes */
Update Paramfil
Set Delivered='N';
end

Alan Davies