|
marwest98
Starting Member
9 Posts |
Posted - 2011-09-23 : 09:57:42
|
| I am selecting data in the stored procedure below that works great except in one situation. When selecting a record from the prodstructure file (C) if the prdstrparentitemno does not have a prdstrcomponentitemno of 260006 then I have to take the prdstrcomponentitemno and move it to the prdstrparentitemno and search for one that does (second Level component) If I find a 'second level' that has a component of 260006 I still must print the first parentitem on a label. For example:select * from dbo.ProdStructureFile Ainner join ProdStructureFile BON A.PrdStrParentItemNo=B.PrdStrComponentItemNowhere B.PrdStrParentItemNo = '600972'and A.PrdStrComponentItemNo='260006'The first select without the inner join and the and condition would return a prdstrparentitemno of 600972 with the inner join I get CA32112 L as the prdstrparentitemno field that would print on the label not the 600972I'm lost at how to get this to work.My stored procedure that works for all other items but this situation looks like:USE [SilverLabel]GO/****** Object: StoredProcedure [dbo].[sel_OrderInformation] Script Date: 09/21/2011 15:45:22 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Mary Westerman-- Create date: 08/17/11-- Description: Stored procedure to get order -- information necessary to create-- a silverlabel--[sel_OrderInformation] '74543','74714','260006','Y'-- =============================================ALTER PROCEDURE [dbo].[sel_OrderInformation] -- Add the parameters for the stored procedure here@BegOrderNo nvarchar(6),@EndOrderNo nvarchar(6),@label nvarchar(6),@useCustPartNo nvarchar (1)ASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;SELECT CAST(a.OrderNo as NVarchar),CAST(a.OrderCustomerNo as NVarchar),CAST(a.OrderShippingDate as NVarchar),CAST(B.LineItemQtyToShip as NVarchar),CAST(C.prdstrComponentItemNo as NVarchar),CAST(RIGHT(a.OrderNo,3) as NVARCHAR(3))Ord,CAST(C.PrdStrParentItemNo as NVArchar) From dbo.COPOrderHeaderFile as AInner Join dbo.COPOrderLineItemFile as Bon A.OrderNo = B.LineItemOrderNoInner Join dbo.ProdStructureFile as Con ltrim(rtrim(B.LineItemItemNo)) = ltrim(rtrim(c.PrdStrParentItemNo))where A.OrderNo >= ltrim(rtrim(@BegOrderNo))and A.OrderNo <= ltrim(rtrim(@EndOrderNo))and B.LineItemLocation = ' 1'and LTRIM(rtrim(C.prdstrComponentItemNo)) = @labelorder by A.OrderNoCREATE Table #OrderItems (OrderNo NVarchar(20),ParentItem NVarchar(20),ShippingDate Int,Qty Int,MatlComponent NVarchar(10),Ord NVarchar(3),CustomerItem NVarchar(50))-- Insert statements for procedure hereif @useCustPartNo = 'N' Begin INSERT #OrderItemsSELECT CAST(a.OrderNo as NVarchar),CAST(a.OrderCustomerNo as NVarchar),CAST(a.OrderShippingDate as NVarchar),CAST(B.LineItemQtyToShip as NVarchar),CAST(C.prdstrComponentItemNo as NVarchar),CAST(RIGHT(a.OrderNo,3) as NVARCHAR(3))Ord,CAST(C.PrdStrParentItemNo as NVArchar) From dbo.COPOrderHeaderFile as AInner Join dbo.COPOrderLineItemFile as Bon A.OrderNo = B.LineItemOrderNoInner Join dbo.ProdStructureFile as Con ltrim(rtrim(B.LineItemItemNo)) = ltrim(rtrim(c.PrdStrParentItemNo))where A.OrderNo >= ltrim(rtrim(@BegOrderNo))and A.OrderNo <= ltrim(rtrim(@EndOrderNo))and B.LineItemLocation = ' 1'order by A.OrderNoendelseif @useCustPartNo = 'Y' BeginINSERT #OrderItemsSELECT a.OrderNo,a.OrderCustomerNo,a.OrderShippingDate,B.LineItemQtyToShip,C.prdstrComponentItemNo,RIGHT(a.OrderNo,3)Ord,ISNULL(D.CustPartCustPartNumber ,b.LineItemItemNo)CustPartCustPartNumber From dbo.COPOrderHeaderFile as AInner Join dbo.COPOrderLineItemFile as Bon A.OrderNo = B.LineItemOrderNoLeft Outer Join dbo.ProdStructureFile as C on ltrim(rtrim(LEFT(B.LineItemItemNo,PATINDEX('%LW%',B.LineItemItemNo)-1))) = ltrim(rtrim(c.PrdStrParentItemNo))Left Outer Join dbo.CustPartNumberFile as don a.OrderCustomerNo = d.CustPartcustNumber2 where A.OrderNo >= @BegOrderNoand A.OrderNo <= @EndOrderNoand b.LineItemLocation = ' 1'and LTRIM(rtrim(C.prdstrComponentItemNo)) = @labeland B.LineItemItemNo like '%LW%'unionSELECT a.OrderNo,a.OrderCustomerNo,a.OrderShippingDate,B.LineItemQtyToShip,C.prdstrComponentItemNo,RIGHT(a.OrderNo,3)Ord,ISNULL(D.CustPartCustPartNumber ,b.LineItemItemNo)CustPartCustPartNumber From dbo.COPOrderHeaderFile as AInner Join dbo.COPOrderLineItemFile as Bon A.OrderNo = B.LineItemOrderNoLeft Outer Join dbo.ProdStructureFile as C on ltrim(rtrim(B.LineItemItemNo)) = ltrim(rtrim(c.PrdStrParentItemNo))Left Outer Join dbo.CustPartNumberFile as don a.OrderCustomerNo = d.CustPartcustNumber2 where A.OrderNo >= @BegOrderNoand A.OrderNo <= @EndOrderNoand b.LineItemLocation = ' 1'and LTRIM(rtrim(C.prdstrComponentItemNo)) = @labelorder by A.OrderNoendSELECT OrderNo ShopOrder#,CustomerItem Item#,CAST(Right(ShippingDate,6) as varchar(10))+'-'+Cast(RIGHT('000' + RTRIM(ltrim(rtrim(Ord))), 3)as Varchar(6))Serial#,Qty FROM #OrderItemsENDThis SQL is basically what I need to get to but does not seem to work in the stored procedure even if I add another column to the table:SELECT a.OrderNo, a.OrderCustomerNo, a.OrderShippingDate, B.LineItemQtyToShip, C.prdstrComponentItemNo, D.prdstrComponentItemNo, RIGHT(a.OrderNo,3)as Ord, C.PrdStrParentItemNo From dbo.COPOrderHeaderFile as A Inner Join dbo.COPOrderLineItemFile as B on A.OrderNo = B.LineItemOrderNo Inner Join dbo.ProdStructureFile as C on ltrim(rtrim(B.LineItemItemNo)) = ltrim(rtrim(c.PrdStrParentItemNo)) left join dbo.ProdStructureFile as D ON D.PrdStrParentItemNo=C.PrdStrComponentItemNo where B.LineItemLocation = ' 1' and A.OrderNo >='74526' and A.OrderNo <='74714' and (LTRIM(rtrim(C.prdstrComponentItemNo)) = '260006' or LTRIM(rtrim(D.prdstrcomponentitemno)) = '260006') order by A.OrderNo Please help. |
|