PL/SQL common syntax and examples

PLSQL statement

DECLARE declaration section

BEGIN program writing, SQL statement

EXECPTION handles exceptions

END;

/

Declaration section (DECLARE)

SQL> set serveroutput on -- turn on output

SQL> declare

  2  v_num number;

  3  begin

  4 v_num:=30;

  5  dbms_output.put_line('V_NUM'||v_num);

  6  end;

  7  /

V_NUM30

Find the name of the employee whose empno is 7900

declare

v1 scott.emp.ename%type;

v2 scott.emp.sal%type;

begin

select ename,sal into v1,v2 from scott.emp where empno=7900;

dbms_output.put_line('ename:'||v1);

dbms_output.put_line('sal:'||v2);

end;

/

set serveroutput on

/

Find employee number and name

set serveroutput on

declare

   v_eno number;

   v_ename varchar2(10);

   begin

   v_eno: = & empno;

   select ename into v_ename from emp where empno=v_eno;

   dbms_output.put_line('number:'||v_eno||'name:'||v_ename);

   end;

   /

Find the average salary

SQL> !vi test.sql

SQL> !cat test.sql

declare

v_avgsal number(6,2);

begin

select avg(sal) into v_avgsal from scott.emp where deptno=&no;

dbms_output.put_line('avg_sal:'||v_avgsal);

end;

SQL> @test.sql

  7  /

Enter value for no: 20

old   4: select avg(sal) into v_avgsal from scott.emp where deptno=&no;

new   4: select avg(sal) into v_avgsal from scott.emp where deptno=20;

PL/SQL procedure successfully completed.

Syntax for declaring variables

composition:

CONSTANT: define constants

NOT NULL: Indicates that this variable is not allowed to be set to NULL

:=value: indicates that when the variable is declared, its initialization content is set

DECLARE

V_resultA CONSTANT NUMBER NOT NULL:=100;

BEGIN

DBMS_OUTPUT.put_line('v_resultA constant content:'||(v_resultA));

END;

/

Use %TYPE to declare variable type (assignment to column type)

DECLARE

eno emp.emono%TYPE;

ename emp.ename%TYPE;

BEGIN

  DBMS_OUTPUT.put_line( ' Please enter the employee number: ' );

  dbms_output.put_line('sal:'||v2);

end;

Use the %ROWTYPE tag to define the type of a row in the table (assign the row type)

SQL> DECLARE

  2  deptRow dept%ROWTYPE;

  3  BEGIN

  4  SELECT * INTO deptRow FROM dept WHERE deptno=10;

  5  DBMS_OUTPUT.put_line('deptno:'||deptRow.deptno||',name:'||deptRow.dname||',loc:'||deptRow.loc);

  6  end;

  7  /

Use relational operators

DECLARE

        v_url VARCHAR2(50):='www.vdedu.com.cn';

        v_num1 NUMBER:=80;

        v_num2 NUMBER:=30;

BEGIN

        IF v_num1>v_num2 THEN

                DBMS_OUTPUT.put_line('A');

        END IF;

        IF v_url LIKE '%vdedu%' THEN

                DBMS_OUTPUT.put_line('includes vdedu');

        END IF;

END;

 

 

AND OR NOT operator

 

DECLARE

v_flag1 BOOLEAN: = TRUE;

v_flag2 BOOLEAN: = FALSE;

v_flag3 BOOLEAN;

BEGIN

IF v_flag1 and (NOT v_flag2) THEN

DBMS_OUTPUT.PUT_LINE('1');

END IF;

IF v_flag1 or v_flag3 THEN

DBMS_OUTPUT.PUT_LINE('2');

END IF;

IF v_flag1 and v_flag3 is null THEN

DBMS_OUTPUT.PUT_LINE('3');

END IF;

END;

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325306271&siteId=291194637