the oracle PL / SQL study notes

SQL statements stored procedure execution immediately

execute immediate sql;

Execute a stored procedure in sqlplus

 execute 存储过程名

oracle create synonyms, defaults to private synonyms

  create synonym ctb for item.chinastudentbook@dblink6;

Public synonym

create public synonym ctb for .... 

Delete synonyms

drop synonym ctb;
drop public synonym pb_ctb;

Define the constant, constant keyword

  c_id constant number := 50;

Bind variables, use: variable name fashion

SQL> begin
	:bd:='hello you';
	end;
SQL> print bd;

Defined subtypes

declare
	subtype sub_num is number(5,1);
	v2 sub_num;
begin
 	null;
end;

Support reverse reverse circulation

for counter in [reverse] 开始值..结束值  loop

end loop;

Unconditional exit

loop

exit

end loop;

Conditional exit

loop

exit when y > 3;

end loop;

Unconditional continue

loop

continue;

end loop;

Conditional continue

loop

continue when y>3;

end loop;

goto statement, you need to define labels, custom label name tag syntax << >>

declare
	...
begin
	<<hbk>>
	...
	if v1>20 then
	goto hbk;
	end if;
	...
end;

When the program executes the goto statement, the statement is not executed after the goto, but a jump to the label hbk starts executing code from the hbk.

Abnormal about PL / SQL in
Here Insert Picture Description

declare
	...
begin
	...
exception
	when 异常1  then
	对异常1进行处理;
	when 异常2 then
	对异常2进行处理;
	...
	when others then
	...
end;

Catch an Exception

when  others then
dbms_output.put_line('Error code:'||SQLCODE||'   '||SQLERRM);

SQLCODE and Oracle SQLERRM are predefined functions, the former returns an error code, which returns an error message.

Custom exception, the syntax is as follows:
"Exception name" exception

pragma exception_init(exception_name,-Oracle_error_number);

Range -20 000 to -20 999 numbers

Trigger three
1, an exception is triggered automatically by the oracle
2, using the raise statement triggered manually
3, call a stored procedure triggered manually raise_application_error

declare
	myecp exception;
begin
	raise myecp;
	exception when myecp then
	dbms_output.put_line('myecp error..');
end;


declare
	myecp2 exception;
	pragma exception_init(myecp2,-20009);
begin
	raise_application_error(-20009,'data is out of list');
	exception when myecp2 then
	dbms_output.put_line('Error code:'||SQLCODE||'  '||SQLERRM);
end;

Guess you like

Origin blog.csdn.net/huangbaokang/article/details/94625214