使用MS SQL驱动程序版本4,存储过程不会使用JDBC连接返回结果集

时间:2014-05-01 12:44:43

标签: sql-server tsql jdbc

由于某种原因,以下存储过程不会返回带有MS SQL驱动程序的ResultSet,但它可以与JTDS一起使用。我尝试使用预准备语句,可调用语句,输出参数等。在所有情况下,我永远不会得到一个ResultSet对象。

我期待刚刚创建/更新的表名的VARCHAR。这是SQL 2008服务器,存储过程在查询分析器中工作正常。

这些是我称之为的一些方式:

"spScheduleSearchCached ?, ?, ?, ?, ?"
"spScheduleSearchCached(?, ?, ?, ?, ?)"
"{? = call spScheduleSearchCached(?, ?, ?, ?, ?) }"
"{ call spScheduleSearchCached(?, ?, ?, ?, ?) }"

使用PreparedStatements和CallableStatements。

我尝试使用callable通过调用statement.getString(1)等直接返回结果......

有什么想法吗? TIA

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE spScheduleSearchCached (
@ProcedureList varchar(1500),
@StartDate smalldatetime,
@ReservationList varchar(1500) = NULL,
@interval int = 60,
@future int = 30,
@PatientID int = NULL )
AS
 DECLARE @tableName varchar(250)
 DECLARE @sqlCreateTable varchar(4096)
 DECLARE @sqlInsertRecords varchar(4096)

 SET @tableName = 'SEARCHRESULT_' + REPLACE (REPLACE (@PROCEDURELIST, ',', '_'), ' ', '') + '_' + convert(varchar, GETDATE(), 112) + '_' + CAST( CAST( RAND() * 99999 as int) as varchar(5))
 SET @sqlCreateTable = N'
    CREATE TABLE ' + @tableName + '
    (
        ID int NOT NULL identity(1, 1)
        , DefaultResult int NULL DEFAULT 0
        , Start1 smalldatetime NULL
        , Room1 int NULL
        , AMPM1 int NULL
    )'

 exec(@sqlCreateTable)

 /* Determine if this is a phased exam */
 IF EXISTS (select 1 from examcode where examcode_no in (select val from fnconverttotable(@ProcedureList)) AND ScheduleWithPhases = 1)
 BEGIN
   -- Phased Exam, make sure there is only one procdure being passed in
   IF (Select count(1) From fnConvertToTable(@ProcedureList)) > 1
    BEGIN
     -- Do Nothing, too many phased procedures passed in
     PRINT 'Only 1 phased procedure at a time'
    END
   ELSE
    BEGIN
     SET @sqlInsertRecords = N'
        INSERT INTO ' + @tableName + '
        (start1, room1, start2, room2, start3, room3, start4, room4, start5, room5)
        EXEC spScheduleSearchPhasedExams ' + @ProcedureList + ', ''' + CONVERT(varchar, @StartDate, 102) + ''', ' + CONVERT(varchar, @interval) + ', ' + CONVERT(varchar, @future) + ', ' + ISNULL(CONVERT(varchar, @PatientID), 'NULL')
     EXEC (@sqlInsertRecords)
     SELECT @tableName as resultKey
    END
   END
ELSE
   BEGIN
    SET @sqlInsertRecords = N'
    INSERT INTO ' + @tableName + '
    (start1, room1, start2, room2, start3, room3, start4, room4, start5, room5)
    EXEC spScheduleSearchNormalExams ' + @ProcedureList + ', ''' + CONVERT(varchar, @StartDate, 102) + ''', ' + ISNULL(@ReservationList, 'NULL') + ', ' + CONVERT(varchar, @interval) + ', ' + CONVERT(varchar, @future) + ', ' + ISNULL(CONVERT(varchar, @PatientID), 'NULL')
    EXEC (@sqlInsertRecords)
    SELECT @tableName as resultKey
   END

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

2 个答案:

答案 0 :(得分:1)

您可能会收到多个结果集?尝试添加以下行:

SET NOCOUNT ON

JTDS不需要这一行,但我已添加为其他驱动程序添加此行。

答案 1 :(得分:0)

如果有其他人遇到这个。问题是MSSQL驱动程序返回更新计数结果以及结果集。如果存储过程执行任何更新,则需要遍历所有结果类型以查找实际结果集。

try (CallableStatement st = con.prepareCall(statement)) {
    setParameters(st, params);

    st.execute();
    while (true) {
        boolean queryResult = st.getMoreResults();
        if (queryResult) {
            try (ResultSet rs = st.getResultSet()) {
               // do something here
            }
        } else {
            if (st.getUpdateCount() == -1) {
                break;
            }
        }
    }
}