SQL> DECLARE 2 c CHAR(32767) := ' '; 3 v VARCHAR2(32767) := ' '; 4 BEGIN 5 dbms_output.put_line('c is ['||LENGTH(c)||']'); 6 dbms_output.put_line('v is ['||LENGTH(v)||']'); 7 v := v || ' '; 8 dbms_output.put_line('v is ['||LENGTH(v)||']'); END; 9 / c is [32767] v is [1] v is [2] PL/SQL 过程已成功完成。输出的结果显示:CHAR类型变量在定义的时候分配内存,分配的内存也可能超过变量定义的大小,VARCHAR2类型变量动态分配内存,根据设置的变量的大小。
SQL> DECLARE 2 c CHAR(32767) := ' '; 3 v VARCHAR2(32767) := ' '; 4 var1 CHAR := ' '; -- Implicitly sized at 1 byte. 5 var2 CHAR(1) := ' '; -- Explicitly sized at 1 byte. 6 var3 CHAR(1 BYTE) := ' '; -- Explicitly sized at 1 byte. 7 var4 CHAR(1 CHAR) := ' '; -- Explicitly sized at 1 character. 8 BEGIN 9 dbms_output.put_line('c is ['||LENGTH(c)||']'); 10 dbms_output.put_line('v is ['||LENGTH(v)||']'); 11 dbms_output.put_line('var1 is ['||LENGTH(var1)||']'); 12 dbms_output.put_line('var2 is ['||LENGTH(var2)||']'); 13 dbms_output.put_line('var3 is ['||LENGTH(var3)||']'); 14 dbms_output.put_line('var4 is ['||LENGTH(var4)||']'); 15 v := v || ' '; 16 dbms_output.put_line('v is ['||LENGTH(v)||']'); END; 17 / c is [32767] v is [1] var1 is [1] var2 is [1] var3 is [1] var4 is [1] v is [2]
SQL> DECLARE 2 SUBTYPE code IS CHAR(1 CHAR); 3 c CHAR(1 CHAR) := 'A'; 4 d CODE; 5 BEGIN 6 d := c; 7 END; 8 / PL/SQL 过程已成功完成。
SQL> DECLARE 2 d DATE := SYSTIMESTAMP; 3 t TIMESTAMP(8) := SYSTIMESTAMP; 4 BEGIN 5 dbms_output.put_line('DATE ['||d||']'); 6 dbms_output.put_line('TO_CHAR ['||TO_CHAR(d,'DD-MON-YY HH24:MI:SS')||'] '); 7 dbms_output.put_line('TIMESTAMP ['||t||']'); 8 END; 9 / DATE [12-6月 -12] TO_CHAR [12-6月 -12 17:50:17] TIMESTAMP [12-6月 -12 05.50.17.40600000 下午] PL/SQL 过程已成功完成。
SQL> DECLARE 2 var1 TIMESTAMP WITH LOCAL TIME ZONE; 3 var2 TIMESTAMP WITH LOCAL TIME ZONE := SYSTIMESTAMP; 4 var3 TIMESTAMP(3) WITH LOCAL TIME ZONE; 5 var4 TIMESTAMP(3) WITH LOCAL TIME ZONE := SYSTIMESTAMP; 6 BEGIN 7 dbms_output.put_line('var1 ['||var1||']'); 8 dbms_output.put_line('var2 ['||var2||']'); 9 dbms_output.put_line('var3 ['||var3||']'); 10 dbms_output.put_line('var4 ['||var4||']'); 11 END; 12 / var1 [] var2 [12-6月 -12 05.51.44.750000 下午] var3 [] var4 [12-6月 -12 05.51.44.750 下午] PL/SQL 过程已成功完成。
SQL> DECLARE 2 d DATE := SYSDATE; 3 BEGIN 4 dbms_output.put_line(TO_CHAR( TRUNC(d) ,'DD-MON-YY HH24:MI:SS')); 5 END; 6 / 12-6月 -12 00:00:00 PL/SQL 过程已成功完成。
PLS_INTEGER使用操作系统的数学函数库,INTEGER是NUMBER的subtype。
PLS_INTEGER使用本地数学函数库,声明语句后不分配内存,直到变量分配一个值,可以通过length()函数进行侧测试;
SQL> DECLARE 2 var1 PLS_INTEGER; -- A null value requires no space. 3 var2 PLS_INTEGER := 11; -- An integer requires space for each character. 4 BEGIN 5 dbms_output.put_line('var1 ['||length(var1)||']'); 6 dbms_output.put_line('var2 ['||length(var2)||']'); 7 END; 8 / var1 [] var2 [2] PL/SQL 过程已成功完成。