在聲明VARCHAR2變數時,我們可以指定其最大長度單位為CHAR:v1 VARCHAR2(2 CHAR); 或BYTE:v2 VARCHAR2(2 BYTE); ,
大多數預設什麼都不指定:v3 VARCHAR2(2); 。 那麼這三者有什麼區別呢? 請看例子:
DECLARE<br /> v1 VARCHAR2(2 CHAR);<br /> v2 VARCHAR2(2 BYTE);<br /> v3 VARCHAR2(2);<br />BEGIN</p><p> <<v1_test>><br /> BEGIN<br /> v1 := '測試';<br /> dbms_output.put_line('v1_test successed, v1=' || v1);<br /> EXCEPTION<br /> WHEN OTHERS THEN<br /> dbms_output.put_line('v1_test failed, =>' ||<br /> dbms_utility.format_error_stack);<br /> END;</p><p> <<v2_test>><br /> BEGIN<br /> v2 := '測試';<br /> dbms_output.put_line('v2_test successed, v2=' || v2);<br /> EXCEPTION<br /> WHEN OTHERS THEN<br /> dbms_output.put_line('v2_test failed, =>' ||<br /> dbms_utility.format_error_stack);<br /> END;</p><p> <<v3_test>><br /> DECLARE<br /> l_default_qualifier nls_session_parameters.VALUE%TYPE;<br /> BEGIN<br /> --we can get the default qualifier by querying nls_session_parameters.<br /> SELECT VALUE<br /> INTO l_default_qualifier<br /> FROM nls_session_parameters<br /> WHERE parameter = 'NLS_LENGTH_SEMANTICS';<br /> dbms_output.put_line('v3_test, l_default_qualifier=' ||<br /> l_default_qualifier);</p><p> v3 := '測試';<br /> dbms_output.put_line('v3_test successed, v3=' || v3);<br /> EXCEPTION<br /> WHEN OTHERS THEN<br /> dbms_output.put_line('v3_test failed, =>' ||<br /> dbms_utility.format_error_stack);<br /> END;<br />END;<br />
輸出:
v1_test successed, v1=測試<br />v2_test failed, =>ORA-06502: PL/SQL: numeric or value error: character string buffer too small</p><p>v3_test, l_default_qualifier=BYTE<br />v3_test failed, =>ORA-06502: PL/SQL: numeric or value error: character string buffer too small</p><p>