cancel
Showing results for 
Search instead for 
Did you mean: 

Adding variables breaks the query

IldiW
Explorer
0 Kudos

Thanks for reading!
I'm trying to find a way to make this query work with variables to list documents where the item price was within a range and the document date is later then something.

It seems to be working fine as long as I have defined dates and price range but as soon as I replace either with [%0] it breaks. The desired outcome is that the user can enter the variables for all three (date and price).

SELECT 
T0.Docentry
, T0.Docdate
, T0.CardCode
, T2.E_Mail
, T1.ItemCode
, T1.Price
FROM OINV T0 INNER JOIN INV1 T1 ON T0.Docentry=T1.Docentry
INNER JOIN OCRD T2 ON T0.CardCode=T2.CardCode
INNER JOIN OITM T3 ON T1.ItemCode=T3.ItemCode
WHERE T0.DocDate > '2022-09-01'  AND T1.Price  >= 17 AND T1.Price <= 30

I've tried to declare which has worked in a different query (although that one asked for item codes rather than price, not sure if that makes a difference).

/* SELECT * FROM OINV T0 */
DECLARE @PriceFrom NVARCHAR(19)
DECLARE @PriceTo NVARCHAR(19)
/*WHERE*/
SET @PriceFrom = /* T0.Price */ '[%0]'
SET @PriceTo = /* T0.Price */ '[%1]'
/*SELECT * FROM OFLT T1*/
DECLARE @FromDate DATE
DECLARE @ToDate DATE
/*WHERE*/
SET @FromDate = /* T1.FromDate */ '[%2]';

SELECT 
T0.Docentry
, T0.Docdate
, T0.CardCode
, T2.E_Mail
, T1.ItemCode
, T1.Price
FROM OINV T0 INNER JOIN INV1 T1 ON T0.Docentry=T1.Docentry
INNER JOIN OCRD T2 ON T0.CardCode=T2.CardCode
INNER JOIN OITM T3 ON T1.ItemCode=T3.ItemCode
WHERE T0.DocDate >= @FromDate AND T1.Price  >= @PriceFrom AND T1.Price <= @PriceTo
View Entire Topic
waeel
Explorer
0 Kudos

<feed xmlns="http://www.w3.org/2005/Atom">

<title>All content by waeel Taher</title>

<link rel="alternate" type="text/html" href="https://community.sap.com/" />

<id>https://community.sap.com/</id>

<rights>© 2018 SAP SE https://www.sap.com/about/legal/copyright.html</rights>;

<logo>https://www.sap.com/dam/application/shared/logos/sap-logo-svg.svg</logo>

<updated>2022-08-04T20:34:15Z</updated>

<entry>

<title>This is my question exercise as part of the SAP Community Q&A Tutorial</title>

<link rel="alternate" href="https://answers.sap.com/comments/13692066/view.html" />

<category term="SAP Business One" />

<category term="SAP Business ByDesign" />

<category term="submityourquestionnow" />

<author>

<name>waeel Taher</name>

</author>

<id>https://answers.sap.com/comments/13692066/view.html</id>

<updated>2022-08-04T20:34:15Z</updated>

<published>2022-08-04T20:34:15Z</published>

<summary type="text">This tutorial is version number 08.2022.001</summary>

</entry>

<entry>

<title>This is my question exercise as part of the SAP Community Q&A Tutorial</title>

<link rel="alternate" href="https://answers.sap.com/questions/13691679/this-is-my-question-exercise-as-part-of-the-sap-co-377.html" />

<category term="SAP Business One" />

<category term="SAP Business ByDesign" />

<category term="submityourquestionnow" />

<author>

<name>waeel Taher</name>

</author>

<id>https://answers.sap.com/questions/13691679/this-is-my-question-exercise-as-part-of-the-sap-co-377.html</id>

<updated>2022-08-06T05:04:12Z</updated>

<published>2022-08-02T22:15:43Z</published>

<summary type="text">How to change supplier quotation request after posting?</summary>

</entry>

</feed>