# How to execute syntax sql file in scala

**URL:** <https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043>\
**Category:** Question\
**Created:** [December 21, 2021, 9:40am UTC](https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043 "2021-12-21T09:40:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ian/32/2117_2.png) [@ian](https://users.scala-lang.org/u/ian)\
**Post date:** [December 21, 2021, 9:40am UTC](https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043/1 "2021-12-21T09:40:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ian](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ian/32/2117_2.png) [@ian](https://users.scala-lang.org/u/ian)\
**Post date:** [December 21, 2021, 11:23am UTC](https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043/2 "2021-12-21T11:23:33Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![crater2150](https://users.scala-lang.org/user_avatar/users.scala-lang.org/crater2150/32/1011_2.png) [@crater2150](https://users.scala-lang.org/u/crater2150)\
**Post date:** [December 21, 2021, 11:40am UTC](https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043/3 "2021-12-21T11:40:17Z")

</div>

> [@ian](#):
>
> `Source.fromFile( **myQuery.sql** ).getLines.mkString`

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:

```auto
Source.fromFile( **myQuery.sql** ).getLines.mkString(" ")

```

---

<div class="post-metadata">

**Author:** ![ian](https://users.scala-lang.org/user_avatar/users.scala-lang.org/ian/32/2117_2.png) [@ian](https://users.scala-lang.org/u/ian)\
**Post date:** [December 21, 2021, 1:10pm UTC](https://users.scala-lang.org/t/how-to-execute-syntax-sql-file-in-scala/8043/4 "2021-12-21T13:10:56Z")

</div>

> [@crater2150](#):
>
> es from the file are concatenated into

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

Thank you 🙂
