我想做选择 column_name
从 table_name
哪儿 column_name
.
table_name
应该是来自游标。
查询。
DECLARE
COLUMN_NAME VARCHAR(50);
TABLE_NAME VARCHAR(100);
schema_name VARCHAR(100);
A VARCHAR(100);
B VARCHAR(100);
CURSOR col_cursor IS
select col.owner as schema_name,
col.table_name,
col.column_name
from sys.all_tab_columns col
inner join sys.all_tables t
on col.owner = t.owner and
col.table_name = t.table_name
where col.owner = 'PIYUSH1910_BEFORE'
AND
DATA_TYPE = 'NUMBER'
AND
DATA_PRECISION IS NULL
AND
col.TABLE_NAME NOT LIKE '%ER%';
BEGIN
OPEN col_cursor;
LOOP
FETCH col_cursor INTO schema_name,TABLE_NAME,COLUMN_NAME;
EXIT WHEN col_cursor%NOTFOUND;
EXECUTE IMMEDIATE ' SELECT '||COLUMN_NAME ||' INTO A from ' || Table_Name || 'WHERE'||COLUMN_NAME||'- TRUNC('||COLUMN_NAME||',2) > 0';
dbms_output.Put_line(A);
END LOOP;
CLOSE col_cursor;
END
但它却抛出了这个错误
错误报告 - ORA -00933: SQL命令没有正确结束 ORA-06512:在第29行00933. 00000 - "SQL命令未正确结束 *Cause: *错误报告 - ORA-00933:SQL命令未正确结束。
INTO
子句必须放在最后,而使用 EXECUTE IMMEDIATE
如下所示。
EXECUTE IMMEDIATE ' SELECT '||COLUMN_NAME ||' from '
|| Table_Name || ' WHERE '||COLUMN_NAME||'- TRUNC('||COLUMN_NAME||',2) > 0'
INTO A;
另外,前后的空格 WHERE
在上面的解决方案中加入了关键字。
将EXECUTE IMMEDIATE改为。
EXECUTE IMMEDIATE ' SELECT '|| COLUMN_NAME || ' from ' || Table_Name ||
' WHERE ' || COLUMN_NAME || '- TRUNC('||COLUMN_NAME||',2) > 0'
INTO A;
这就把 "EXECUTE IMMEDIATE "移到了 INTO
子句到正确的位置(在本例中它是 EXECUTE IMMEDIATE
而不是属于 SELECT
),并略微重新编写代码,使列名与例如 WHERE
关键字