且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Oracle中按功能填充的虚拟列的数据类型的长度

更新时间:2023-01-29 16:55:06

将大小报告为4000似乎并不是一个问题-无论如何,您实际上将无法放入长值(假设您的函数是只返回短的),而不是浪费空间.

Having the size reported as 4000 doesn't really seem to be a problem - you won't actually be able to put long values in anyway (assuming your function only returns short ones), and it's not like it's wasting space.

但是,如果您希望它更整洁并以较小的尺寸显示,则可以使用 cast() ,它与其他功能(甚至是诸如substr()之类的内置功能)的处理方式不同:

But if you want it to be neater and appear as a smaller size you can use cast(), which isn't treated the same as other functions (even built-in ones like substr()):

create table sometable( 
  value varchar2(255),
  awesome varchar2(30) as (cast(awesomeness(value) as varchar2(30)))
);

Table sometable created.

desc sometable

Name    Null Type          
------- ---- ------------- 
VALUE        VARCHAR2(255) 
AWESOME      VARCHAR2(30)  

如果您的函数以某种方式返回了更长的值(例如在示例中,因为retVal为255,尽管字符串文字足够短),则您的虚拟列值将被截断为30个字符.

If your function somehow returns a longer value - as it could in your example since retVal is 255, though your string literal is short enough - your virtual column value would be truncated to 30 characters.

使用您的原始功能,当虚拟列对于返回的列而言太小时会发生这种情况:

With your original function, this is what happens when the virtual column is too small for what it returns:

create table sometable( 
  value varchar2(255),
  awesome varchar2(10) as (cast(awesomeness(value) as varchar2(10)))
);

insert into sometable (value) values ('X');

select awesome from sometable;

AWESOME   
----------
Some amazi