简体   繁体   English

在Oracle 11g存储过程中将日期与时间戳进行比较

[英]Compare Date with Timestamp in oracle 11g stored procedure

I'm very confused from this problem. 我对这个问题感到非常困惑。 We use Oracle DB with stored procedures, these are called from C#. 我们将Oracle DB与存储过程一起使用,这些存储过程从C#中调用。

This is problematic part of stored procedure: 这是存储过程中有问题的部分:

  IF pi_from_date IS NOT NULL THEN
       where_part := where_part || 'and created_on >= ''' ||  pi_from_date || '''';
   END IF;
   IF pi_to_date IS NOT NULL THEN
       where_part := where_part || 'and created_on <= ''' ||  pi_to_date || '''';
   END IF;

pi_from_date and pi_to_date are IN parameters type of DATE. pi_from_date和pi_to_date是DATE的IN参数类型。 credated_on is column type of TIMESTAMP. credated_on是TIMESTAMP的列类型。 I call this procedure from C# code and use C# type DateTime for pi_to_date and pi_from_date. 我从C#代码调用此过程,并将C#类型的DateTime用于pi_to_date和pi_from_date。 It isn't working. 它不起作用。 I don't get any results, but in DB are some records. 我没有任何结果,但是在数据库中有一些记录。 I tried functions like TO_DATE, TO_CHAR, but nothing is working. 我尝试了TO_DATE,TO_CHAR之类的功能,但没有任何效果。 Sometimes I get error: 有时我会出错:

SQL Error: ORA-00904: "APR": invalid identifier SQL错误:ORA-00904:“ APR”:无效的标识符

Could you help me how to solve this problem, please? 您能帮我解决这个问题吗? Thanks. 谢谢。

EDIT: Here is C# code: 编辑:这是C#代码:

var parameters = new OracleDynamicParameters();
parameters.Add("pi_from_date", filterData.From); //DateTime.Now.AddMonths(-1);
parameters.Add("pi_to_date", filterData.To); //DateTime.Now.AddMonths(1);

var payments = conn.Query<Payment>("TEST_PCG.find_proc", parameters, commandType: commandType.StoredProcedure);

In DB are records with date 1.8.2013. DB中记录着日期为1.8.2013的记录。

Stored procedure: 存储过程:

PROCEDURE find_proc ( pi_from_date           IN DATE,
                          pi_to_date             IN DATE,
                          items_ret              OUT sys_refcursor)
AS 
  stmt varchar2(32767);
  where_part varchar2(32767);
BEGIN
  -- select statement part
   stmt := 'SELECT * FROM (SELECT a.*, rownum r__ FROM (SELECT * FROM Payment WHERE 1=1 ';        

   -- where statement part
   IF pi_from_date IS NOT NULL THEN
       where_part := where_part || 'and created_on >= ''' ||  pi_from_date || '''';
   END IF;
   IF pi_to_date IS NOT NULL THEN
       where_part := where_part || 'and created_on <= ''' ||  pi_to_date || '''';
   END IF;
   stmt := stmt || where_part;

   -- get ref cursor from query
   OPEN items_ret FOR stmt;
END find_proc;

Your Dynamic SQL considers your date value as a string, and depending on your machine, C# would convert the date into different formats of string (eg dd/mm/yyyy, DD-MM-YYYY..etc) so you need to specify that as well. 动态SQL将日期值视为字符串,根据计算机的不同,C#会将日期转换为不同格式的字符串(例如dd / mm / yyyy,DD-MM-YYYY..etc),因此您需要指定也一样 If you have to use D-SQL then your where clause should convert the date value you are passing to a date using TO_DATE and have the same date format that you are converting to in C# 如果必须使用D-SQL,则where子句应将要传递的日期值转换为使用TO_DATE的日期,并具有与在C#中转换为的日期格式相同的日期格式

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

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