Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Help with SQL Cursor

$
0
0
Hi, I am trying to explode Bill of materials in NAV 2013 R2 using SQL query. Here is my cursor reason. For some reason, the query does not loop through more than 2 levels of BOM for a finished good. The attached PDF shows the all the raw materials for a finished good. My result set is below where it does not print the raw materials for BOM RCP73 which is inside BOM MDESSWHITE.Production BOM No_ No_ Quantity Per Unit of Measure Code06200 RM000014 1.00000000000000000000 EA06200 RM000088 1.06000000000000000000 MT06200 RM000095 1.08000000000000000000 MT06200 RM000098 1.60000000000000000000 FT06200 RM000099 1.08900000000000000000 FT06200 RM000015 2.00000000000000000000 EA06200 RM000043 2.00000000000000000000 EA06200 RM000094 2.00000000000000000000 EA06200 RM000094 0.60000000000000000000 MT06200 RM000109 1.00000000000000000000 EA06200 RM000803 2.00000000000000000000 EAWould appreciate if someone can help me. Many thanks!Regards,UmaDECLARE @production_bom_no_ nvarchar(20), @no_ nvarchar(20), @quantity_per decimal(38,20), @unit_of_measure_code nvarchar(10);DECLARE @nested_production_bom_no_ nvarchar(20), @nested_no_ nvarchar(20), @nested_quantity_per decimal(38,20), @nested_unit_of_measure_code nvarchar(10);DECLARE @new_nested_no_ nvarchar(20);SET ROWCOUNT 1DECLARE bom_explode_cursor CURSOR FORSELECT b.[PRODUCTION BOM No_], b.[No_], b.[Quantity Per], b.[Unit of Measure Code]FROM [TOC$Production BOM Header] aINNER JOIN [TOC$Production BOM Line] bon a.[No_] = b.[Production BOM No_]and a.[Low-Level Code] = 1and a.[No_] NOT LIKE 'IMP%'INNER JOIN TOC$Item don a.No_ = d.No_and b.[Version Code] = ''OPEN bom_explode_cursorFETCH NEXT FROM bom_explode_cursorINTO @production_bom_no_, @no_, @quantity_per, @unit_of_measure_codeWHILE @@FETCH_STATUS = 0BEGIN IF @no_ LIKE 'RM%' INSERT INTO [BOM Explosion] VALUES ( @production_bom_no_, @no_, @quantity_per, @unit_of_measure_code ) ELSE BEGIN /** Here's where we need to have the nested cursor - loop through **/ SET @nested_production_bom_no_ = @no_ DECLARE nested_bom_cursor CURSOR FOR SELECT [Production BOM No_], [No_], [Quantity Per], [Unit of Measure Code] FROM [TOC$Production BOM Line] WHERE [PRODUCTION BOM No_] = @nested_production_bom_no_ AND [Version Code] = '' OPEN nested_bom_cursor FETCH NEXT FROM nested_bom_cursor INTO @nested_production_bom_no_, @nested_no_, @nested_quantity_per, @nested_unit_of_measure_code WHILE @@FETCH_STATUS = 0 BEGIN IF @nested_no_ LIKE 'RM%' INSERT INTO [BOM Explosion] VALUES ( @production_bom_no_, @nested_no_, @quantity_per, @unit_of_measure_code ) ELSE SET @nested_production_bom_no_ = @nested_no_ FETCH NEXT FROM nested_bom_cursor INTO @nested_production_bom_no_, @nested_no_, @nested_quantity_per, @nested_unit_of_measure_code END INSERT INTO [BOM Explosion] VALUES ( @production_bom_no_, /* The root production BOM No */ @nested_no_, @nested_quantity_per, @nested_unit_of_measure_code ) CLOSE nested_bom_cursor DEALLOCATE nested_bom_cursor END FETCH NEXT FROM bom_explode_cursor INTO @production_bom_no_, @no_, @quantity_per, @unit_of_measure_code ENDCLOSE bom_explode_cursor;DEALLOCATE bom_explode_cursor;

Viewing all articles
Browse latest Browse all 3145

Trending Articles