如何使用JDBC执行一组相关的SQL指令? [英] How to execute a set of related SQL instructions with JDBC?
本文介绍了如何使用JDBC执行一组相关的SQL指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过JDBC(它是SQL Server)执行此脚本:
I'm trying to execute this script through JDBC (it's SQL Server):
DECLARE @var VARCHAR(10)
SET @var = "test"
INSERT INTO foo (name) VALUES (@var)
这只是一个例子,在我的业务案例中,我在一个脚本中收集了大量SQL语句.
It's just an example, in my business case I have a big collection of SQL statements in one script.
我正在尝试:
final Statement stmt = connection.createStatement();
for (String sql : lines) {
stmt.executeUpdate(sql);
}
stmt.close();
SQL Server在第二行说:
SQL Server is saying on the second line:
java.sql.SQLException: Must declare the scalar variable "@var".
好像我做错了事...
Looks like I'm doing something wrong...
推荐答案
您一次要执行一行,所以第二行当然会失败,因为它取决于第一行.为什么不立即执行所有操作呢?...
You're executing it one line at a time, so of course the second line fails as it depends on the first line. Why not execute it all at once?...
这篇关于如何使用JDBC执行一组相关的SQL指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文