简体   繁体   中英

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#.

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. credated_on is column type of TIMESTAMP. I call this procedure from C# code and use C# type DateTime for pi_to_date and 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. Sometimes I get error:

SQL Error: ORA-00904: "APR": invalid identifier

Could you help me how to solve this problem, please? Thanks.

EDIT: Here is C# code:

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.

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. 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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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