开发者

How to execute a set of related SQL instructions with JDBC?

开发者 https://www.devze.com 2023-03-09 08:07 出处:网络
I\'m trying to execute this script through JDBC (it\'s SQL Server): DECLARE @var VARCHAR(10) SET @var = \"test\"

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)

It's just an example, in my business case I have a big collection of SQL statements in one script.

I'm trying this:

f开发者_运维技巧inal Statement stmt = connection.createStatement();
for (String sql : lines) {
  stmt.executeUpdate(sql);
}
stmt.close();

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

0

精彩评论

暂无评论...
验证码 换一张
取 消