试图在Oracle中获取已定义变量的大小.我可能需要在声明varchar2的大小时使用数字,而不是必须跟踪额外的变量或数字.
示例伪代码:
declare myvar varchar(42) := 'a'; begin /* I know the length is length(myvar) = 1. */ /* but how do I get 42? */ /* What is the max defined size of this variable */ declared_size_of(myvar); end
我需要这个的原因是lpad字符串的长度为声明的大小,因此它不会产生异常.
解决方法
正如@Justin在他的评论中所说,如果使用CHAR数据类型,则不必显式空白填充字符串. Oracle会将值空白填充到它的最大大小.
If the data type of the receiver is CHAR,PL/sql blank-pads the value
to the maximum size. Information about trailing blanks in the original
value is lost.
例如,
sql> SET serveroutput ON sql> DECLARE 2 myvar CHAR(42); 3 BEGIN 4 myvar:='a'; 5 dbms_output.put_line(LENGTH(myvar)); 6 END; 7 / 42 PL/sql procedure successfully completed. sql>