views:

32

answers:

1
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.

+2  A: 

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.

Pondlife
thanks, I am just about to try it out.
NimChimpsky
well it solved one problem. However, getresultlist does not work with insert statements ... and executeUpdate is only appropriate for update or delete according to the api ?
NimChimpsky