SAP B1 Service Layer SQLQueries: how to run SQL and fix errors 701, 703, 402 and -2028
By Zain Abbas, Kodenova · Updated
Why SQLQueries exists
The Service Layer exposes business objects such as Orders and Invoices. That is fine for creating documents, but reporting needs joins and grouping across many rows. Pulling every invoice line through OData and grouping it in your app is slow.
SQLQueries solves this. You save a SELECT statement in SAP Business One once, then run it over HTTPS whenever you need it. We use it in the Kodenova SAP B1 dashboard to read a year of sales in a few calls.
Step 1: Save the query
POST https://your-b1-server:50000/b1s/v1/SQLQueries
Content-Type: application/json
{
"SqlCode": "KN_SALES_01",
"SqlName": "Sales lines since a date",
"SqlText": "SELECT T0.\"DocEntry\", T0.\"DocDate\", T0.\"CardCode\", T1.\"ItemCode\", T1.\"LineTotal\" FROM OINV T0 INNER JOIN INV1 T1 ON T0.\"DocEntry\" = T1.\"DocEntry\" WHERE T0.\"CANCELED\" = 'N' AND T0.\"DocDate\" >= :dfrom"
}
A successful call returns 201 Created. SqlCode is the key you call later, so keep it short and stable.
Step 2: Run it with List
POST https://your-b1-server:50000/b1s/v1/SQLQueries('KN_SALES_01')/List
Content-Type: application/json
{ "ParamList": "dfrom='2025-01-01'" }
The response has a value array with your rows. Large results come in pages: follow odata.nextLink (v1) or @odata.nextLink (v2) until it disappears.
Several parameters are joined with &:
{ "ParamList": "dfrom='2025-01-01'&card='C0001'" }
Error 701: invalid SQL syntax
A typical message looks like this:
[701] Invalid SQL syntax:, line 1, character position 280,
no viable alternative at input 'T0."DocDate" >= :from'
This is the one that cost us an afternoon. The SQL was valid, but the parameter was called :from. The parser read it as the FROM keyword. Renaming it to :dfrom fixed it at once.
Other causes of 701:
- Using SQL Server syntax on a HANA company, or the other way round. Double-quote identifiers and match the database dialect.
- Expressions the allow-list does not accept, such as some
CASEor function calls. Select raw columns and calculate in your app. - A trailing semicolon or a comment inside
SqlText.
Error -2028: no matching records found
You call SQLQueries('KN_SALES_01')/List and get -2028. The query is not in that company. Common reasons:
- The company database was restored or copied without your saved queries.
- Someone deleted the query, or another app used the same
SqlCode. - You logged in to a different company than the one you saved the query in.
A robust integration handles this itself: on -2028, POST the query again, then retry List once. For -2028 and the other common codes outside SQLQueries, see SAP B1 DI API error codes. We name codes with a short hash of the SQL text, so a changed query gets a new code and never runs stale SQL.
Allow-list and permissions
SQLQueries does not run any SQL on any table. It checks the statement against an allow-list on the Service Layer server. If a table is not allowed, the call fails even though the SQL is correct. Ask your SAP administrator to review the allow-list before you design the report around a blocked table.
The user also needs two authorizations:
- Modify SQL Queries in Service Layer: to create, change and delete queries.
- Service Layer SQL Query: to execute a specific query.
Other SQLQueries errors: 703, 402 and unsupported functions
Error 703: column does not exist (alias in ORDER BY)
A query that sums RDR1."Quantity" AS Q and ends with ORDER BY Q DESC returns:
703 Column 'Q' from table 'RDR1' not exist
This was reported on SAP B1 10.0 SP2402, also for simpler queries: SQLQueries reads the alias in ORDER BY as a column of the table. We have not found an SAP-confirmed fix, so do not order by an alias. Sort the rows in your application instead.
Error 402: bad configuration in file b1s_sqltable.conf
b1s_sqltable.conf is the table allow-list of SQLQueries, and error 402 means the file is damaged. On Windows it is in %ProgramFiles%\SAP\SAP Business One ServerTools\ServiceLayer\Conf\, on Linux in /usr/sap/SAPBusinessOne/ServiceLayer/conf/. The fix given on SAP Community, citing SAP Note 3064927:
- Rename
b1s_sqltable.conftob1s_sqltable_backup.conf. - Open
b1s_sqltable.template.confand save it asb1s_sqltable.conf. - Restart the Service Layer.
- Add the extra tables you need to the table list again.
Unsupported expressions and functions (701)
SQLQueries accepts a limited subset of SQL. SAP’s introduction blog lists joins, GROUP BY, ORDER BY, LIKE and functions such as sum, avg, max, min, left, right, distinct, count, lower, upper, isnull and ifnull. Things outside that subset fail with 701:
- UNION / UNION ALL:
Incorrect syntax near 'union'. Run two queries and merge the rows in your app. - Arithmetic in the select list:
mismatched input '-' expecting FROM. Select the raw columns and calculate in your app. - Other functions:
Cannot support this function or expression: 'DAYS_BETWEEN'.
UDT and UDO tables are supported from 10.0 FP 2102. Log tables and SBO-COMMON are not.
Error 502 proxy error on List
A 502 from SQLQueries List is not always about size. In one SAP Community case the cause was Cyrillic text in the result, documented in SAP Note 3029055 and fixed in a later feature pack. For big results, always page them with the Prefer: odata.maxpagesize header, as shown in server-driven paging in our Service Layer guide.
Checklist
- Keep
SqlTextsimple: raw columns, joins, WHERE and GROUP BY. - Never name a parameter after a SQL keyword.
- Handle -2028 by recreating and retrying once.
- No UNION, arithmetic or ORDER BY alias in SqlText (701 / 703); on 402, rebuild b1s_sqltable.conf from the template.
- Follow nextLink until all pages are read.
- Reconcile totals with SAP once. Line totals ignore the document discount, which we explain in why SAP B1 sales reports don’t match.
Need a report or an integration built on SQLQueries? See our Service Layer guide or contact us.
Sources
- SAP Learning: Managing SQL Queries Using Service Layer API
- SAP Community: New SAP Business One Service Layer SQL Query
- SAP Community: Service Layer SQL Query 502 proxy error when retrieving
- SAP Community: Can't use alias in a Service Layer SQLQueries ORDER BY clause (error 703)
- SAP Community: SQL queries with Service Layer returns error code 402
- SAP Community: Incorrect syntax near UNION (SQLQueries)
Related services
Frequently asked questions
Why does :from fail but :dfrom works?
In our tests the SQL parser treated :from as the FROM keyword and returned error 701, 'no viable alternative at input'. Renaming the parameter to :dfrom fixed it. Avoid parameter names that match SQL keywords, such as from, to, date, order or select.
What does error -2028 mean on SQLQueries List?
It means 'No matching records found': the SqlCode you called does not exist in that company. This happens after a database restore, a company copy, or when another tool deleted the query. Recreate the query and call List again.
Which tables can SQLQueries read?
Only tables on the Service Layer's allow-list. Common document and master tables are usually allowed, while some system and finance tables may be blocked. If a query returns a permission error for a table, check the allow-list configuration on the Service Layer server.
Can I use CASE, CAST or date functions?
SQLQueries accepts a limited subset of SQL. In projects we have worked on, expressions such as CASE and some functions were rejected. Keep the SQL simple, select raw columns, and do calculations in your application.
What permissions does the user need?
To create or change queries the user needs Full Authorization for 'Modify SQL Queries in Service Layer'. To run a query the user needs authorization for that query under 'Service Layer SQL Query'.
What does error 402 Bad configuration in file b1s_sqltable.conf mean?
The table allow-list file of the Service Layer is damaged. In the Service Layer Conf folder, rename b1s_sqltable.conf as a backup, save b1s_sqltable.template.conf as a new b1s_sqltable.conf, restart the Service Layer and add the extra tables you need again.
Ready to get started?
Tell us what you need. You get a clear technical answer, a fixed scope and a price, usually within 24 hours.
