我有一个声明为
的类型TYPE "TABLE_TYPE" AS TABLE OF varchar2(4000)
如何转换我在查询中选择的列以返回该类型。
例如
Select cast(to_char(sysdate) as TABLE_TYPE) from dual
返回ORA-00932: inconsistent datatypes: expected - got CHAR
错误。有没有办法将varchar2的列转换为用户定义的类型?
答案 0 :(得分:3)
如果你真的想要返回一个带有单个元素的集合,你只需要调用该类型的构造函数
SQL> create type table_type as table of varchar2(100);
2 /
Type created.
SQL> select table_type( to_char( sysdate ))
2 from dual;
TABLE_TYPE(TO_CHAR(SYSDATE))
-----------------------------------------------------------------------------
TABLE_TYPE('14-FEB-13')
但是,通常情况下,在创建集合时,目的是通过运行返回多行的查询来使用多个元素填充它。为此,您可能希望执行类似
的操作DECLARE
l_strs table_type;
BEGIN
SELECT ename
BULK COLLECT INTO l_strs
FROM emp;
<<l_strs now contains 1 element for each row in the EMP table>>
END;
答案 1 :(得分:3)
使用cast + multiset
select cast(multiset(
select to_char(sysdate-level, 'YYYYMMDD')
from dual
connect by level <= 10
) as t_vchar_tab)
from dual;
答案 2 :(得分:2)
Union causing an ORA-01790: expression must have same datatype as corresponding expression
CREATE OR REPLACE TYPE varchar2_ntt AS TABLE OF VARCHAR2(4000);
/
SELECT deptno
, CAST(COLLECT(ename) AS varchar2_ntt) AS emps
FROM scott.emp
GROUP BY deptno
/
答案 3 :(得分:1)
select table_type(to_char(sysdate)) from dual;