在Oracle中,DECLARE關鍵字用于聲明一個或多個變量、常量或游標。以下是DECLARE的幾種用法:
DECLARE
variable_name datatype [DEFAULT initial_value];
BEGIN
– code
END;
DECLARE
constant_name CONSTANT datatype := initial_value;
BEGIN
– code
END;
DECLARE
cursor_name CURSOR FOR select_statement;
BEGIN
– code
END;
DECLARE
exception_name EXCEPTION;
BEGIN
– code
EXCEPTION
WHEN exception_name THEN
– exception handling code
END;
DECLARE
TYPE record_type IS RECORD (
field1 datatype,
field2 datatype,
…
);
variable_name record_type;
BEGIN
– code
END;
DECLARE
TYPE table_type IS TABLE OF datatype;
variable_name table_type;
BEGIN
– code
END;
需要注意的是,DECLARE語句必須在BEGIN和END關鍵字之前使用,并且只能在PL/SQL塊中使用。