SAP Business One Query Parameters Syntax Reference
This reference shows how to add user prompts to SAP Business One saved queries with [%0] parameters, including the DECLARE pattern for variables, date ranges, views and stored procedures. It covers SAP Business One 10.0 on SQL Server and SAP HANA, plus the OUQR and OQCN tables that store queries.
| Pattern | SQL Server syntax | HANA syntax | What the user sees | Notes |
|---|---|---|---|---|
Simple parameter | SELECT T0.DocNum, T0.CardName FROM OINV T0 WHERE T0.DocDate >= [%0] | SELECT T0."DocNum", T0."CardName" FROM OINV T0 WHERE T0."DocDate" >= [%0] | A prompt labelled with the field description (Posting Date) and a date picker | B1 replaces [%0] with the value before running the SQL. |
Parameter on a key column | WHERE T0.CardCode = [%0] | WHERE T0."CardCode" = [%0] | A prompt with a choose-from-list of business partners | B1 offers the list because it knows the column. |
Date range | WHERE T0.DocDate BETWEEN [%0] AND [%1] | WHERE T0."DocDate" BETWEEN [%0] AND [%1] | Two date prompts | Number parameters in the order you want them asked. |
Same parameter used twice | WHERE T0.DocDate >= [%0] OR T0.DocDueDate >= [%0] | WHERE T0."DocDate" >= [%0] OR T0."DocDueDate" >= [%0] | One prompt | A repeated number is asked once and reused. |
Several parameters, mixed fields | WHERE T0.CardCode = [%0] AND T0.DocDate >= [%1] AND T0.DocStatus = [%2] | WHERE T0."CardCode" = [%0] AND T0."DocDate" >= [%1] AND T0."DocStatus" = [%2] | Three prompts in one window | Each prompt gets the field description as label. |
Comment header for variables | /* SELECT FROM [dbo].[OINV] T0 */ | Not used | Nothing; it tells B1 which table the parameters refer to | Needed on SQL Server when the parameter is assigned to a variable instead of compared in WHERE. |
Variable assigned from a parameter | DECLARE @FromDate AS DATETIME /* WHERE */ SET @FromDate = /* T0.DocDate */ '[%0]' | Not available in a plain saved query: compare [%0] to the column directly | A Posting Date prompt | The comments /* WHERE */ and /* T0.DocDate */ give B1 the field for the prompt label and type. |
Variable used in the real query | SELECT T0.DocNum, T0.DocTotal FROM OINV T0 WHERE T0.DocDate >= @FromDate | SELECT T0."DocNum", T0."DocTotal" FROM OINV T0 WHERE T0."DocDate" >= [%0] | Results after the prompt | Lets you reuse one value in subqueries, CTEs and UNIONs on SQL Server. |
Parameter with a SQL view | /* SELECT FROM [dbo].[OCRD] T0 */ DECLARE @Card AS NVARCHAR(50) /* WHERE */ SET @Card = /* T0.CardCode */ '[%0]' SELECT * FROM dbo.ZV_OPEN_ITEMS V WHERE V.CardCode = @Card | SELECT * FROM "ZV_OPEN_ITEMS" V WHERE V."CardCode" = [%0] | A BP prompt | On SQL Server the comment header points B1 at a real table so the prompt works for a view. ZV_OPEN_ITEMS is an example name. |
Stored procedure | /* SELECT FROM [dbo].[OINV] T0 */ DECLARE @d AS DATETIME /* WHERE */ SET @d = /* T0.DocDate */ '[%0]' EXEC dbo.Z_SALES_BY_DAY @d | CALL "Z_SALES_BY_DAY" ([%0]) | A date prompt, then the procedure result | Procedure names are examples. The procedure must return one result set. |
Optional parameter | WHERE (T0.SlpCode = @Slp OR @Slp = -1) with the DECLARE pattern | Not supported the same way; use separate saved queries | A prompt the user may fill with -1 for all | B1 prompts are mandatory by default; use a sentinel value. |
FMS and alerts | $[$38.1.0] or $[OINV.CardCode] in formatted searches | Same $[...] syntax | No prompt; the value is taken from the open form | Query Manager uses [%0]; formatted searches use $[...]. Do not mix them. |
Saved query table | SELECT IntrnalKey, QCategory, QName, QString FROM OUQR | SELECT "IntrnalKey", "QCategory", "QName", "QString" FROM OUQR | List of all saved queries with their SQL | QString holds the full SQL text. Use it to search queries that use a table or column. |
Query category table | SELECT CategoryId, CatName FROM OQCN | SELECT "CategoryId", "CatName" FROM OQCN | Query Manager folders | Join OUQR.QCategory = OQCN.CategoryId. |
Find queries by table | SELECT T0.QName, T1.CatName FROM OUQR T0 JOIN OQCN T1 ON T1.CategoryId = T0.QCategory WHERE T0.QString LIKE '%OITW%' | SELECT T0."QName", T1."CatName" FROM OUQR T0 JOIN OQCN T1 ON T1."CategoryId" = T0."QCategory" WHERE T0."QString" LIKE '%OITW%' | Every saved query that reads OITW | Useful before an upgrade or a table change. |
Authorization by category | Not SQL: categories are assigned to authorization groups | Same | Users see only the categories their group allows | Set the group per category in Query Manager (Manage Categories) and grant the groups in General Authorizations. |
Read-only rule | SELECT only | SELECT only | Do not run INSERT, UPDATE or DELETE | Direct writes to B1 tables are not supported by SAP; use the DI API or Service Layer. |
Patterns follow SAP Business One 10.0 Query Manager and Query Generator behaviour and common SAP Community usage. The DECLARE pattern is a SQL Server technique; test it on your version. Object names starting with Z_ or ZV_ are examples.
Get a parameter query built
Describe the report and prompts you need and whether you run SQL Server or HANA, and we will send a saved query ready to paste.
Optional. The tool and every export work without this. No spam.
How parameters work
In Query Manager and Query Generator, [%0], [%1] and so on are placeholders. When the query runs, B1 looks at the column each placeholder is compared to, shows a prompt labelled with that field's description, offers a choose-from-list for known key columns and a date picker for dates, then substitutes the value into the SQL. The same number used twice is asked once. Because B1 needs a real column to build the prompt, the parameter must sit in a comparison with a table column in the FROM clause, or, on SQL Server, in the comment pattern that names one.
- Compare to a real column: T0.DocDate >= [%0].
- Number prompts in the order you want them asked.
- Formatted searches use $[...] instead of [%0].
The DECLARE pattern on SQL Server
When you need a value in a variable, for a CTE, a UNION, a view or a procedure, write a comment header naming a real table, for example /* SELECT FROM [dbo].[OINV] T0 */, then DECLARE @FromDate AS DATETIME, then /* WHERE */, then SET @FromDate = /* T0.DocDate */ '[%0]'. The comments tell B1 which field the prompt belongs to so it shows Posting Date and a date picker, while SQL Server ignores them. After that, use @FromDate anywhere in the real query. On HANA saved queries this pattern is not available, so compare [%0] directly with the column.
Worked example: invoices for a customer and date range
On SQL Server: SELECT T0.DocNum, T0.DocDate, T0.DocTotal FROM OINV T0 WHERE T0.CardCode = [%0] AND T0.DocDate BETWEEN [%1] AND [%2] ORDER BY T0.DocDate. The user gets a business partner prompt with a choose-from-list and two date pickers. On HANA the same query quotes the columns: T0."CardCode" = [%0] and T0."DocDate" BETWEEN [%1] AND [%2]. Save it in a category and set the category's authorization group so only the sales team sees it.
Where queries are stored
Saved queries are in table OUQR: IntrnalKey is the key, QName the name, QCategory the category and QString the full SQL text. Categories are in OQCN with CategoryId and CatName. Search QString with LIKE to find every query that reads a table before an upgrade or a UDF change. Treat both tables as read-only: create and change queries in Query Manager or through the DI API, not with SQL updates, which SAP does not support.
Frequently Asked Questions
How do I add a parameter to an SAP Business One query?
Put [%0] where the value goes and compare it to a real column, for example WHERE T0.DocDate >= [%0]. When the query runs, B1 shows a prompt labelled with the field name. Use [%1], [%2] for more prompts. The same number used twice is asked once.
Which table stores all saved queries in SAP B1?
OUQR. QName is the query name, QCategory the category and QString the SQL text. Categories are in OQCN (CategoryId, CatName). Join OUQR.QCategory = OQCN.CategoryId to list queries by folder.
How do I use a variable with a SQL view in a B1 query?
On SQL Server use the comment pattern: /* SELECT FROM [dbo].[OCRD] T0 */, DECLARE @Card, /* WHERE */, SET @Card = /* T0.CardCode */ '[%0]', then SELECT from the view with WHERE CardCode = @Card. On HANA compare [%0] directly with the view column.
Why does my parameter not show a choose-from-list?
B1 only offers a list when it can tie the parameter to a known column, such as CardCode or ItemCode in a table in the FROM clause. If the parameter is in a variable without the comment pattern, or compared to an expression, you get a plain text prompt.
If your users want to ask SAP Business One questions in plain English instead of building parameter queries, Netray can run that assistant on your own server.
Related Tools
SAP B1 Item, Warehouse and Batch Columns Reference (OITM, OITW, OITL, OBTN)
Column-level reference for B1 item, stock, price list and batch/serial tables, with the meaning of each column, example values and SQL Server vs HANA notes.
Developer ToolsSAP B1 SQL Query Generator (OITM, ORDR, RDR1)
Fill in a few filters and get a read-only SQL query over OITM, ORDR and RDR1 to paste into the SAP B1 Query Manager.
SAPSAP Business One Database Tables Reference
Searchable lookup of the core SAP Business One tables, with the matching row table and what each one stores.
SAPSAP Business One Document Object Type Codes
Lookup of ObjType numbers used in B1 queries, UDO links and approval templates, with header and row tables.
More SAP tools
See all- SAP B1 User Permission Matrix Worksheet
- SAP B1 Period End Close Checklist
- SAP B1 Approval Procedure Template
- SAP B1 Business Partner Master Data Request Form
- SAP Business One User Acceptance Test Script Template
- SAP Business One Data Migration Mapping Worksheet
- SAP Business One User-Defined Fields and Tables Register
- SAP Business One User-Defined Field Planning Worksheet
- SAP Business One Private AI Layer Blueprint Generator
Keep exploring
Free tools
- SAP Business One AR Aging SQL Query Generator (SQL Server and HANA)Generate a customer receivables aging query for SAP Business One Query Manager, with your own bucket days and aging basis, in SQL Server and HANA syntax.
- SAP Business One T-SQL to HANA SQL Query Conversion ReferenceSide-by-side lookup of the T-SQL constructs used in SAP Business One queries and formatted searches, their HANA SQL equivalents, a B1 example and the gotcha that breaks the migrated query.
- SAP Business One Database Tables ReferenceSearchable lookup of the core SAP Business One tables, with the matching row table and what each one stores.
ERP fixes and how-tos
- Private AI for SAP Business One: Service Layer, a read replica and a local modelAdd a private AI assistant to SAP Business One: Service Layer reads, SQL Server or HANA replica, document index, local LLM, permissions and approvals.
- SAP Business One Query: Released Production Orders with Component ShortagesReady-to-run SAP Business One queries on OWOR, WOR1 and OITW to list released production orders with missing components, for SQL Server and HANA.
- SAP Business One tables list: the core tables and how the names workA practical list of core SAP Business One tables (OITM, OCRD, ORDR, OPOR, OWOR, OINM) with prefix conventions and safe query advice for reports.
Runs with your ERP
- i95Dev Connect for Adobe Commerce and SAP Business One: store syncHow the i95Dev connector syncs orders, customers, products and inventory between Adobe Commerce (Magento) and SAP Business One on SQL or HANA, and what breaks.
- TrueCommerce EDI with SAP Business OneHow the TrueCommerce EDI add-on automates sales orders, 940/945 warehouse documents, ASNs and invoices in SAP Business One, with setup, pitfalls and AI.
- Spreadsheet Server with SAP Business One: live Excel reporting on inventory, sales and costHow insightsoftware Spreadsheet Server gives Excel live drill-down on SAP Business One inventory, sales and production cost data, with setup and pitfalls.
AI on your ERP
- AI for SAP Business One, Without Sending Your Books to a Public APIAdd AI to SAP Business One without sending customer data or financials to a public API. Service Layer and DI API integration, sized for an SMB budget.
- AI for SAP Business One Purchasing, From Purchase Request to A/P InvoiceGround AI in SAP Business One purchasing: purchase requests, POs, goods receipts and A/P invoices via Service Layer, read-only, on-prem or private cloud.
Integrations
- How to connect Power BI to SAP Business OneConnect Power BI to SAP Business One: SQL Server or HANA direct, Service Layer OData, gateway setup, key manufacturing dashboards and natural-language Q&A.
- How to build an MCP server for SAP Business OneBuild a Model Context Protocol server for SAP Business One: tools to expose, Service Layer auth, stdio versus HTTP, permission scoping and audit logging.