How to execute syntax sql file in scala

When I run this code, I have error with the sql executed on file myQuery.sql

val sqlString = Source.fromFile(**myQuery.sql**).getLines.mkString
var psmt: PreparedStatement = dbc.prepareStatement(sqlString)
var result = psmt.executeQuery()
psmt.close()

myQuery.sql contains big merge query :

MERGE INTO xxxxxx e
    USING (SELECT *  from ..................)
    ON (condition.............)
  WHEN MATCHED THEN
    UPDATE SET ................
  WHEN NOT MATCHED THEN
    INSERT (.................)
    VALUES (..................)

The error is: Error Msg = ORA-00969: key word ON is missing
But in the query, I have ON key word with condition surrounded by parenthesis
When I execute the query in oracle I don’t have errors

There is any syntax , I shoud resperct il query file ?

Thank you for your help.

1 Like

The error of keyword missing in the query is interpretated by scala like :

```
MERGE INTO xxxxxx e
    USING (SELECT *  from ..................)
    ON (condition.............)
  WHEN MATCHEDTHEN
    UPDATE SET ................
```

MATCHED THEN is interpreted like MATCHEDTHEN

So how can I write the sql query in the file in order to scala interpret it as correct sql query ?

With mkString, all lines from the file are concatenated into a single string without any separator. Is your THEN at the beginning of a new line without indentation?
Try adding a separator as argument to mkString, e.g. to add a space between each pair of lines:

Source.fromFile(**myQuery.sql**).getLines.mkString(" ")
2 Likes

Your statement has resolved the problem, now the keyword are not concatenated and the query is correctelly executed.

Thank you :slight_smile: