在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块中使用。