How do I execute a sql script in a batch file

1,234 views
Skip to first unread message

gabjos

unread,
Jan 28, 2010, 10:19:17 PM1/28/10
to H2 Database
I need to right a batch file in windows that can execute sql commands
in my H2 database. What is the syntax for doing this?

salk31

unread,
Jan 29, 2010, 8:41:09 AM1/29/10
to H2 Database
Ive not used it but have you tried the RunScript class? If you need
something more powerful an Ant script might be handy (doing wildcard
includes, other actions)... The documentation seems to be in the
javadoc pages.

gabjos

unread,
Jan 29, 2010, 1:33:06 PM1/29/10
to H2 Database
can I execute this line in a .bat file
java org.h2.tools.RunScript -url jdbc:h2:tcp://localhost:1521/test -
user test_usr -script C:\tmp\ESS.sql > ESS.log

> > in my H2 database. What is the syntax for doing this?- Hide quoted text -
>
> - Show quoted text -

Brian

unread,
Jan 29, 2010, 4:43:22 PM1/29/10
to H2 Database
It would look something like:

java.exe -Xmx512m -classpath "h2.jar" org.h2.tools.RunScript -url
jdbc:h2:mydbname -user sa -script yourscript.sql >output.txt 2>&1

(Assuming h2.jar is in the current working directory)

Brian

unread,
Jan 29, 2010, 4:45:06 PM1/29/10
to H2 Database
Yes, your example should work if your h2 jar file is in the CLASSPATH
already

gabjos

unread,
Jan 30, 2010, 1:19:57 PM1/30/10
to H2 Database
How would I put h2 jar in the classpath?

> > user test_usr -script C:\tmp\ESS.sql > ESS.log- Hide quoted text -

gabjos

unread,
Feb 1, 2010, 12:36:44 PM2/1/10
to H2 Database
I did this and it worked

java -classpath "C:\program files\test\Load\lib\h2.jar"


org.h2.tools.RunScript -url jdbc:h2:tcp://localhost:1521/test -user

<username> -password <password> -script "C:\Documents and Settings
\Desktop\ESS.sql" > "C:\Documents and Settings\Desktop\output.txt"
2>&1

> > - Show quoted text -- Hide quoted text -

gabjos

unread,
Feb 1, 2010, 12:38:38 PM2/1/10
to H2 Database
The only problem is the output.txt file is empty. It doesn't show the
results of the sql script that is executed if there are no errors. Is
there a way to get the output from running the sql script?

Thomas Mueller

unread,
Feb 2, 2010, 1:10:20 PM2/2/10
to h2-da...@googlegroups.com
Hi,

> Is there a way to get the output from running the sql script?

See http://www.h2database.com/javadoc/org/h2/tools/RunScript.html#main_String...
specially
-showResults

Regards,
Thomas

Reply all
Reply to author
Forward
0 new messages