2007年7月6日星期五

PL/SQL

DECLARE
[CONSTANT] [NOT NULL] [(:= | DEFAULT) ];
a CONSTANT NUMBER := 100;
b VANCHAR2(100) NOT NULL DEFAULT '100';
c VANCHAR2(100) := b;

//NOT NULL makes the varialbe become non-nullable
//DEFAULT here has the same use as :=
BEGIN
......
END


Variable Types:
CHAR[(length)] - string that with fixed length.
eg. CHAR; // string length=1
CHAR(100); // string length=100
This string '' is of length 0.
A string ONLY use single quotes to represent, eg. 'hihi'.

VARCHAR2(max_len) - string that with variable length within the range specified in max_len.
eg. VARCHAR2(1999)

NUMBER[(precision, scale)] - stores floating point number or fixed point number
precision: number of decimal place to be display
scale : place that rounding occurs.
eg. NUMBER // floating point
NUMBER(3,4) //12.34567 => 12.345
NUMBER(4,4) //12.34567 => 12.3457
NUMBER(2,-1)//123.6 => 120.00
NUMBER(2) //123.6 => 123.00

BINARY_INTEGER - integer

PLS_INTEGER - integer that has faster access and require less storage

BOOLEAN - stores TRUE, FALSE, and NULL

DATE - store the time
eg. abc DATE := SYSDATE // get the current system date
def DATE := TO_DATE('01-01-85', 'dd-mm-yy')

Declare variables using %TYPE:
can be used in table column name and variable name
TABLE.COLUMN%TYPE
VARIABLE%TYPE

eg. id table1.user_id%TYPE;
id2 id%TYPE;

Bind Variable:
Create environment variable for SQLPLUS to use
eg.
VARIABLE aaa NUMBER //no semi-colon

BEGIN
SELECT num INTO :aaa //use :to refer the variable as local variable
FROM table1;
END;

PRINT aaa //this is print outside the plsql block

To print things inside the plsql block
1. SET SERVEROUTPUT ON
2. DBMS_OUTPUT.PUT_LINE('HIHI, MY VALUE IS' || TO_CHAR(variable));

沒有留言: