-1

I want to do select column_name from table_name where column_name.

table_name should be coming from cursor.

Query:

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

But its throwing this error:

Error report - ORA-00933: SQL command not properly ended. ORA-06512: at line 29 00933. 00000 - "SQL command not properly ended *Cause: *Action:

William Robertson
  • 15,273
  • 4
  • 38
  • 44
Piyush Ranjan
  • 29
  • 1
  • 8
  • Above code is giving error like below Error report - ORA-00933: SQL command not properly ended. ORA-06512: at line 29 00933. 00000 - "SQL command not properly ended *Cause: *Action: – Piyush Ranjan May 08 '20 at 14:39
  • Could you check the solution https://stackoverflow.com/questions/61663260/find-a-column-and-a-value-from-a-schema-scan-all-the-table-in-oracle/61663782?noredirect=1#comment109076196_61663782 – VN'sCorner May 08 '20 at 14:49

2 Answers2

1

INTO clause must be at the end while using the EXECUTE IMMEDIATE as follows:

EXECUTE IMMEDIATE ' SELECT '||COLUMN_NAME ||' from ' 
        || Table_Name || ' WHERE '||COLUMN_NAME||'- TRUNC('||COLUMN_NAME||',2) > 0' 
        INTO A;

Also, space before and after WHERE keyword is added in the above solution.

Popeye
  • 35,427
  • 4
  • 10
  • 31
0

Change your EXECUTE IMMEDIATE to:

EXECUTE IMMEDIATE ' SELECT '|| COLUMN_NAME || ' from ' || Table_Name || 
                  ' WHERE ' || COLUMN_NAME || '- TRUNC('||COLUMN_NAME||',2) > 0'
  INTO A;

This moves the INTO clause to the correct position (in this case it's part of the EXECUTE IMMEDIATE rather than being part of the SELECT) and reformats the code slightly so that column names are separated from e.g. the WHERE keyword.