简体   繁体   中英

How to execute 'NOT IN' clause in Execute SQL processor in nifi

I am trying to execute the following query using 'ExecuteSQL' processor in Apache nifi.

INSERT INTO SampleDB VALUES (${rno}, '${tno}', '${tval}', '${lotno}', '${datval}') WHERE ${rno} NOT IN (SELECT rno FROM SampleDB);

Here the ${rno} is obtained as flow file attribute.

The following error is obtained on execution:

ExecuteSQL[id=01781107-63a4-1204-8110-6b19db3d5ffc] Unable to execute SQL select query INSERT INTO LimsOnCloud VALUES (1, 'CTG123 ', 'ITM123 ', '123 ', '5 ') WHERE 1 NOT IN (SELECT rno FROM SampleDB); for StandardFlowFileRecord[uuid=93db20b2-5f9f-4521-ac42-11239abb94c2,claim=StandardContentClaim [resourceClaim=StandardResourceClaim[id=1615281573937-132, container=default, section=132], offset=168254, length=152],offset=0,name=098a8ad2-0dc9-4564-a242-8b4855b619b2,size=152] due to Incorrect syntax near the keyword 'WHERE'.; routing to failure: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'WHERE'.

Not able to figure out where the error lies or what is the better way to achieve this.

  • You should be using proper parameterized queries.

  • To use a WHERE like that, you need SELECT .

  • You should also specify the column names to insert into explicitly.

  • NOT IN falls down in the face of nullables, so favour NOT EXISTS instead.

INSERT INTO SampleDB
    (rno, tno, tval, lotno, datval)
SELECT @rno, @tno, @tval, @lotno, @datval
WHERE NOT EXISTS (SELECT 1
    FROM SampleDB
    WHERE rno = @rno
);

The Error is being thrown because it tries for find a column named 1 in your table, so Instead of the above, try this

INSERT INTO SampleDB SELECT ${rno}, '${tno}', '${tval}', '${lotno}', '${datval}' 
WHERE  NOT EXISTS (SELECT 1 FROM SampleDB WHERE rno = ${rno});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM