Oracle declaring and Using Dates

145 Views Asked by At

So I have been using SQL Server for about 7 years now, just changed jobs and a lot of our production stuff is in Oracle SQL Developer. Day 1, not enjoying Oracle but hoping for the best.

I am trying to do what I would think would be extremely simply and easy but all the methods i find only do not work. I run the below and get an Error.

Bind Variable "Startdt" is NOT DECLARED anonymous block completed

I have tried declaring it several different ways, including how it Enddt is. DealerNbr works fine as is. The query itself i pulled from an SSRS report, but I have to declare the variables so i can test/add columns to it. the SSRS works fine.

DECLARE 
  DealerNbr NUMBER;
  Startdt DATE;
  Enddt DATE;
BEGIN
  DealerNbr := 18913;
  Startdt   := TO_DATE ('2012-01-01','mm,dd,yyyy');
  Enddt     := '2012-12-31';
END;

select prq_payee_nbr ,
prq_status_cd 
from payable_requisitions 
where prq_payee_nbr = :DealerNbr
and prq_status_cd <> 'V' 
and prq_dt between :Startdt and :Enddt
order by prq_dt

Essential I am just trying to do the most basic thing. In SQL Server it would be:

Declare @DealerNbr Int
,@Startdt date
,@EndDt date

Set @DealerNbr = '18913'
Set @Startdt = '2012-01-01'
Set @Enddt = '2012-12-31'

Select * 
from payable_requistion
where prq_payee_nbr = @DealerNbr
and prq_dt between @Startdt and @EndDt
1

There are 1 best solutions below

2
On BEST ANSWER
VARIABLE cur REFCURSOR;
/

DECLARE
  DealerNbr NUMBER := 18913;
  Startdt   DATE   := DATE '2012-01-01';
  Enddt     DATE   := DATE '2012-12-31';
BEGIN
  OPEN :cur FOR
  select prq_payee_nbr ,
         prq_status_cd 
  from   payable_requisitions 
  where  prq_payee_nbr = DealerNbr
  and    prq_status_cd <> 'V' 
  and    prq_dt between Startdt and Enddt
  order by prq_dtl;
END;
/

PRINT cur;

Or

VARIABLE Startdt VARCHAR2;
VARIABLE Enddt VARCHAR2;
VARIABLE DealerNbr NUMBER;

BEGIN
  :DealerNbr := 18913;
  :Startdt   := '2012-01-01';
  :Enddt     := '2012-12-31';
END;
/

select prq_payee_nbr ,
       prq_status_cd 
from   payable_requisitions 
where  prq_payee_nbr = :DealerNbr
and    prq_status_cd <> 'V' 
and    prq_dt between TO_DATE( :Startdt, 'YYYY-MM-DD' )
              and     TO_DATE( :Enddt,   'YYYY-MM-DD' )
order by prq_dtl;