views:

171

answers:

1

Hi, I would like to call this stored procedure with jdbc:

sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

JDBC thinks the ? is a placeholder for an argument. In fact it is used by the SP to put in the table name. How can I call the stored procedure? I tried this:

CallableStatement call = jdbcConnection.prepareCall("call sp_msforeachtable(?)");
call.setString(1, "\"ALTER TABLE ? NOCHECK CONSTRAINT all\"");
call.executeUpdate();

and I get a syntax error near '@P0'. I guess '@P0' is the ?. How can I call this SP? I am using SQL Server 2008 BTW.

+1  A: 

You say that the ? is supposed to be for a table name, so you need to provide an actual table name before calling Statement.executeUpdate(). At that point the JDBC driver will tell the database to actually run the statement, so obviously all parameters need to be bound.

Maybe you meant to write this:

CallableStatement call = jdbcConnection.prepareCall("call sp_msforeachtable(?)");
call.setString(1, "AnActualTableName");
call.executeUpdate();

or maybe you meant to write this:

CallableStatement call = jdbcConnection.prepareCall("call sp_msforeachtable(\"ALTER TABLE ? NOCHECK CONSTRAINT all\")");
call.setString(1, "AnActualTableName");
call.executeUpdate();

I'm not sure exactly what sp_msforeachtable() is supposed to do, but I do know that you have to provide values for all parameters before calling executeUpdate()

netjeff

related questions