gpt4 book ai didi

sql - Oracle 查询 all_tab_columns.data_default(类型 LONG)

转载 作者:行者123 更新时间:2023-12-04 22:43:03 34 4
gpt4 key购买 nike

我已经运行了这个查询:

SELECT
OWNER,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
DATA_LENGTH,
(CASE
WHEN DATA_PRECISION IS NULL THEN 0
ELSE DATA_PRECISION
END) DATA_PRECISION,
(CASE
WHEN DATA_SCALE IS NULL THEN 0
ELSE DATA_SCALE
END) DATA_SCALE,
NULLABLE,
COLUMN_ID
DEFAULT_LENGTH,
DATA_DEFAULT,
(CASE
WHEN DATA_DEFAULT IS NULL THEN '0'
ELSE DATA_DEFAULT
END) DATA_DEFAULT1
FROM
all_tab_columns
WHERE
table_name LIKE 'TABLE1';

但它在列 DATA_DEFAULT 处引发错误:

ORA-00932: inconsistent datatypes: expected CHAR got LONG
00932. 00000 - "inconsistent datatypes: expected %s got %s"



我该如何解决?

谢谢!

最佳答案

你不能用 LONG 做任何事情. Oracle 仍然在数据字典中使用它们是一个 PITA。
您可以使用 XML:

select owner
, table_name
, column_name
, data_type
, data_length
, case
when data_precision is null then 0
else data_precision
end data_precision
, case
when data_scale is null then 0
else data_scale
end data_scale
, nullable
, column_id
, default_length
, case
when default_length is null then '0'
else
extractvalue
( dbms_xmlgen.getxmltype
( 'select data_default from user_tab_columns where table_name = ''' || c.table_name || ''' and column_name = ''' || c.column_name || '''' )
, '//text()' )
end as data_default
from all_tab_columns c
where table_name like 'TABLE1';
从 12.1 开始,您可以内联编写自己的查找函数:
with
function get_default(tab varchar2, col varchar2) return varchar2
as
dflt varchar2(4000);
begin
select c.data_default into dflt
from user_tab_columns c
where c.table_name = upper(tab)
and c.column_name = upper(col);

return dflt;
end get_default;
select owner
, table_name
, column_name
, data_type
, data_length
, case
when data_precision is null then 0
else data_precision
end data_precision
, case
when data_scale is null then 0
else data_scale
end data_scale
, nullable
, column_id
, default_length
, get_default(c.table_name, c.column_name) as data_default
from all_tab_columns c
where table_name like 'TABLE1%'
/
或者当然制作一个独立的函数或包函数来做同样的事情。

关于sql - Oracle 查询 all_tab_columns.data_default(类型 LONG),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45009225/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com