关键点:char型变量在:=''初始化时自动填充空格。
test@testdb>drop table t2
2 /
表已删除。
已用时间: 00: 00: 00.04
test@testdb>create table t2(xm char(20) not null)
2 /
表已创建。
已用时间: 00: 00: 00.01
test@testdb>CREATE OR REPLACE PROCEDURE sp_test1
2 IS
3 v_xm t2.xm%TYPE := '';
4 BEGIN
5 INSERT INTO t2
6 VALUES (v_xm
7 );
8 END;
9 /
过程已创建。
已用时间: 00: 00: 00.01
test@testdb>
test@testdb>CREATE OR REPLACE PROCEDURE sp_test2
2 IS
3 v_xm t2.xm%TYPE;
4 BEGIN
5 INSERT INTO t2
6 VALUES (v_xm
7 );
8 END;
9 /
过程已创建。
已用时间: 00: 00: 00.01
test@testdb>
test@testdb>CREATE OR REPLACE PROCEDURE sp_test3
2 IS
3 v_xm varchar2(20) := '';
4 BEGIN
5 INSERT INTO t2
6 VALUES (v_xm
7 );
8 END;
9 /
过程已创建。
已用时间: 00: 00: 00.03
test@testdb>
test@testdb>exec sp_test1;
PL/SQL 过程已成功完成。
已用时间: 00: 00: 00.03
test@testdb>exec sp_test2;
BEGIN sp_test2; END;
*
第 1 行出现错误:
ORA-01400: 无法将 NULL 插入 ("SIDB"."T2"."XM")
ORA-06512: 在"SIDB.SP_TEST2", line 5
ORA-06512: 在line 1
已用时间: 00: 00: 00.06
test@testdb>exec sp_test3;
BEGIN sp_test3; END;
*
第 1 行出现错误:
ORA-01400: 无法将 NULL 插入 ("SIDB"."T2"."XM")
ORA-06512: 在"SIDB.SP_TEST3", line 5
ORA-06512: 在line 1
已用时间: 00: 00: 00.07
test@testdb>select * from t2;
XM
--------------------
已用时间: 00: 00: 00.09
附测试脚本:
drop table t2
/
create table t2(xm char(20) not null)
/
CREATE OR REPLACE PROCEDURE sp_test1
IS
v_xm t2.xm%TYPE := '';
BEGIN
INSERT INTO t2
VALUES (v_xm
);
END;
/
CREATE OR REPLACE PROCEDURE sp_test2
IS
v_xm t2.xm%TYPE;
BEGIN
INSERT INTO t2
VALUES (v_xm
);
END;
/
CREATE OR REPLACE PROCEDURE sp_test3
IS
v_xm varchar2(20) := '';
BEGIN
INSERT INTO t2
VALUES (v_xm
);
END;
/
exec sp_test1;
exec sp_test2;
exec sp_test3;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/6906/viewspace-539670/ ,如需转载,请注明出处,否则将追究法律责任。