简体   繁体   English

Oracle 12c数据默认值

[英]Oracle 12c Data Default Value

Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

Table:桌子:

CREATE TABLE t1 (
  c1 VARCHAR2(100 CHAR) DEFAULT 'SOME DATA' NOT NULL, 
  c2 DATE DEFAULT SYSDATE NOT NULL, 
  c3 NUMBER DEFAULT 10 NOT NULL);

Select: Select:

BEGIN
  FOR c IN (SELECT * FROM user_tab_columns WHERE table_name = 'T1') LOOP
    DBMS_output.put_line('"' || c.data_default || '"');
  END LOOP;
END;

Result:结果:

"'SOME DATA' "
"SYSDATE "
"10 "

Why there are spaces at the end of DATA DEFAULT ?为什么DATA DEFAULT末尾有空格? I need to compare data default in Oracle dictionnary with data default in project metadata, but this space ruins comparison.我需要将 Oracle 字典中的数据默认值与项目元数据中的数据默认值进行比较,但这个空间破坏了比较。

EDIT: @GordonLinoff asks "Is the extra character really a space?"编辑: @GordonLinoff 问“多余的字符真的是一个空格吗?” Due to LONG datatype of DATA_DEFAULT column I have found only one test:由于DATA_DEFAULT列的LONG数据类型,我只找到了一个测试:

DECLARE
  l_ret   VARCHAR2(32000);
  l_dump  VARCHAR2(32000);
BEGIN
  FOR c IN (SELECT * FROM user_tab_columns WHERE table_name = 'T1') LOOP
    DBMS_output.put_line('"' || c.data_default || '"');
    l_ret := c.data_default;
    SELECT dump(l_ret) 
      INTO l_dump
      FROM dual;
    DBMS_output.put_line(l_dump);
  END LOOP;
END;

Result:结果:

"10 "
Typ=1 Len=3: 49,48,32
"SYSDATE "
Typ=1 Len=8: 83,89,83,68,65,84,69,32
"'SOME DATA' "
Typ=1 Len=12: 39,83,79,77,69,32,68,65,84,65,39,32

So, it is a space (32).所以,它是一个空间(32)。

Interesting.有趣的。

A simple test shows that the extra space is only added when the column for which you define a default value is declared NOT NULL. (So, contrary to what @GordonLinoff speculated, this is not an artifact of the default value being stored as LONG.)一个简单的测试表明,仅当您为其定义默认值的列被声明为 NOT NULL 时才会添加额外的空间。(因此,与@GordonLinoff 推测的相反,这不是默认值存储为 LONG 的产物。 )

Why Oracle is doing this... who knows.为什么 Oracle 正在这样做......谁知道呢。

In any case, as you compare the default values from the dictionary to your specifications (project metadata), the simplest solution seems to be to conditionally add a trailing space at the end of the other term in the comparison.在任何情况下,当您将字典中的默认值与您的规范(项目元数据)进行比较时,最简单的解决方案似乎是在比较中的另一个术语的末尾有条件地添加一个尾随空格。 (Conditional on whether the column is NOT NULL.) (条件是该列是否不是 NULL。)

Alternatively, if you really need to manipulate the purported default value directly, you could also retrieve the DEFAULT_LENGTH value from USER_TAB_COLUMNS, so that you can easily remove just the trailing space (when the column is not nullable) while preserving any trailing spaces that may be part of the actual, desired default value.或者,如果您确实需要直接操作声称的默认值,您还可以从 USER_TAB_COLUMNS 中检索 DEFAULT_LENGTH 值,这样您就可以轻松地只删除尾随空格(当列不可为空时),同时保留任何尾随空格可能是实际的、期望的默认值的一部分。 (For example: default value of exactly one space for a non-NULL text column; the DEFAULT_VALUE is reported as two spaces, and you only want to trim the second space, but not the first.) (例如:非 NULL 文本列的默认值为一个空格;DEFAULT_VALUE 报告为两个空格,您只想修剪第二个空格,而不是第一个。)

You can just trim() that:你可以trim()那个:

BEGIN
  FOR c IN (SELECT * FROM user_tab_columns WHERE table_name = 'T1') LOOP
    DBMS_output.put_line('"' || trim(c.data_default) || '"');
  END LOOP;
END;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM