Same prepared statement, multiple databases, same

2019-07-23 13:22发布

问题:

I have multiple ms sql server databases x1, x2, x3, ... xn, effectively all the same schema type and one database y that is different and holds management data.

I am using jdbc prepared statements and there are occasions when the same prepared statement needs to be executed on multiple x databases from one program at one occasion.

If the prepared statement's SQL does not indicate a database:

Is the prepared statement associated just with the server, and if executed is executed on a database selected according to context, e.g. following a USE statement.

OR is it forever associated with the default database in the connection URL? If so, and I still want to use preprepared statements, must I create a prepared statement for every single database, even though the schemas and statements are in principal identical.

回答1:

I am pretty certain (but not 100% positive) that a prepared statement is bound to a specific database. Given that a query plan has to evaluate index and table statistics which would differ in each database.

And since this appears to be an internal system, and SQL injection may not be an issue - I would use a direct execute or stmt.executeQuery(query).