假设我们有一个'使用以下SQL命令创建的表:
CREATE TABLE person (
name VARCHAR2(50) ,
gender VARCHAR2(50) CHECK( gender IN ('male','female','other') )
);
有没有办法检索“性别”的授权值列表?柱?即。 [男,女,其他]?
答案 0 :(得分:1)
检查是一种约束,它可以从表all_constraints
中获取,其中列search_condition
将包含枚举('男','女' ,'其他'),此列的类型为LONG。检查约束类型为C
。
要解析LONG值,您可以创建一个函数:
CREATE or REPLACE function Find_Value
(av_cname varchar2)
RETURN varchar2
IS
long_var LONG;
BEGIN
SELECT SEARCH_CONDITION INTO long_var
FROM ALL_CONSTRAINTS
WHERE constraint_name = av_cname;
return long_var;
END;
以下查询将为您提供所需内容:
select Find_Value(constraint_name) from all_constraints
where table_name = 'your_table_name' AND constraint_type = 'C'
答案 1 :(得分:0)
您可以这样做:
with col_constraints as
(select table_name,constraint_name,
extractvalue
( dbms_xmlgen.getxmltype
( 'select search_condition from user_constraints where constraint_name = ''' || u.constraint_name || '''' )
, '//text()' ) search_cond
from user_constraints u
where constraint_type = 'C'
and table_name = 'PERSON'
)
, sval as
(select substr(search_cond
,instr(search_cond,'(',instr(upper(search_cond),' IN ')+4)+1 -- after opening parenthesis
,instr(search_cond,')',instr(upper(search_cond),' IN ')+5) -- pos of closing parenthesis
-instr(search_cond,'(',instr(upper(search_cond),' IN ')+4)-1) -- subtract start to get list length
inlist_string
from col_constraints
)
SELECT regexp_substr(inlist_string,'[^,]+',1,level),level
from sval
connect by level <= REGEXP_COUNT (inlist_string,'[^,]+');
如您所见,它分为三个部分:
1:使用xmlgen将LONG传递给可行的东西(感谢William Robertson) 2:仅提取“ IN”的列表 3:使用递归regexp_substr将值拆分为行