gpt4 book ai didi

oracle - 请 00302 : component DATA_TYPE must de declared in function

转载 作者:行者123 更新时间:2023-12-02 06:24:05 29 4
gpt4 key购买 nike

我必须遵循 Oracle 函数 build_select,它创建一个选择请求。返回值格式如下:

select col1 ||'|'||col2 ||'|'||col3 from table;

下面是 build_select 函数:

create or replace FUNCTION build_select (
p_table_name IN VARCHAR2
)
RETURN VARCHAR2
AS
l_ret VARCHAR2 (32767);
BEGIN
FOR eachcol IN ( SELECT column_name, data_type
, LEAD (column_name), LEAD (data_type)
OVER (
PARTITION BY table_name ORDER BY column_id
)
next_column
FROM all_tab_cols
WHERE table_name = p_table_name
ORDER BY column_id)
LOOP
IF eachcol.data_type = 'CLOB' THEN
l_ret := l_ret || dbms_lob.substr( eachcol.column_name, 3000, 1 ) || CASE WHEN eachcol.next_column IS NULL THEN NULL ELSE ' ||''|''||' END;
ELSE
l_ret := l_ret || eachcol.column_name || CASE WHEN eachcol.next_column IS NULL THEN NULL ELSE ' ||''|''||' END;
END IF;
END LOOP;

IF l_ret IS NULL
THEN
raise_application_error (-20001, 'table ' || p_table_name || ' not found');
END IF;

l_ret := 'select ' || l_ret || ' from ' || p_table_name || ';';

RETURN l_ret;
END build_select;

我想要做的是测试列的数据类型是否为 CLOB,如果是,则将其返回

dbms_lob.substr( eachcol.column_name, 3000, 1 )

我在循环部分添加了 if else 条件。但我收到错误:

PLS 00302 : component DATA_TYPE must de declared.

请帮忙吗?

我需要这样做,因为当我对返回的选择进行假脱机操作时,它不会返回 CLOB 数据类型的所有列。

最佳答案

我认为你的功能应该是这样的:

create or replace FUNCTION build_select (
p_table_name IN VARCHAR2
)
RETURN VARCHAR2
AS
l_ret VARCHAR2 (32767);
BEGIN
FOR eachcol IN ( SELECT column_name, data_type
FROM all_tab_cols
WHERE table_name = p_table_name
ORDER BY column_id)
LOOP
IF eachcol.data_type = 'CLOB' THEN
l_ret := l_ret || 'dbms_lob.substr( '||eachcol.column_name||', 3000, 1 ),';
ELSE
l_ret := l_ret || eachcol.column_name||',';
END IF;
END LOOP;

IF l_ret IS NULL
THEN
raise_application_error (-20001, 'table ' || p_table_name || ' not found');
END IF;

l_ret := 'select ' || regexp_replace(l_ret, ',$', NULL) || ' from ' || p_table_name || ';';

RETURN l_ret;
END build_select;

请注意,ALL_TAB_COLS 还选择系统生成的隐藏列和不可见列,这可能会出现问题。如果您想过滤它们,请查询ALL_TAB_COLUMNS

关于oracle - 请 00302 : component DATA_TYPE must de declared in function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49025961/

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