【问题标题】:How to get OUTPUT parameter of stored procedure sp_setapprole in SQLServer如何在 SQL Server 中获取存储过程 sp_setapprole 的 OUTPUT 参数
【发布时间】:2013-12-13 08:18:20
【问题描述】:

我正在使用以下 JDBC 驱动程序(来自 Microsoft 的驱动程序):http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

我想检索存储过程sp_setapprole 的输出参数(@cookie)。我可以像这样调用该过程,但我不知道如何检索 OUTPUT 参数。

statement = connection.createStatement();
statement.execute("EXEC sp_setapprole '" + applicationRoleName + "', '" + applicationRolePassword + "'");

我尝试使用 PreparedStatement 和 CallableStatement,但总是得到以下异常:Stored procedure 'sys.sp_setapprole' can only be executed at the ad hoc level.。我找到了这个帖子:https://stackoverflow.com/a/6944693/1362049,但我不喜欢这个解决方案(使用另一个 JDBC 驱动程序)。

所以我的问题是:如何使用 Statement 从 SQLServer 中的存储过程 sp_setapprole 获取 OUTPUT 参数。

【问题讨论】:

    标签: java sql sql-server stored-procedures


    【解决方案1】:

    我认为这会有所帮助。我不是一个大型 Java 程序,但多年来一直使用 ODBC。

    http://www.tutorialspoint.com/jdbc/jdbc-statements.htm

    查看可调用语句。您需要定义输入/输出或输入参数。执行 SP 并读取输出。

    在 C# 中,它只是查看如下参数为您提供的值。但是建立连接、绑定参数、进行调用和读取输出的想法是一样的。

    my_Cmd.Parameters["@PageCount"].Value.ToString();

    这是一篇来自 MSDN 的 C++ 文章。

    http://technet.microsoft.com/en-us/library/ms403283.aspx

    同样的想法,绑定参数,进行调用,使用 [SQLExecDirect] 读取输出

    如果你不明白,请给我打电话。

    【讨论】:

    • 我无法使用 CallableStatement(或 PreparedStatement),因为我收到错误 Stored procedure 'sys.sp_setapprole' can only be executed at the ad hoc level.。请参阅我的第一篇文章。
    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    相关资源
    最近更新 更多