|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2005-10-13 : 08:00:15
|
| Tena writes "I am new to SQL programming and I have a project that is for a manufacturing facility. At the start of this project parts were initially all loaded into a table (tblItems), and then parts were removed and added as time progressed.So, now they want 7000 parts added back from the backup that originally contained all these parts (I was given a few SP's to accomplish this task), but the problem is that the original itemid's didn't carry over with the added items. It is imperative that they keep their original id's as the info from the BOM (Bill of Material) tables is also being brought over and itemid's are what identifies them to the BOM tables.I am including the SP's that are doing the work. TIA-Tena______________________________________________CREATE PROCEDURE spBeck_CopyPartsASSET NOCOUNT ONdeclare @ItemNumber varchar(50)declare @id intDECLARE items CURSOR LOCAL FAST_FORWARD FOR SELECT ItemNumberFROM PARTFILEOPEN items FETCH items INTO @ItemNumberWHILE @@FETCH_STATUS = 0 BEGIN exec spBeck_CopyItem @ItemNumber, @id outFETCH items INTO @ItemNumberEND CLOSE items DEALLOCATE items RETURN_______________________________________________ALTER PROCEDURE spBeck_CopyItem(@item_number varchar(50),@new_id int out)ASSET NOCOUNT ONdeclare @item_id intdeclare @sub_id intdeclare @StepId intdeclare @Qty floatdeclare @ToBeProduced tinyintdeclare @IsAdditive tinyintdeclare @SeqNo intdeclare @QtyScalingFactor floatdeclare @ItemNumber varchar(50)set @new_id = NULLSELECT @new_id = ItemIdFROM tblItemsWHERE ItemNumber = @item_numberif @new_id IS NOT NULL returnSELECT @item_id = ItemIdFROM backups..tblItemsWHERE ItemNumber = @item_numberINSERT INTO tblItems ( ItemNumber, ItemCode, ItemDescription, MaterialId, PrimaryUOMID, PrimaryUOMtypeID, SecondaryUOMID, SecondaryUOMtypeId, UOMConversionFactor, IsInventoried, Comments, IsDeleted, CategoryID, MinBuildQty, MinInventoryQty, IsVendorSupplied, VendorID, IsSpecial, BuildYield, AtomicBuildQty, IncrementBuildQty, IsScrap, IsCombinable, ToBeProduced, ReplenishmentTime, AverageBuildQty, UnitPrice, IsCountByWeight, Origin, ToBeChecked, dtChecked, LastMod, dtLastMod, MaxBuildQty, DemandDirectly )SELECT ItemNumber, ItemCode, ItemDescription, MaterialId, PrimaryUOMID, PrimaryUOMtypeID, SecondaryUOMID, SecondaryUOMtypeId, UOMConversionFactor, IsInventoried, Comments, IsDeleted, CategoryID, MinBuildQty, MinInventoryQty, IsVendorSupplied, VendorID, IsSpecial, BuildYield, AtomicBuildQty, IncrementBuildQty, IsScrap, IsCombinable, ToBeProduced, ReplenishmentTime, AverageBuildQty, UnitPrice, IsCountByWeight, Origin, ToBeChecked, dtChecked, LastMod, dtLastMod, MaxBuildQty, DemandDirectlyFROM backups..tblItemsWHERE ItemId = @item_idSELECT @new_id = ItemIdFROM tblItemsWHERE ItemNumber = @item_number-- now let's copy the BOMBR-- first, copy route stepsINSERT INTO tblBOMBRSteps( ItemId, StepNo, OperationId, WIPTypeOut, UOMout, SecondaryUOMout, Instructions, ISOdocument, Duration, StepCode)SELECT @new_id, StepNo, OperationId, WIPTypeOut, UOMout, SecondaryUOMout, Instructions, ISOdocument, Duration,StepCodeFROM backups..tblBOMBRStepsWHERE ItemId = @item_idORDER BY StepNo-- then, copy BOMBR input items-- note that items might not exist, so we need to copy them as wellDECLARE materials CURSOR LOCAL FAST_FORWARD FOR SELECT N.StepId,I.Qty, I.ToBeProduced, I.IsAdditive, I.SeqNo, I.QtyScalingFactor,P.ItemNumberFROM backups..tblBOMBRStepItems I,backups..tblBOMBRSteps B, tblBOMBRSteps N,backups..tblItems PWHERE B.ItemId = @item_id ANDI.StepId = B.StepI |
|