SAP Business One Parameterized Queries - tutorial featured image

Publications

SAP Business One Parameterized Queries: Interactive Reports with SQL

Hector Pincheira

Hector Pincheira

CIO | CTO
Digital Strategy & Technology Innovation

items

SAP Business One Parameterized Queries is the difference between a report that is useful just once and one that works for you for years. Imagine the scene, so common in any company that uses the ERP: a manager asks for «March sales,» you run a query, export it, and hand it over. The next day they ask for «now April, but only for client X.» You edit the SQL again, change the dates by hand, adjust the filter, and run it again. Multiply that by ten weekly requests and you will see how much time evaporates on something that should be automatic.

The solution is not to write more queries, but to write smarter queries. Instead of editing the code every time, you define the query once with variables, and SAP asks the user for the values at runtime. Therefore, the same report works for any month, customer, or range without touching a line of SQL. In this guide you will build that type of report step by step, with the detail that makes the difference: each query comes in its version for SQL Server and for SAP HANA.

SAP Business One parameterized queries - interactive reporting panel
A parameterized report turns a static query into a reusable decision-making tool.

Why you need to master parameterized queries

A fixed report ages quickly. However, a parameterized one adapts to every business question without technical intervention. Furthermore, there are three concrete reasons why this approach elevates your operation.

First of all, you reduce the risk of error. Every time someone edits dates or codes by hand within the SQL, there is a possibility of breaking the query or filtering the data incorrectly. Consequently, delegating those values to a selection window eliminates that source of failures.

Secondly, you empower the end user. A warehouse manager or a salesperson can run the report and choose their own date range, without depending on IT for every variation. This way, your technical team stops being a bottleneck and focuses on the strategic.

Third, you standardize the analysis. When the entire company uses the same parameterized query, everyone looks at the data with the same logic. For example, «gross margin» is calculated the same way for sales, for finance, and for management, without parallel versions that contradict each other.

Prerequisites before starting

To follow this tutorial you need few elements, but they are essential. Below is the minimum list.

First, you require access to the Query Generator (Query Generator) or the Query Manager of SAP Business One, available in Tools > Queries. Additionally, your user must have the corresponding authorization to create and save queries.

Second, it is useful to know which database engine your installation uses, because the syntax changes. If your SAP runs on Microsoft SQL Server, you will use the left column of each example; if it runs on SAP HANA, you will use the HANA one. Therefore, identify your environment before copying any code.

Third, it helps to know the base tables: OINV customer invoices, OCRD (business partners) and their lines. However, even if you don't master them, the examples are self-explanatory and you will be able to adapt them.

Step-by-step tutorial: your first parameterized query

The heart of everything is in a symbol: [%0]. Each time SAP finds [%0], [%1], [%2] and so on, understand that it must stop and ask the user for that value before executing. Below is the complete process.

Step 1. Open Tools > Queries > Query Builder. The editor will open where you will write your SQL.

Step 2. Write the query using [%0] and [%1] wherever you want the user to enter values. For example, to ask for a date range you will use BETWEEN [%0] AND [%1].

Step 3. Execute with Execute (or F5). SAP will automatically open a window asking for each value. As a result, you do not edit anything: you just type or select.

Step 4. Save the query with Save, assign it a category and a clear name. This way it becomes available in the Query Manager for the entire organization.

SQL code with variables explained in detail

Let's look at four parameterized queries that solve real needs. Each one includes its version for SQL Server and for SAP HANA, because the syntax differs in details that break execution if you ignore them.

1) Sales by date range

This is the most used parameterization. The user chooses from and to, and the report returns the invoices for that period. In addition, by associating [%0] For a date field, SAP displays a calendar to choose from.

SQL Server

SELECT T0.DocNum, T0.CardCode, T1.CardName, T0.DocDate, T0.DocTotal
FROM OINV T0
INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
WHERE T0.DocDate BETWEEN [%0] AND [%1]
ORDER BY T0.DocDate

SAP HANA

SELECT T0."DocNum", T0."CardCode", T1."CardName", T0."DocDate", T0."DocTotal"
FROM OINV T0
INNER JOIN OCRD T1 ON T0."CardCode" = T1."CardCode"
WHERE T0."DocDate" BETWEEN [%0] AND [%1]
ORDER BY T0."DocDate""

Notice the key difference: HANA requires double quotes around field names, whereas SQL Server does not need them. Therefore, forgetting that detail is the number one mistake when migrating queries between engines.

2) Filter with a selection list (the field trick)

Here is the magic that few know: SAP decides which selection window to display based on the field to the left of the variable. For example, if you write T0.CardCode = '[%0]'', SAP will open the business partner list so you can choose one. As a result, the user does not memorize codes.

SQL Server

SELECT T0.DocNum, T0.DocDate, T0.DocTotal, T0.DocStatus
FROM OINV T0
WHERE T0.CardCode = '[%0]'
ORDER BY T0.DocDate DESC

SAP HANA

SELECT T0."DocNum", T0."DocDate", T0."DocTotal", T0."DocStatus"
FROM OINV T0
WHERE T0."CardCode" = '[%0]'
ORDER BY T0."DocDate" DESC

The same principle applies to articles (ItemCode open the product list) or vendors (SlpCode opens the sales employee list). This way, a single pattern gives you dozens of filtered reports.

SAP Business One parameterized queries - data analysis and SQL variables
Variables transform a rigid query into a flexible analysis dashboard.

3) Optional parameter: leave blank to view all

Sometimes you want a filter that is flexible: if the user enters a customer, it filters by that customer; but if they leave it empty, it shows all of them. However, many consultants do not know how to solve this. The trick is to evaluate if the parameter comes in empty.

SQL Server

SELECT T0.CardCode, T1.CardName, SUM(T0.DocTotal) AS 'Total Sales'
FROM OINV T0
INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode
WHERE ISNULL('[%0]','') = '' OR T0.CardCode = '[%0]'
GROUP BY T0.CardCode, T1.CardName
ORDER BY 3 DESC

SAP HANA

SELECT T0."CardCode", T1."CardName", SUM(T0."DocTotal") AS "Total Sales"
FROM OINV T0
INNER JOIN OCRD T1 ON T0."CardCode" = T1."CardCode"
WHERE IFNULL('[%0]','') = '' OR T0."CardCode" = '[%0]'
GROUP BY T0."CardCode", T1."CardName"
ORDER BY 3 DESC

Observe the change of function: SQL Server uses ISNULL, while HANA uses IFNULL. Both follow the same logic, but they are not interchangeable. Therefore, always verify the equivalent function before migrating.

4) Overdue days with respect to a cutoff date

This report requests a cutoff date and calculates how many days overdue each open invoice is. Furthermore, it is the basis for any serious collection management.

SQL Server

SELECT T0.DocNum, T0.CardCode, T0.DocDueDate,
       DATEDIFF(DAY, T0.DocDueDate, '[%0]') AS 'Days Past Due',
       T0.DocTotal
FROM OINV T0
WHERE T0.DocStatus = 'O' AND T0.DocDueDate < '[%0]'
ORDER BY 4 DESC

SAP HANA

SELECT T0."DocNum", T0."CardCode", T0."DocDueDate",
       DAYS_BETWEEN(T0."DocDueDate", TO_DATE('[%0]')) AS "Days Past Due",
       T0."DocTotal"
FROM OINV T0
WHERE T0."DocStatus" = 'O' AND T0."DocDueDate" < '[%0]'
ORDER BY 4 DESC

The difference in dates requires different functions: DATEDIFF vs SQL Server DAYS_BETWEEN in HANA. However, both receive the same parameter [%0], so the user experience is identical.

Advanced variations and use cases

Once you master the pattern, the possibilities multiply. For example, you can combine several parameters in the same query: start date, end date, and warehouse, giving the user three selection fields in a single execution.

In addition, you can reuse the same parameter multiple times. If you write [%0] In two places in the query, SAP requests it only once and applies that value to both. This way, you prevent the user from entering the same data twice.

You can also feed these queries into Crystal Reports or a Formatted Search, taking interactivity beyond the Query Manager. As a result, a parameterized report becomes the engine for complete dashboards.

Best practices and common mistakes

First, name your queries with a clear standard. For example, «VEN – Invoices by date range» indicates area and purpose at a glance. Also, group them by categories in the Query Manager so that no one duplicates work.

Second, watch out for quotation marks. In HANA, field names go in double quotes and text values in single quotes. However, mixing them up is the most frequent cause of syntax errors.

Third, validate the data type of the parameter. Yes [%0] populate a date field, associate it with a date column so that SAP displays the calendar; if you populate text, use single quotes around it. Therefore, the correct association defines the window the user will see.

Fourth, avoid leaving parameters without context. A [%0] Loose, without a field to the left, it will generate a generic, scarcely useful window. Consequently, always anchor the variable to a recognizable field.

Official SAP Resources

For more details, consult the official documentation at SAP Help Portal, consultation section and the Query Generator. Furthermore, the community in SAP Community It maintains active threads on parameters in HANA and SQL Server. However, always validate in a test environment before releasing a query to production.

Conclusion: From reports operator to information architect

Parameterized queries might seem like a minor technical detail, but they change your role within the company. Instead of running the same report over and over again, you build tools that others use autonomously. This way, your time stops being spent on repetitive tasks and is invested in analysis that generates real value.

The next step is yours: take one of your most requested reports, parameterize it with [%0] and observe how many hours you recover each month. As a result, you will have taken a concrete step toward data management that scales with your business.

This tutorial is part of the exclusive content of the Strategic Circle. If you want to access the complete library of practical SAP Business One tutorials, with ready-to-use queries in SQL Server and HANA, join at hectorpincheira.com and transform your ERP into a true competitive advantage.

Most Read

QlikSense Hierarchy: parent-child chart of accounts expanded by levels
QlikSense Hierarchy: the Chart of Accounts That Doesn't Add Up
QlikSense Sum TOTAL: share of each family over the total
QlikSense Sum TOTAL: the Percentage That Changes When You Filter
SAP Business One Inventory Revaluation - featured image of the tutorial
SAP Business One Inventory Revaluation: Fix Wrongly Loaded Costs
Empty chamber with wooden benches: the counterweight that required being present to be used
The Veto You Had to Say in Person: Your Decisions With No Counterweight
SAP Business One customizations - featured image of the pre-upgrade audit tutorial
SAP Business One Customizations: Pre-Upgrade Audit
SAP Business One Customer Concentration - featured image of the tutorial
SAP Business One Customer Concentration: Measure Your Risk with SQL

RELATED ARTICLES