question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQL syntax error occurs by typeorm insert of multiple rows into table for sap (HANA) database

See original GitHub issue

Issue Description

Expected Behavior

Two new records have been created in table "<HANA_SCHEMA>"."photo".

Actual Behavior

An error occurs at typeorm database operation. No records have been created on database. Server console output:

[Nest] 1032  - 07/13/2021, 8:27:38 AM   ERROR [ExceptionsHandler] sql syntax error: incorrect syntax near ",": line 1 col 48 (at pos 48)
Error: sql syntax error: incorrect syntax near ",": line 1 col 48 (at pos 48)
    at SapQueryRunner.<anonymous> (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/src/driver/sap/SapQueryRunner.ts:193:53)
    at step (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:143:27)
    at Object.next (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:124:57)
    at fulfilled (/home/niels/sap/issues/typeorm-hana-insert-multiple-rows/node_modules/tslib/tslib.js:114:62)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Steps to Reproduce

Prepare:

  1. Clone repository https://github.com/niels-schmitt/typeorm-hana-insert-multiple-rows.
  2. npm install
  3. Get a running HANA instance, and credentials for user <HANA_USER> with authorization CREATE ANY on schema <HANA_SCHEMA>.
  4. cp template.env .env
  5. In file .env enter values for: HANA_HOST, HANA_PORT, HANA_USER, HANA_PASSWORD, HANA_SCHEMA

Reproduce Issue:

npm run start
curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower","Tree"]' localhost:3000/photos

or

curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower","Tree"]' localhost:3000/photosByQueryBuilder

My Environment

Dependency Version
Operating System Ubuntu 20.04.2
Node.js version v14.17.2
Typescript version @4.3.5
TypeORM version @0.2.34
@nestjs/core @8.0.2
@nestjs/typeorm @8.0.1
@sap/hana-client @2.9.23
hdb-pool @0.1.6

Additional Context

Root cause seems to be that an incorrect SQL is created:

INSERT INTO "photo"("id", "name") VALUES (?, ?), (?, ?)

Correct SQL would be:

INSERT INTO "photo"("id", "name") VALUES ((?, ?), (?, ?))

The executed SQL is logged to the console in case of POST for /photosByQueryBuilder.

POST for /photos executes

await this.photoRepository.manager.insert(Photo, photos);

POST for /photosByQueryBuilder executes

await this.photoRepository
  .createQueryBuilder()
  .insert()
  .into(Photo)
  .values(photos)
  .execute();

The issues occurs for both variants.

The issue does not occur in case only one record is inserted:

curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Flower"]' localhost:3000/photos

or

curl -w'\n' -X POST -H 'Content-Type: application/json' -d '["Tree"]' localhost:3000/photosByQueryBuilder

For verification, read all records by

curl -w'\n' localhost:3000/photos

The issue does not occur in case a postgres database is connected.

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Thanks for your help & kind regards Niels Schmitt

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
imnotjamescommented, Jul 15, 2021

We do something similar for Oracle.

I think that’s possible for Hana.

0reactions
niels-schmittcommented, Oct 12, 2021

Hi James, I have tested draft PR https://github.com/typeorm/typeorm/pull/7957 in my local setup. It looks quite promising, though in case of multiple rows there still exists an SQL-syntax error: The VALUES keyword must be removed for that case. E.g. the SQL query generated by your draft PR looks as follows:

INSERT INTO "photo"("id", "name") VALUES
SELECT '1993a3eb-6664-4676-b759-304c9cac7201', 'Flower' FROM dummy UNION ALL
SELECT '1993a3eb-6664-4676-b759-304c9cac7203', 'Forest' FROM dummy;

The correct SQL would be:

INSERT INTO "photo"("id", "name")
SELECT '1993a3eb-6664-4676-b759-304c9cac7201', 'Flower' FROM dummy UNION ALL
SELECT '1993a3eb-6664-4676-b759-304c9cac7203', 'Forest' FROM dummy;

Therefore, in src/query-builder/InsertQueryBuilder.ts, starting from line 341

        // add VALUES expression
        if (valuesExpression) {
            if (this.connection.driver instanceof OracleDriver && this.getValueSets().length > 1) {
                query += ` ${valuesExpression}`;
            } else {
                query += ` VALUES ${valuesExpression}`;
            }

line 343 should be modified as follows:

        // add VALUES expression
        if (valuesExpression) {
            if ((this.connection.driver instanceof OracleDriver || this.connection.driver instanceof SapDriver ) && this.getValueSets().length > 1) {
                query += ` ${valuesExpression}`;
            } else {
                query += ` VALUES ${valuesExpression}`;
            }

Kind regards, Niels

Read more comments on GitHub >

github_iconTop Results From Across the Web

SAP HANA Driver wrongly escapes the entity / table name
A Database syntax error is thrown and looks like this: query: INSERT INTO "foo"."bar"."bak::XZY" { code: 257, message: 'sql syntax error: ...
Read more >
Inserting Multiple Rows of Data With One Insert in HANA
Symptom. When inserting multiple rows of data with one insert statement by simply comma separating the VALUES part of the statement, it returns...
Read more >
Multiple Row Insert into HANA - SAP Community
I'm trying to insert more than a single row into a table I have in HANA. insert into TABLE ("NAME", "VALUE") values ('hi',...
Read more >
HANA - Insert Multiple rows of data - SAP Community
I am pulling data from system B(Source) into System A (Target). HANA Native SQL Reading data from Calculation View in System B.
Read more >
Insert all default values - SAP Community
Hi, I'm trying to add SAP Hana database support to my ORM. There are cases when entity / table is defined with all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found