1

我使用 vb.net 和 oracle 的基本日期选择器作为我的数据库。当我从基本日期选择器插入日期时,出现此错误>> ORA-01797:此运算符必须后跟 ANY 或 ALL

这是我的代码:

    Private Function GetDate(ByVal bdp1 As Date) As DataSet

    Dim connectionString As String = "Data Source = ***; User ID =***; Password =**;"
    Dim sqlConnection As OracleClient.OracleConnection = New OracleClient.OracleConnection(connectionString)

    Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= ('" & bdp1 & "' , 'MM/DD/YYYY') and tran_dttm > ('" & bdp1 & "', 'MM/DD/YYYY')"

    Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
    sqlCommand.CommandTimeout = 0

    Dim dataAdapter As OracleClient.OracleDataAdapter = New OracleClient.OracleDataAdapter(sqlCommand)
    Dim dataSet As DataSet = New DataSet
    dataAdapter.Fill(dataSet)

    Return dataSet

End Function
4

3 回答 3

0

试试这个 ..

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= cdate('" & bdp1.Value.ToString & "') and tran_dttm > cdate('" & bdp1.Value.ToString  & "')"
于 2013-07-01T01:07:35.653 回答
0

试试这个:

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= '" & bdp1.ToString("MM/dd/yyyy") & "' and tran_dttm > '" & bdp1.ToString("MM/dd/yyyy") & "'"

但是对于 oracle,默认的日期时间格式是YYYY-MM-DD. 你有三个选择:

  • 更改适合您需求的默认文件格式
  • 使用默认格式:bdp1.ToString("yyyy-MM-dd")
  • 使用todate指定读取格式的 oracle 函数:
    " to_date('" & bdp1.ToString("MM/dd/yyyy") & "', 'mm/dd/yyyy') "
于 2013-07-01T05:44:44.607 回答
0

尝试,而不是:

Dim queryString As String = "select * from smsdw.lot_act where tran_dttm <= :dtm1 and tran_dttm > :dtm2"

Dim sqlCommand As OracleClient.OracleCommand = New OracleClient.OracleCommand(queryString, sqlConnection)
sqlCommand.Parameters.AddWithValue("dtm1",bdp1)
sqlCommand.Parameters.AddWithValue("dtm2",bdp1)

其中 a) 避免了 SQL 注入的可能性,并且 b) 始终将日期保留为日期,而不是将其与字符串相混淆。

It doesn't fix the logical issue with your query though - where you're trying to find a row where tran_dttm is both "less than or equal" and "greater than" the same value.

于 2013-07-01T07:22:59.320 回答