No, i am not looking at that.
I will rephrase my problem in a more clear manner.
Look at the code that i am pasting below.
Assumption:
For a given table there is only 1 primary key column.
Objective:
Provide table name
Provide primary key value
Result: retreive values from the tablename as per the primary key value passed.
--- Code start here
create or replace procedure sp_proc_1
(
ls_tablename user_tables.table_name%type,
ls_pkval1 varchar2
)
is
-- Fetch the primary key column
cursor cur_metadata
is
SELECT uc.table_name, ucc.column_name
FROM user_constraints uc, user_cons_columns ucc
WHERE uc.constraint_name = ucc.constraint_name
AND uc.constraint_type = 'P'
AND uc.table_name = ls_tablename
ORDER BY ucc.POSITION;
cursor cur_metadata_1
is
SELECT column_name
FROM user_tab_columns
WHERE table_name = ls_tablename
ORDER BY column_id;
ll_totcolumns := ll_totcolumns + 1;
dbms_output.put_line('ls_wherestring ' || ls_wherestring);
end loop;
close cur_metadata;
open cur_metadata_1;
loop
fetch cur_metadata_1 into metadata_1_cur;
exit when cur_metadata_1%notfound;
ls_selectstring_1 := ls_selectstring_1 || rtrim(upper(metadata_1_cur.column_name)) || ',';
end loop;
close cur_metadata_1;
-- [START] PROBLEM STARTS HERE
-- Dynamic cursor execution
open dyna_cursor_def for ls_selectstring_1;
loop
fetch dyna_cursor_def into dyna_cursor;
exit when dyna_cursor_def%notfound;
dbms_output.put_line('Rownum : ' || dyna_cursor_def%rowcount);
-- i want to fetch the column name here, so that i can trace the values
-- but there should not be any hardcoding of table names
-- the record type variable should be dynamic in nature
end loop;
close dyna_cursor_def;
-- [COMPLETED] PROBLEM ENDS HERE
A more complex alternative would be to use DBMS_SQL to
1) PARSE the query
2) DESCRIBE the result set
3) DEFINE_COLUMN according to the results from (2)
4) Loop round with FETCH_ROWS for each row returned (in this case there should be 1 as you are using a primary key)
4a) Loop round with COLUMN_VALUE for each column in the row
Bookmarks