JSP sql - set table name in variable
I'm trying to set my table name in variable, what is the syntax in sql of selecting from a table which name is set with a variable name?
That's easy :
Say, ur table name is MY_TABLE.
Now, set a variable say x as :
var x = "MY_TABLE"; (Javascript)
or
String x = "MY_TABLE" (JSP/Servlet);
Finally, use the SQL as,
String SQL = "SELECT * FROM " + x + "WHERE condition". (JSP/Servlet).
or
var SQL = "SELECT * FROM " + x + "WHERE condition". (Javascript).
and BINGO !!
Do keep me posted.
Cheers !!
Sherbir
Similar Messages
-
Dynamic SQL : passing table name as parameter
Hi
I have a SQL query (a store procedure ) that i want to convert to PLSQL
This is a part of my SQL query that i am trying to to find a solution for it, because i cant convert it to oracle :
DECLARE lookupTableRow CURSOR FOR
SELECT TableName FROM SYS_LookUpTable
OPEN lookupTableRow
FETCH NEXT FROM lookupTableRow INTO @tableName
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql='SELECT * FROM '+@tableName
EXECUTE sp_executesql @sql
IF @counter=0
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName)
END
ELSE
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table'+CONVERT(NVARCHAR(10),@counter), @tableName)
END
SET @counter=@counter+1
FETCH NEXT FROM lookupTableRow INTO @tableName
END
CLOSE lookupTableRow
DEALLOCATE lookupTableRow
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
Furthermore when i execute this dynamic query in my SQL store procedure each SELECT statement return me as a result the relevant table rows , those result are different in each loop .
So i cant do this too with ORACLE dynamic sql .
Please advice for any solution
* how can i use dynamic sql with table name as parameter ?
* how can i use a "dynamic" cursor, in order to be able to display the dynamic results ?
Thanks for the adviceHi,
b003cf5e-e55d-4ff1-bdd2-f088a662d9f7 wrote:
Hi
I have a SQL query (a store procedure ) that i want to convert to PLSQL
This is a part of my SQL query that i am trying to to find a solution for it, because i cant convert it to oracle :
DECLARE lookupTableRow CURSOR FOR
SELECT TableName FROM SYS_LookUpTable
OPEN lookupTableRow
FETCH NEXT FROM lookupTableRow INTO @tableName
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql='SELECT * FROM '+@tableName
EXECUTE sp_executesql @sql
IF @counter=0
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table', @tableName)
END
ELSE
BEGIN
INSERT INTO T_TABLE_MAPPING VALUES('P_MAIN_METADATA', 'Table'+CONVERT(NVARCHAR(10),@counter), @tableName)
END
SET @counter=@counter+1
FETCH NEXT FROM lookupTableRow INTO @tableName
END
CLOSE lookupTableRow
DEALLOCATE lookupTableRow
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
Furthermore when i execute this dynamic query in my SQL store procedure each SELECT statement return me as a result the relevant table rows , those result are different in each loop .
So i cant do this too with ORACLE dynamic sql .
Please advice for any solution
* how can i use dynamic sql with table name as parameter ?
* how can i use a "dynamic" cursor, in order to be able to display the dynamic results ?
Thanks for the advice
I have a SQL query (a store procedure ) that i want to convert to PLSQL
I doesn't help when you use one term to mean another thing.
SQL is a language used in both Oracle and other products, such as Microsoft's SQL Server. I don't know much about SQL Server, but Oracle (at least) doesn't support stored procedures in SQL itself; they have to be coded in some other language, such as PL/SQL.
As i understand i can't use ORACLE dynamic sql (execute immediate) when the table name is a parameter
If the table name is a parameter (or only known at run-time for any reason), that's exactly the kind of situation where you MUST use dynamic SQL.
The number of columns that a query produces (and their datatypes) is fixed when you compile a query, whether that query is dynamic or not. If you have multiple queries, that produce result sets with different numbers of columns, then you can't combine them into a single query. The best you can do with one query is to add NULL columns to some of the queries so they all produce the same number of columns.
If you're just displaying the results, there might not be any reason to combine separate result sets. Just display one result set after another.
Whenever you have a question, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using (e.g. 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
How to dynamic set table names
In our application, the table name can be set at runtime by the customer. So is there a way in Toplink JPA to set table name at runtime (thru annotation or orm.xml) or programatically? Thanks.
Our application will host several domains at once. Each domain will get its own set of database tables for each module it uses. We keep them seperate by using a unique prefix. An example would be domainA.com's blog post table would be called domainA_blogPost, while domainB.com's will be domainB_blogPost. Both tables are the same, except for the name. The name needs to be specified at runtime since the central service controller tells the rest of the application which domain it is servicing and thus which set of tables to use. We will be adding domains dynamically all the time and we can't afford to create new entity sets and recompile the back end for every domain we add. I have been looking at the API and wonder if this is as simple as implementing a solution in our application, or am I going to have to tweek the implementation of toplink?
-
Invalid table name when pass in the table name as variable in dynamic sql
Hi,
I need to create a stored procedure which will return a list of data to my java application like the following.
first, select the table name from the first table.
For example : SELECT T_NAME FROM MDR_SMSTABLES
second, select the data from the table which returned by first select statement
For example : SELECT * FROM T_NAME.
I use dynamic sql with cursor to select the data. But it returns "java.sql.SQLException: ORA-00903: invalid table name"
I fetch the table name to varchar2. I think this might the cause it returns me the this error. But i don't know what type should i put for the table name other then varchar2.
val2 VARCHAR2(200);
OPEN cv FOR
SELECT T_NAME FROM MDR_SMSTABLES WHERE T_DATE=d_dt_sent;
FETCH cv INTO val2;
WHILE cv%FOUND
LOOP
OPEN refcur FOR
'SELECT * FROM :t WHERE MID = :m' USING val2, msg_id;
EXIT WHEN refcur IS NOT NULL;
FETCH cv INTO val2;
END LOOP;
As my stored procedure is quite long, so i just paste some of the code here. Hope the information is enough. Can anyone please help?
ThanksDECLARE
val2 VARCHAR2 (200);
cv sys_refcursor;
refcur sys_refcursor;
BEGIN
OPEN cv FOR
SELECT table_name
FROM user_tables
WHERE table_name IN ('EMP', 'DEPT');
FETCH cv INTO val2;
WHILE cv%FOUND
LOOP
OPEN refcur FOR 'SELECT * FROM '||val2;
EXIT WHEN refcur IS NOT NULL;
FETCH cv INTO val2;
END LOOP;
END; -
Passing table names through variables
Hi All,
Is it possible to pass the table name in a query using a variable.
For example - can we perform a select on a table where the table name is passed using a variable.
Pl assist. Thanks.Hello,
Are you wanting to do this in a procedure? Say the table name being passed in as Input Parameter?
We can do that using Dynamic SQL:
CREATE PROCEDURE "PROC_01"
(IN v_IN_Var1 NVARCHAR)
LANGUAGE SQLSCRIPT AS
BEGIN
DECLARE ABC NVARCHAR(300) := NULL;
ABC := 'SELECT * FROM ' || :v_IN_Var1;
EXEC (:ABC);
END;
CALL PROC_01 ('DUMMY');
But the only thing is you'll not be able to see the result set directly.
You might wan2 fire the select to insert the data into a different table. -
Hello there,
I have a question on the following:
I want to read from a series of tables which a different suffix such as
table123, table234 and table110
How can I put them in a loop and extract data from them
for x in y loop
select abc, edf, .......
from tablexxx;
end loop;
I have tried the execute immediate command but the sql was loo long.
Anybody has any idea? Thanks in advance.I have tried the execute immediate command but the
sql was loo long.So you already know that if you have "dynamic" tables then you need "dynamic" sql.
It's at this point you should ask yourself if "dynamic" tables are really the best solution for your database design.
Clearly, any application written against the database needs to know what tables it is accessing and, likewise, this should make you think that it's perhaps a good idea that the tables are known at the time the application is designed.
What is your requirement for having tables with similar names but different suffix's?
The classic design fault is people who create seperate tables for certain time periods i.e. each month a new table is created for that month of that year. This of course leads to the problem of dynamic tables and dynamic queries.
In such a situation, the better design would be one of the following...
a) if you have the partitioning option on your oracle database, create your table with partitions for the required sub sets of data. Partitions can be added, truncated or removed without the table name changing. They can also help the queries to perform faster as the optimiser will be able to determine what partitions it needs to access to get the data.
b) if you don't have partitioning then consider including relevant columns in the table to provide the relevant sectioning of data. With suitable indexes this can be just as performant, even with a lot of data.
;) -
How to use table name as variable in insert statement
Hi,
I want to insert data from the cursor into the table and I want to submit table name as a variable. I tried the code below but it's not working. Any ideas how to solve this problem? Thx
DECLARE
v_new_table_name VARCHAR2(30) := 'test';
--open cursor
CURSOR ACM IS
SELECT *
FROM DWH.CLI_DIMENSION@SLSPDW CLI
WHERE where_clause
FROM some_table;
BEGIN
FOR REC_ACM IN ACM LOOP
EXECUTE IMMEDIATE 'INSERT INTO ' || v_new_table_name || ' VALUES REC_ACM'; --table name as a variable in which the data from cursor will be inserted
V_ROWCOUNT := V_ROWCOUNT + 1;
IF MOD(V_ROWCOUNT, 100) = 0 THEN --this feature commits after 100 rows.
COMMIT;
END IF;
END LOOP;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Number of inserted records:' || V_ROWCOUNT);
END;Try this and let me know if it works. I have not tested it so I can not say for sure.
DECLARE
v_new_table_name VARCHAR2(30) := 'test';
CURSOR ACM IS
SELECT * FROM DWH.CLI_DIMENSION@SLSPDW CLI
WHERE where_clause
FROM some_table;
BEGIN
loop
fetch ACM INTO REC_ACM
exit when ACM%NOTFOUND;
EXECUTE IMMEDIATE 'INSERT INTO ' || v_new_table_name || ' VALUES REC_ACM';
V_ROWCOUNT := V_ROWCOUNT + 1;
IF MOD(V_ROWCOUNT, 100) = 0 THEN
COMMIT;
END IF;
end loop;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Number of inserted records:' || V_ROWCOUNT);
END; -
Value set table name in fusion app
Hi Team,
Can any one please tell me the table name which is used in fusion app to store the value set name which we create.
Thanks,AjaySorry for the confusion, the FND tables are indeed in the OER and the FND_VS_VALUE_SETS can be found there. There is a data issue where content (prior of R8) was in correctly categorized such that its not available under "technology" product family and my query failed since used combination of both criteria. In any case just use "All versions" for the version criteria and the data will be returned. They are internally working to fix the product family for previous releases..
Jani Rautiainen
Fusion Applications Developer Relations
https://blogs.oracle.com/fadevrel/ -
Read table, table name in variable
Hi all,
I am try trying to do the following.
Select statement to get the table name --> store in tname (variable).
Now, I have to do a select to read the data in this table (table name is in tname variable) based on some conditions
How do I do this?
1st step
Select ME_TECH from RSAC into tname
where NAME = 'PLANT'
and DATEFROM <= ldate
and DATETO >= ldate.
2nd step??
Thanks,
Anirudh.report ztest.
parameters: p_table type dd02l-tabname.
field-symbols: <fs> type standard table." of (p_table).
select * from (p_table)
into table <fs>
up to 20 rows.
There is another way using CL_ALV_TABLE_CREATE, for this you have to pass the fieldcatalog.
it return the internal table reference. using that you can proceed. -
Using table names as variables
Hello!
I have a table containing only 2 columns - SEQUENCE_NUMBER and TABLE_NAME.
The sequence numbers are unique numbers and this table shows in which table a certain sequence number can be found. For example:
table cross:
SN | TABLE_NAME
1 | table1
2 | table1
3 | table3
4 | table2Each one of the tables referenced in the TABLE_NAME column has a column named OPERATION, for example:
table table1:
SEQNUM | OPERATION
1 | i
2 | u
</code>table table2:
SEQNUM | OPERATION
4 | d
What I want to achieve is to display all operations in order:
SN | OPERATION
1 | i
2 | u
3 | ...
4 | d
and so onAnd I succeeded with this pl/sql:
DECLARE
text VARCHAR2 (100) := '';
op VARCHAR2 (1) := '';
CURSOR c_tmp
IS
SELECT *
FROM CROSS
ORDER BY sn;
BEGIN
FOR tmp IN c_tmp
LOOP
text := 'select operation from ' || tmp.table_name || ' where seqnum = ' || tmp.sn;
DBMS_OUTPUT.put_line (text);
EXECUTE IMMEDIATE text INTO op;
DBMS_OUTPUT.put_line ('operation: ' || op);
END LOOP;
END;Now I want to extend the functionality of the code. Suppose there are more fields in every table and every one of the tables (table*) has a different number of fields, for example:
table table1:
SEQNUM | OPERATION | COL1 | COL2 | COL3
1 | i | bla | foo | bar
2 | u | blq | foq | baq
</code>table table2:
SEQNUM | OPERATION | COL1 | COL2
4 | d | 111 | 222
Now I want (using SELECT IMMEDIATE) to select not only the operation of every row, but the whole row (!) and then do something with the other fields, like:
BEGIN
FOR tmp IN c_tmp
LOOP
text := 'select * from ' || tmp.table_name || ' where seqnum = ' || tmp.sn;
DBMS_OUTPUT.put_line (text);
EXECUTE IMMEDIATE text INTO whole_record;
DBMS_OUTPUT.put_line ('field1: ' || whole_record.operation);
DBMS_OUTPUT.put_line ('field1: ' || whole_record.col1);
END LOOP;
END;How is that possible? The type of the "whole_record" variable is defined by the cursor c_tmp, which means, that the variable must be declared in the beginning of the loop!
How can I do that?
Regards.
Edited by: user10962462 on Dec 11, 2009 7:48 AMSounds like you want to dynamically extract the columns at run time when you don't know what they are at design time.
Generally this is poor design and practice, but there are a few applications where it's handy e.g...
The following allows any query to be executed and the data retrieved (in this case to generate a CSV file)...
As sys user:
CREATE OR REPLACE DIRECTORY TEST_DIR AS '\tmp\myfiles'
GRANT READ, WRITE ON DIRECTORY TEST_DIR TO myuser
/As myuser:
CREATE OR REPLACE PROCEDURE run_query(p_sql IN VARCHAR2
,p_dir IN VARCHAR2
,p_header_file IN VARCHAR2
,p_data_file IN VARCHAR2 := NULL) IS
v_finaltxt VARCHAR2(4000);
v_v_val VARCHAR2(4000);
v_n_val NUMBER;
v_d_val DATE;
v_ret NUMBER;
c NUMBER;
d NUMBER;
col_cnt INTEGER;
f BOOLEAN;
rec_tab DBMS_SQL.DESC_TAB;
col_num NUMBER;
v_fh UTL_FILE.FILE_TYPE;
v_samefile BOOLEAN := (NVL(p_data_file,p_header_file) = p_header_file);
BEGIN
c := DBMS_SQL.OPEN_CURSOR;
DBMS_SQL.PARSE(c, p_sql, DBMS_SQL.NATIVE);
d := DBMS_SQL.EXECUTE(c);
DBMS_SQL.DESCRIBE_COLUMNS(c, col_cnt, rec_tab);
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
WHEN 2 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_n_val);
WHEN 12 THEN DBMS_SQL.DEFINE_COLUMN(c,j,v_d_val);
ELSE
DBMS_SQL.DEFINE_COLUMN(c,j,v_v_val,2000);
END CASE;
END LOOP;
-- This part outputs the HEADER
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_header_file,'w',32767);
FOR j in 1..col_cnt
LOOP
v_finaltxt := ltrim(v_finaltxt||','||lower(rec_tab(j).col_name),',');
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
IF NOT v_samefile THEN
UTL_FILE.FCLOSE(v_fh);
END IF;
-- This part outputs the DATA
IF NOT v_samefile THEN
v_fh := UTL_FILE.FOPEN(upper(p_dir),p_data_file,'w',32767);
END IF;
LOOP
v_ret := DBMS_SQL.FETCH_ROWS(c);
EXIT WHEN v_ret = 0;
v_finaltxt := NULL;
FOR j in 1..col_cnt
LOOP
CASE rec_tab(j).col_type
WHEN 1 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_v_val);
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
WHEN 2 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_n_val);
v_finaltxt := ltrim(v_finaltxt||','||v_n_val,',');
WHEN 12 THEN DBMS_SQL.COLUMN_VALUE(c,j,v_d_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
ELSE
v_finaltxt := ltrim(v_finaltxt||',"'||v_v_val||'"',',');
END CASE;
END LOOP;
-- DBMS_OUTPUT.PUT_LINE(v_finaltxt);
UTL_FILE.PUT_LINE(v_fh, v_finaltxt);
END LOOP;
UTL_FILE.FCLOSE(v_fh);
DBMS_SQL.CLOSE_CURSOR(c);
END;This allows for the header row and the data to be written to seperate files if required.
e.g.
SQL> exec run_query('select * from emp','TEST_DIR','output.txt');
PL/SQL procedure successfully completed.Output.txt file contains:
empno,ename,job,mgr,hiredate,sal,comm,deptno
7369,"SMITH","CLERK",7902,17/12/1980 00:00:00,800,,20
7499,"ALLEN","SALESMAN",7698,20/02/1981 00:00:00,1600,300,30
7521,"WARD","SALESMAN",7698,22/02/1981 00:00:00,1250,500,30
7566,"JONES","MANAGER",7839,02/04/1981 00:00:00,2975,,20
7654,"MARTIN","SALESMAN",7698,28/09/1981 00:00:00,1250,1400,30
7698,"BLAKE","MANAGER",7839,01/05/1981 00:00:00,2850,,30
7782,"CLARK","MANAGER",7839,09/06/1981 00:00:00,2450,,10
7788,"SCOTT","ANALYST",7566,19/04/1987 00:00:00,3000,,20
7839,"KING","PRESIDENT",,17/11/1981 00:00:00,5000,,10
7844,"TURNER","SALESMAN",7698,08/09/1981 00:00:00,1500,0,30
7876,"ADAMS","CLERK",7788,23/05/1987 00:00:00,1100,,20
7900,"JAMES","CLERK",7698,03/12/1981 00:00:00,950,,30
7902,"FORD","ANALYST",7566,03/12/1981 00:00:00,3000,,20
7934,"MILLER","CLERK",7782,23/01/1982 00:00:00,1300,,10The procedure allows for the header and data to go to seperate files if required. Just specifying the "header" filename will put the header and data in the one file.
Adapt to output different datatypes and styles are required. -
How to use table name as variable
P.s. I wish to send a tablename through a function as a variable
and then want to make a use in sql query. How can I make a use
of it.
nullRajeev Grover (guest) wrote:
: P.s. I wish to send a tablename through a function as a
variable
: and then want to make a use in sql query. How can I make a use
: of it.
Use the dbms_sql package.
E.g.
DECLARE
tbl_name VARCHAR2(40);
cur_stmt VARCHAR2(2000);
cur_hndl INTEGER;
BEGIN
cur_stmt :=
'SELECT column1, column2, column3, ... ' -
Dynamic table names without dynamic sql?
I am trying to figure out a way to use table names as variables without using dynamic sql.
So for instance, I have...
execute immediate 'INSERT INTO '||vTABLE||' VALUES ('test')';
and
execute immediate 'INSERT INTO TABLE1 SELECT * FROM '||vTable;
Is there any other way to do these without the dynamic sql in Oracle 10.2g?I am trying to figure out a way to use table names as variables without using dynamic sql. Well I guess it makes a change from perpetual motion machines or cold fusion.
Are you doing this as a purely academic exercise or do you have something against dynamic SQL?
Cheers, APC
blog: http://radiofreetooting.blogspot.com -
Providing table name dynamicaaly in SQL
Hi,
how can provide table name dynamically in an SQL query. no help of PL/SQL is possible in the scenario.
I am trying many possiblities but without any success. any1 knowling please let me know.
Regards,
Prashwell, depending on the requirments you can use XML to some extent:
SQL> var name varchar2(20);
SQL> exec :name:='scott.emp';
PL/SQL procedure successfully completed.
SQL> select 'Table "'||:name||'" has ' ||
2 extractvalue(dbms_xmlgen.getXMLType('select count(*) cnt from '||:name),
3 '//CNT') || ' rows'
4 from dual;
'TABLE"'||:NAME||'"HAS'||EXTRACTVALUE(DBMS_XMLGEN.GETXMLTYPE('SELECTCOUNT(*)CNTF
Table "scott.emp" has 14 rows
SQL> exec :name:='all_tables';
PL/SQL procedure successfully completed.
SQL> select 'Table "'||:name||'" has ' ||
2 extractvalue(dbms_xmlgen.getXMLType('select count(*) cnt from '||:name),
3 '//CNT') || ' rows'
4 from dual;
'TABLE"'||:NAME||'"HAS'||EXTRACTVALUE(DBMS_XMLGEN.GETXMLTYPE('SELECTCOUNT(*)CNTF
Table "all_tables" has 1622 rows -
Passing dynamic table name in ADO. net destination
I am new to SSIS and I have a requirement where i need to pass dynamic table name in ADO .net destination .
My package contains an "Execute Sql task" in the control flow which computes the destination table name to be provided at run time and stores it in a variable expression
"@[User::Table_name]"(which has a scope of full package).
Now, the problem I'm facing right now is that , I am unable to use this variable expression in the ADO .Net Destination .
I need to pass this variable expression as the table name in ado .net destination.
But, whenever I use this variable in place as the table name I keep on getting this error and my package fails:-
[ADO NET Destination [403]] Error: The Table or View name is not expected. \n\t If you are quoting the table name, please use the prefix " and the suffix " of your selected data provider for quotation. \n\t If you are using multipart name,
please use at most three parts for the table name.
Although ,I am able to run my package when i am providing the existing(static) table name.So there is nothing wrong with the package.
Tried a lot of things still not working..Please help...I am having the result stored in the variable expression . I just need a way to be able to use it as the Destination table in ADO .net Destination.
I am not sure if this will work for you, but I am able to store the table name in variable and use it dynamically (via data flow task expression property ) as shown.
Thanks, hsbal
Hi Adeep,
Based on my further research, just as hsbal said, we can set a variable as ADO.NET Destination table via Expressions property in Data Flow Task.
If there are any other questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
Parameterized cursor for varient Table name?
Hi all,
I am using Oracle 9i and have a cursor defined as :-
Code:
CREATE PROCEDURE Proc_Abc
AS
CURSOR
My_Cursor (UserName VARCHAR) IS
SELECT Emp_Name, Salary FROM Employee_Table
WHERE User_Name = UserName;
(Rest of the code)
This code is working perfectly, but if I try to provide the table name through the cursor variable, it gives an error
Below is the code that I am writing to pass table name through variable:-
Code:
CREATE PROCEDURE Proc_Abc
AS
CURSOR
My_Cursor (TableName VARCHAR, UserName VARCHAR) IS
SELECT Emp_Name, Salary FROM TableName
WHERE User_Name = UserName;
(Rest of the code)
All the tables that I need to pass through cursor variables have the same fields and are all pre known to me, thats why "SELECT Emp_Name, Salary " is remaining common throughout.
Please suggest how can I make a cursor with variant tables?
Thanks in advance.The following procedure compares between two tables and then it picking the column from all_tab_column table and finally execute the SELECT statement to compare the data between this two table. But, you can proceed your program taking help from this -
satyaki>ed
Wrote file afiedt.buf
1 create or replace procedure compr_tab_dat(TAR_TAB IN VARCHAR2,
2 TAR_UID IN VARCHAR2,
3 SRC_TAB IN VARCHAR2,
4 SRC_UID IN VARCHAR2)
5 is
6 cursor c1
7 is
8 select column_name
9 from (
10 select column_name,column_id
11 from all_tab_columns
12 where table_name = SRC_TAB
13 and owner = SRC_UID
14 intersect
15 select column_name,column_id
16 from all_tab_columns
17 where table_name = TAR_TAB
18 and owner = TAR_UID
19 )
20 order by column_id;
21 cursor c_count
22 is
23 select count(column_name) as c_cnt
24 from (
25 select column_name
26 from all_tab_columns
27 where table_name = SRC_TAB
28 and owner = SRC_UID
29 intersect
30 select column_name
31 from all_tab_columns
32 where table_name = TAR_TAB
33 and owner = TAR_UID
34 );
35 rec1 c1%rowtype;
36 rec2 c1%rowtype;
37 rec3 c1%rowtype;
38 rec6 c_count%rowtype;
39 cnt number(10);
40 cnt1 number(10);
41 str varchar2(32000);
42 --str clob;
43 BEGIN
44 cnt := 0;
45 cnt1 := 1;
46 dbms_output.enable(10000000);
47 for rec6 in c_count
48 loop
49 cnt := rec6.c_cnt;
50 end loop;
51 if cnt = 0 then
52 dbms_output.put_line('No matched columns found.... ');
53 else
54 dbms_output.put_line('UnMatched Datas Are-- ');
55 str:='declare '||
56 ' cursor c3 '||
57 ' is '||
58 ' select ';
59 open c1;
60 loop
61 fetch c1 into rec1;
62 exit when c1%notfound;
63 if cnt = cnt1 then
64 str:= str||rec1.column_name;
65 elsif cnt1<cnt then
66 str:= str||rec1.column_name||',';
67 end if;
68 cnt1 := cnt1 + 1;
69 end loop;
70 close c1;
71 str:=str||' from '||SRC_TAB||
72 ' minus '||
73 ' select ';
74 cnt1:=1;
75 open c1;
76 loop
77 fetch c1 into rec2;
78 exit when c1%notfound;
79 if cnt = cnt1 then
80 str:= str||rec2.column_name;
81 elsif cnt1<cnt then
82 str:= str||rec2.column_name||',';
83 end if;
84 cnt1 := cnt1 + 1;
85 end loop;
86 close c1;
87 str:=str||' from '||TAR_TAB||';'||
88 ' r3 c3%rowtype; '||
89 ' begin '||
90 ' for r3 in c3 '||
91 ' loop '||
92 ' dbms_output.put_line(';
93 cnt1:=1;
94 open c1;
95 loop
96 fetch c1 into rec3;
97 exit when c1%notfound;
98 if cnt = cnt1 then
99 str:= str||' r3.'||rec3.column_name;
100 elsif cnt1<cnt then
101 str:= str||' r3.'||rec3.column_name||
102 '||'',''||';
103 cnt1 := cnt1 + 1;
104 end if;
105 end loop;
106 close c1;
107 str:=str||');'||
108 ' end loop;'||
109 ' exception '||
110 ' when others then '||
111 ' dbms_output.put_line(sqlerrm); '||
112 ' end; ';
113 end if;
114 execute immediate(str);
115 --dbms_output.put_line(str);
116 exception
117 when others then
118 dbms_output.put_line(sqlerrm);
119* END;
120 /
Procedure created.
satyaki>
satyaki>
satyaki>
satyaki>create table emp_t
2 as
3 select * from emp
4 where rownum < 5;
Table created.
satyaki>
satyaki>
satyaki>desc emp;
Name Null? Type
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
satyaki>
satyaki>
satyaki>desc emp_t;
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
satyaki>set lin 1000
satyaki>
satyaki>select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
satyaki>
satyaki>
satyaki>select * from emp_t;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
satyaki>
satyaki>
satyaki>set serveroutput on
satyaki>
satyaki>
satyaki>begin
2 compr_tab_dat('EMP_T','SCOTT','EMP','SCOTT');
3 end;
4 /
No matched columns found....
ORA-06535: statement string in EXECUTE IMMEDIATE is NULL or 0 length
PL/SQL procedure successfully completed.
satyaki>
satyaki>
satyaki>sho user;
USER is "TRG2"
satyaki>
satyaki>
satyaki>
satyaki>
satyaki>begin
2 compr_tab_dat('EMP_T','TRG2','EMP','TRG2');
3 end;
4 /
5 .
satyaki>
satyaki>ed
Wrote file afiedt.buf
1 begin
2 compr_tab_dat('EMP_T','TRG2','EMP','TRG2');
3* end;
satyaki>/
UnMatched Datas Are--
7654,MARTIN,SALESMAN,7698,28-SEP-81,1250,1400,30
7698,BLAKE,MANAGER,7839,01-MAY-81,2850,,30
7782,CLARK,MANAGER,7839,09-JUN-81,2450,,10
7788,SCOTT,ANALYST,7566,19-APR-87,3000,,20
7839,KING,PRESIDENT,,17-NOV-81,5000,,10
7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
7876,ADAMS,CLERK,7788,23-MAY-87,1100,,20
7900,JAMES,CLERK,7698,03-DEC-81,950,,30
7902,FORD,ANALYST,7566,03-DEC-81,3000,,20
7934,MILLER,CLERK,7782,23-JAN-82,1300,,10
PL/SQL procedure successfully completed.N.B.: May be Any other member can come with much shorter or better technique than this one. But, according to your requirement - i'm posting it. Hope this will help you, or atleast give you some idea.
Regards.
Satyaki De.
Maybe you are looking for
-
Can two users on two separate computers share their screens?
I often conduct training sessions and demos in a conference room with a large monitor mounted on the wall. I often have remote participants, so I use Lync or Live Meeting, along with a dial-in number for the audio. Lately I've been wanting to share n
-
Dear All, In which scenario the DIF account gets hit in SAP.... please guide cheers
-
Under settings i mistakenly erased my downloaded files. is there a way to restore those files?
-
My app store update page is blocking
since I put the iOS 6.1 update my app store is blocked. This is the "update" which remains white despite the tests change the place, put the wifi etc ... the label tells me three updates but I can not install
-
An error occurred while executing the test. There was an error getting information about the SAS controllers installed on the nodes. There was an error retrieving information about the SAS host bus adapters from node Invalid class Successfully put