我需要对生成的生成列名运行查询。
以下是查询:
select 'col_'||4 from MY_TABLE
注意:
答案 0 :(得分:3)
在Oracle内部,您需要使用动态SQL。 (对于您的示例,YourVariable值为4)
EXECUTE IMMEDIATE ' select col_' || YourVariable || ' from MY_TABLE ';
从Java可以构建任何SQL并执行它们
答案 1 :(得分:1)
要运行动态SELECT语句,您有两种选择:
对于单行选择,您使用EXECUTE IMMEDIATE ... INTO:
EXECUTE IMMEDIATE 'select col_' || l_num || ' from MY_TABLE WHERE id = 37' INTO l_result;
要选择多行,您可以使用动态光标:
DECLARE
TYPE MyCurType IS REF CURSOR;
my_cv MyCurType;
BEGIN
OPEN emp_cv FOR 'select col_' || l_num || ' from MY_TABLE';
...
END;
答案 2 :(得分:1)
此代码生成一个SELECT,返回带有列名的表:
SELECT
'SELECT ' ||(
SELECT
LISTAGG(
c.TABLE_NAME || '.' || c.COLUMN_NAME || ' AS "' || c.TABLE_NAME || '.' || c.COLUMN_NAME || '"',
', '
) WITHIN GROUP(
ORDER BY
c.TABLE_NAME
) "TABLE_NAMES"
FROM
USER_TAB_COLS c
WHERE
TABLE_NAME IN(
'PESSOA',
'PESSOA_FISICA',
'PESSOA_JURIDICA'
)
)|| 'FROM PERSON;'
FROM
DUAL;