我有一个存储过程,它将日期或null返回到命令参数中。但是,我在处理null返回时遇到了困难。
该过程如下所示:
PROCEDURE GetDate(inParam IN VARCHAR2, outDate OUT DATE) AS
BEGIN
SELECT MAX(DateVal) INTO outDate
FROM Table1
WHERE Col1 = inParam;
EXCEPTION
WHEN no_data_found THEN
outDate:= NULL;
END GetDate;
我的VB.NET代码看起来像这样:
Dim returnDate As Date
Connection.Open()
Command.CommandType = CommandType.StoredProcedure
Command.CommandText = "GEN_PACKAGE.GetDate"
Command.Parameters.Add("inParam", inParam).Direction = ParameterDirection.Input
Command.Parameters.Add("outDate", OracleDbType.Date).Direction = ParameterDirection.Output
Command.ExecuteNonQuery()
returnDate = CDate(Command.Parameters("outDate").Value)
然而,使用它我得到了returnDate = CDate(Command.Parameters("outDate").Value)
上的异常Conversion from type 'OracleDate' to type 'Date' is not valid.
。
如果我将OracleDbType.Date
更改为DbType.Date
(这是我从另一个答案中找到的解决方案),我会得到Command.Parameters.Add("D0131Date", DbType.Date).Direction = ParameterDirection.Output
上的Value does not fall within the expected range
。
当我的返回日期可能为空也可能不为空时,我如何为给定的输入返回日期?
发布于 2019-05-21 03:12:06
抱歉,无法测试。没有Oracle
Dim returnDate = If(Command.Parameters("outDate").Value = DBNull.Value, Nothing, Command.Parameters("outDate").Value)
If returnDate Is Nothing Then
'Handle a null return
Else
Dim MyDate As Date = DirectCast(returnDate, Date)
End If
发布于 2019-05-20 18:00:09
您可以将过程的异常块转换为
EXCEPTION
WHEN no_data_found THEN
outDate:= date'1900-01-01';
返回一个非常旧的日期,并通过一些if
或case
语句将这个特定的值作为NULL值在您的应用程序中过滤出来,并使用outDate
的值进行排序。
https://stackoverflow.com/questions/56218227
复制相似问题