I have a procedure in T-SQL from here which works perfectly when I execute it in SMSS. For example I have an animals table, and if I want to search for all columns that have the word “cat” in it I would do this:
EXEC SearchTable 'animals' 'cat'
That works perfectly, as I said. But when I execute the same thing from java, like so:
Statement statement = ProjectServer.dbConnection.createStatement();
String sql = "EXEC SearchTable '" + tblname + "' '" + searchTerm + "'";
System.out.println(sql);
ResultSet results = statement.executeQuery(sql);
and tblname = animals and searchTerm = cat, it prints out this:
EXEC SearchTable 'animals' 'cat'
which is exactly what I do in SMSS. But I get this exception:
java.sql.SQLException: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near 'cat'.
I have no idea what’s going on. I’m using JDK 7 with NetBeans 7.1.2 and SQL Server 2008.
You are missing a comma, aren’t you?
The SQL should be EXEC SearchTable ‘animals’, ‘cat’