java and jboss, with SQL server. Mulitple insert statements in a native query using entitymanager, not working; but does work in sql

StackOverflow https://stackoverflow.com/questions/3812529

Question

String myQuery1  = "insert into mytable(mycol) values(myval) \ngo";
String myQuery2  = "insert into mytable(mycol) values(myval2) \ngo";
String myQuery = myQuery1 + myQuery2;

Query query = myEntityManager.createNativeQuery(myQuery);
List<?> insertResultList = queryInsertDefaults.getResultList();

using the eclpise debugger I can see the string used, it works fine when I copy and paste into sql server management studio - so I am guessing there is something to do with entity manger that doesn't like multiple line statements/go ... ?

Any advice gratefully recieved (yeah I know about stringbuilder etc etc), and the error I get is :

 SQL Error: 102, SQLState: S0001
 Incorrect syntax near 'go'.

EDIT turns out insert is not supported by entitymanager and query class. So I have to use either a prepared statement or persist the object.

Was it helpful?

Solution

From the MSSQL documentation: "GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor."

That's why it works in SSMS but not when sent 'directly' to the database. Just remove it from your INSERT statements completely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top