SAP B1 SQL Query Generator: Items (OITM) and Sales Orders (ORDR)
Most day-to-day SAP Business One reporting questions are a join between a header table and its lines: items with stock, sales orders with open quantity. This generator produces three tested patterns that you can paste into the Query Manager or a Crystal Reports data source.
Fill in
Numeric group code from OITB.
1. Stock on hand by item group (OITM)
SELECT TOP 100 T0.ItemCode, T0.ItemName, T0.OnHand, T0.IsCommited AS Committed, T0.OnOrder, (T0.OnHand - T0.IsCommited + T0.OnOrder) AS Available, T0.AvgPrice FROM OITM T0 WHERE T0.ItmsGrpCod = 100 AND T0.OnHand >= 0 AND T0.validFor = 'Y' ORDER BY T0.ItemCode;
2. Open sales order lines for one customer (ORDR, RDR1)
SELECT T0.DocNum, T0.DocDate, T0.DocDueDate, T0.CardCode, T0.CardName, T1.LineNum, T1.ItemCode, T1.Dscription, T1.Quantity, T1.OpenQty, T1.LineTotal FROM ORDR T0 INNER JOIN RDR1 T1 ON T1.DocEntry = T0.DocEntry WHERE T0.CardCode = 'C20000' AND T0.DocDate >= '2026-01-01' AND T0.DocDate <= '2026-12-31' AND T0.DocStatus = 'O' AND T0.CANCELED = 'N' AND T1.LineStatus = 'O' ORDER BY T0.DocDueDate, T0.DocNum, T1.LineNum;
3. Open demand against stock, by item
SELECT T1.ItemCode, T2.ItemName, SUM(T1.OpenQty) AS OpenDemand, MAX(T2.OnHand) AS OnHand FROM ORDR T0 INNER JOIN RDR1 T1 ON T1.DocEntry = T0.DocEntry INNER JOIN OITM T2 ON T2.ItemCode = T1.ItemCode WHERE T0.DocStatus = 'O' AND T0.CANCELED = 'N' AND T1.LineStatus = 'O' AND T0.DocDate >= '2026-01-01' AND T0.DocDate <= '2026-12-31' GROUP BY T1.ItemCode, T2.ItemName HAVING SUM(T1.OpenQty) > MAX(T2.OnHand) ORDER BY SUM(T1.OpenQty) DESC;
Written for SQL Server editions of SAP Business One using T-SQL (TOP). On SAP HANA, replace TOP n with LIMIT n at the end, and wrap table and column names in double quotes, for example "OITM"."ItemCode". Table and column names above are standard B1 names, but check them in your version through the Query Generator tree or the DI API reference. Run queries as read-only reports and test on a non-production company first.
Tired of hand-writing B1 queries?
We can show how a read-only natural language layer answers B1 questions and displays the SQL it ran.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter the item group, date range and customer you need. The three code blocks update with your values. Copy one into Tools, Queries, Query Manager (or Query Generator), save it and run it. For a user query that others will run, give it a clear name and put it in a query category. Replace the example group code with a real ItmsGrpCod, which you can read from table OITB. Keep the SELECT only. Writing to B1 tables with SQL bypasses business logic and is unsupported.
The tables behind the queries
OITM is the item master. OnHand is total stock across warehouses, IsCommited is quantity committed to sales orders and production, and OnOrder is on open purchase orders and production receipts. Available to promise is often shown as OnHand minus IsCommited plus OnOrder. ORDR is the sales order header and RDR1 holds its lines. DocStatus O means open and C closed on the header, and LineStatus does the same per line. Always filter out CANCELED = 'Y' orders to avoid double counting.
- OITW holds stock per warehouse if you need it split
- OCRD is the business partner master, joined on CardCode
- OITB is the item groups table
- ODLN and DLN1 hold deliveries, OINV and INV1 hold invoices
Reading the third query
The demand against stock query groups open sales order lines by item and compares total open quantity with on-hand. Items that show demand above stock are candidates for a purchase or production order. It is a quick shortage list, not MRP: it ignores incoming supply, lead times and substitutions. For a proper plan use the Material Requirements Planning wizard, which creates recommendations you can convert into documents.
Common mistakes
A few errors account for most wrong numbers in home-made B1 queries. Check each before you trust a result.
- Joining on DocNum instead of DocEntry
- Forgetting that DocNum can repeat across document series
- Summing LineTotal across currencies without using the local currency columns
- Ignoring cancelled documents and their cancellation documents
- Using Quantity when you need OpenQty
Frequently Asked Questions
Where do I run these queries in SAP Business One?
Open Tools, Queries, Query Manager or Query Generator, paste the SQL and run it. You can save it as a user query, attach it to a field as a formatted search or use it as the data source for a report. Use a test company first.
Do these work on SAP HANA?
The logic does, the syntax needs a small change. Replace TOP n with LIMIT n, and wrap object names in double quotes. Date literals and string quoting follow HANA SQL rules. The notes under the tool list the main differences.
What does IsCommited mean in OITM?
IsCommited is the quantity committed to open sales orders and, in many set-ups, production order components. The spelling is as it appears in the B1 table. Subtract it from OnHand to see stock not already promised.
Can I update data with SQL in B1?
You should not. Direct updates skip B1 business logic, authorisations and audit trails, and can break support. Use the DI API, DTW or the Service Layer for changes, and keep SQL queries read-only.
If your team writes the same B1 queries every week, we can build a natural language layer that generates and shows the SQL read-only, with B1 roles respected. Ask us about it.
Related Tools
SAP B1 Item Master Import Template (DTW)
An editable item master worksheet with the fields most B1 implementations load through DTW, ready to export to Excel or CSV.
SAPSAP B1 MRP Setup Readiness Assessment
A 10-question readiness score for running the SAP Business One MRP wizard, with the data fixes to make first.
SAPSAP Business One Production Order Checklist
A step-by-step checklist for creating, releasing, issuing to, receiving from and closing a production order in SAP Business One.
SAPSAP B1 User Permission Matrix Worksheet
An editable role-by-module matrix to plan SAP Business One authorizations, with a score for roles that mix duties.
More Developer Tools tools
See all- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
- ABAP Unit Test Class Generator
New to SAP Business One? Start with the SAP Business One guide and toolkit.
Keep exploring
Free 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.
- 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 B1 Implementation Cost CalculatorEnter users, rates and effort days to see first-year and three-year SAP Business One cost, using your own partner quotes.
ERP fixes and how-tos
- 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.
- 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 Financial Reports and Reporting FeaturesStandard SAP Business One financial reports (trial balance, P&L, balance sheet, aging), plus Query Manager, Crystal layouts and dashboards and when to use each.
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 Inventory: Stock, Batches, Bins and ValuationPrivate AI over SAP Business One inventory: OITW stock, batches, serials, bins and valuation. Answers grounded in live data via Service Layer or SQL views.
Integrations
- How to add AI agents to SAP Business OneHow to put AI agents on SAP Business One: Service Layer tool design, read-only first, draft documents for human approval, and an on-prem model option.
- 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.