I have a query that displays voided transactions, and I am passing a parameter value to the WHERE clause with like but it is not returning any rows, I know I am missing something but I can't seem to figure out what.
declare @ITEM char(1000)set @ITEM = 'ATIGM9130'SELECT [Company]='NAME',[Source]=cast('History' as varchar(7)),[Doc_Type]=CASE SOP30200.SOPTYPE when 1 then 'QUOTE' when 2 then 'ORDER' when 3 then 'INVOICE' when 4 then 'RETURN' when 5 then 'BACKORDER' else '' end,[Doc_Number]=SOP30200.SOPNUMBE,SOP30300.ITEMNMBR,SOP30300.ITEMDESC,SOP30300.QUANTITY,SOP30200.ORIGNUMB,[Doc_Date]=SOP30200.DOCDATE,[GL_Posting_Date]=SOP30200.GLPOSTDT, [Invoice_Date]=SOP30200.INVODATE,SOP30200.FUFILDAT,SOP30200.ACTLSHIP,SOP30200.LOCNCODE,SOP30200.BCHSOURC,[Batch_No]=SOP30200.BACHNUMB,SOP30200.CUSTNMBR, SOP30200.CUSTNAME,SOP30200.ADDRESS1,SOP30200.ADDRESS2,SOP30200.CITY,SOP30200.[STATE],SOP30200.CNTCPRSN,SOP30200.PHNUMBR1, SOP30200.PHNUMBR2,SOP30200.SHIPMTHD,[Posting_Status]=CASE SOP30200.PSTGSTUS when 0 then 'NOT POSTED' when 2 then 'POSTED' else 'UNKNOWN' end,[Void_Status]=CASE SOP30200.VOIDSTTS when 1 then 'VOIDED' else '' end,--[Message] = CASE SOP30200.VOIDSTTS when 1 then 'Voided By will be the Batch_No for the transaction.' else '' end,[Voided By]= CASE SOP30200.VOIDSTTS when 0 then '' else SOP30200.BACHNUMB end,IV00101.ITMCLSCDFROM SOP30200 WITH(nolock) INNER JOIN SOP30300 WITH(nolock) ON SOP30200.SOPTYPE = SOP30300.SOPTYPE AND SOP30200.SOPNUMBE = SOP30300.SOPNUMBE INNER JOIN IV00101 WITH(nolock) ON SOP30300.ITEMNMBR = IV00101.ITEMNMBRWHERE (IV00101.ITMTRKOP = 2) AND (NOT (SOP30200.SOPTYPE = 1)) AND (SOP30300.ITEMNMBR LIKE '%'+@ITEM+'%' )--AND SOP30300.ITEMNMBR in (select Value from dbo.parmstolist(@ITEM, ','))ORDER BY 8 DESC
for example:
- may be you need to replace AND with OR.
- make sure ITEMNMBR is varchar...
Qlemo
That (commented) subquery of select Value from dbo.parmstolist(@ITEM, ',') points to that @item contains more than one value?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
skull52
ASKER
If I just use the @ITEM param without the LIKE and enter the full item number value it works just fine. the (select Value from dbo.parmstolist(@ITEM, ',') is a stored proc that allows me to pass several values separated by a comma, which also works if I enter multiple complete Item numbers which I don't want do do. Adding an OR does not filter on the parameter value.
Zberteoc
The cause could be multiple and not necessarily the LIKE clause condition. It is possible that the INNER JOIN conditions filter out the results or maybe in a different WHERE clause condition.
My advice is to run a query only with count to see which condition is actually causing the filtering out of the rows:
declare @ITEM char(1000)set @ITEM = 'ATIGM9130'SELECT count(*) cntFROM SOP30200 WITH(nolock) INNER JOIN SOP30300 WITH(nolock) ON SOP30200.SOPTYPE = SOP30300.SOPTYPE AND SOP30200.SOPNUMBE = SOP30300.SOPNUMBE INNER JOIN IV00101 WITH(nolock) ON SOP30300.ITEMNMBR = IV00101.ITEMNMBRWHERE (IV00101.ITMTRKOP = 2) AND (NOT (SOP30200.SOPTYPE = 1)) AND (SOP30300.ITEMNMBR LIKE '%'+@ITEM+'%' )
Start from bottom up and comment one condition at a time and then one join at a time and execute the query after each commenting action. Stop when you will get any count > 0
If you will get rows aftec commenting the LIKE condition it means that there are no ITEMNUMBERs with that format: %ATIGM9130%
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Unlimited question asking, solutions, articles and more.
skull52
ASKER
Yep it was the varchar, I normally don't use char, I copied that declare statement from another query I did a while ago which was a char, missed that one, thanks pourfard and thanks to all who responded. "Happy Holidays"