Moje hlavní dovednosti jsou s SQL Serverem, ale byl jsem požádán, abych provedl nějaké vyladění dotazu Oracle. Napsal jsem následující SQL:
declare @startDate int
select @startDate = 20110501
A dostanu tuto chybu:
declare @startDate int
select @startDate = 20110501
Error at line 1
ORA-06550: line 1, column 9:
PLS-00103: Encountered the symbol "@" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
Jak mohu deklarovat a používat proměnné v Oracle?
Uvnitř bloku pl/sql:
declare
startdate number;
begin
select 20110501 into startdate from dual;
end;
/
pomocí vazebné proměnné:
var startdate number;
begin
select 20110501 into :startdate from dual;
end;
/
Procedura PL/SQL byla úspěšně dokončena.
SQL> print startdate
STARTDATE
----------
20110501
v dotazu:
select object_name
from user_objects
where created > to_date (:startdate,'yyyymmdd'); /*prefix the bind variable wïth ":" */
SQL * Plus podporuje další formát:
DEFINE StartDate = TO_DATE('2016-06-21');
DEFINE EndDate = TO_DATE('2016-06-30');
SELECT
*
FROM
MyTable
WHERE
DateField BETWEEN &StartDate and &EndDate;
Poznamenejte si ampersandy, kde mají být v dotazu provedeny substituce.