简体   繁体   English

调用SQL Server存储过程的“'{'附近的语法不正确”

[英]“Incorrect syntax near '{'” calling SQL Server stored procedure

I am trying to get results from my stored procedure and I am getting ERROR: Incorrect syntax near '{'. 我正在尝试从存储过程中获取结果,并且出现ERROR: Incorrect syntax near '{'. . I saw many examples using the same thing for a sql string and I am not sure why I am getting an error..I have the same amount of parameters... 我看到许多示例对sql string使用相同的东西,但我不确定为什么会出错..我有相同数量的参数...

when I execute my stored procedure om my database like this 当我像这样在数据库中执行存储过程时

exec rptGetAssetbyLocation '2122 ' I get my 5 columns exec rptGetAssetbyLocation '2122我得到了5列

how do I get results back using Callable Statement ? 如何使用Callable Statement返回结果? I am doing something wrong with a registerOutParameter ? 我用registerOutParameter做错了什么?

try
        {
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                                String url = "jdbc:sqlserver:...";
                                String username = "username";
                                String password = "password";

                    sb.append("<table id=\"examplee\" border=1 >" 
                    + "<tr><td>ID</td><td>TAG</td><td>NAME</td>"
                        + "<td>MODEL</td><td>SERIAL NUMBER</td></tr>");

            String query = "{exec rptGetAssetbyLocation(?,?,?,?,?)}";
      Connection conn = DriverManager.getConnection(url, username, password);
                    CallableStatement stmt = conn.prepareCall(query);
                    stmt.registerOutParameter(1, java.sql.Types.INTEGER);
                    stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(3, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(4, java.sql.Types.VARCHAR);
                    stmt.registerOutParameter(5, java.sql.Types.VARCHAR);


                                ResultSet rs = stmt.executeQuery();


            while (rs.next())
            {
                int id = rs.getInt("ID");
                String tag = rs.getString("tag");
                String name = rs.getString("name"); 
                String model = rs.getString("model"); 
                String serNum = rs.getString("serialNumber");

                sb.append("<tr><td>" + id + "</td>" 
                        + "<td>" + tag + "</td>" 
                        + "<td>" + name + "</td>" +
                        "<td>" + model + "</td>" +
                        "<td>" + serNum + "</td></tr>");
            }
            sb.append("</table>");
        }
        catch(Exception e)
        {
            sb.append("<h1>ERROR: " + e.getMessage() + "</h1>");
        }
        sb.append("</body></html>");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(sb);
    }

If all you do to run your Stored Procedure in SQL Server Management Studio (SSMS) is 如果您要做的所有在SQL Server Management Studio(SSMS)中运行存储过程的操作,

exec rptGetAssetbyLocation '2122' 

then you probably don't need to mess with output parameters, you can just use the one input parameter to call your Stored Procedure and then retrieve the results from the ResultSet like this 那么您可能不需要弄乱输出参数,只需使用一个输入参数来调用存储过程,然后像这样从ResultSet中检索结果

String query = "exec rptGetAssetbyLocation ?";
Connection conn = DriverManager.getConnection(url, username, password);
CallableStatement stmt = conn.prepareCall(query);
stmt.setString(1, "2122");
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
    int id = rs.getInt("ID");
    String tag = rs.getString("tag");
    String name = rs.getString("name"); 
    String model = rs.getString("model"); 
    String serNum = rs.getString("serialNumber");
    // ...and do other useful stuff
}

In addition to the answer by Gord Thompson , you could also use the JDBC call escape for stored procedures. 除了Gord Thompson的回答之外,您还可以将JDBC调用转义用于存储过程。

JDBC escapes are very specific escapes defined in the JDBC standard to bridge differences between SQL implementations. JDBC转义是JDBC标准中定义的非常特殊的转义,用于消除SQL实现之间的差异。 The JDBC escapes are translated to the database server specific syntax. JDBC转义被转换为数据库服务器特定的语法。

For calling stored procedures the escape is: 对于调用存储过程,转义为:

{call procname(arg1, ..,)}

or for a stored procedure with a return value: 或对于具有返回值的存储过程:

{? = procname(arg1, ...)}

Where arg1 etc is either a literal value or a parameter placeholder. 其中arg1等是文字值或参数占位符。 See the CallableStatement apidoc. 请参阅CallableStatement apidoc。 When prepared or executed, the driver will translate this to the database syntax (ie EXEC procname arg1, ... in the case of SQL server). 在准备或执行后,驱动程序会将其转换为数据库语法(例如,对于SQL Server,为EXEC procname arg1, ... )。

The syntax you tried to use in your question is a mix between a JDBC escape (the {...} ) and the SQL Server specific syntax of executing a stored procedure. 您在问题中尝试使用的语法是JDBC转义( {...} )和执行存储过程的SQL Server特定语法之间的混合。

Also the result of your stored procedure is probably not an out parameter, but a ResultSet , but this depends on the stored procedure definition. 同样,存储过程的结果可能不是out参数,而是ResultSet ,但这取决于存储过程的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM