Home » SQL & PL/SQL » SQL & PL/SQL » Tablename as input parameter
Tablename as input parameter [message #606768] Wed, 29 January 2014 03:49 Go to next message
sarma.shp
Messages: 111
Registered: February 2012
Senior Member
Hi All,
I am working on a stored procedure where i am giving two tablenames as input parameters and used them in a cursor to fetch the count of the data. But on compiling, it is giving me the error as " no table-name or view exists " . As i want to give the table name at runtime, i have used some dummy name in the procedure. Please suggest me, where i am going wrong!! Below is the code

CREATE OR REPLACE PROCEDURE ABC AS (TAB_NAME1 NVARCHAR2, TAB_NAME2 NVARCHAR2)
AS 
TAB_NAME1_CNT NUMBER := 0;
TAB_NAME2_CNT NUMBER := 0;

BEGIN 
SELECT COUNT(*) INTO TAB_NAME1_CNT FROM TAB_NAME1 @dblink1;
SELECT COUNT(*) INTO TAB_NAME2_CNT FROM TAB_NAME2 @dblink1;

INSERT INTO TOT_TAB_CNT (SNO,TABLE1_CNT, TABLE2_CNT)
VALUES (1,TAB_NAME1_CNT,TAB_NAME2_CNT);
COMMIT;
END;

[Updated on: Wed, 29 January 2014 03:53]

Report message to a moderator

Re: Tablename as input parameter [message #606771 is a reply to message #606768] Wed, 29 January 2014 04:00 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You have to use dynamic SQL for this.
Read about EXECUTE IMMEDIATE in:
PL/SQL User's Guide and Reference
Application Developer's Guide - Fundamentals

Re: Tablename as input parameter [message #606868 is a reply to message #606771] Thu, 30 January 2014 05:06 Go to previous messageGo to next message
_jum
Messages: 577
Registered: February 2008
Senior Member
If you only want to COUNT the data, there are other (not necessarily better) ways to achieve this.
Here a code snippet to count the rows of a variable table with a SYS_REFCURSOR:
DECLARE
  tab_name1     VARCHAR2(30) := 'MY_LINKED_TABLE';
  tab_name1_cnt NUMBER := 0;
  cur_data1     SYS_REFCURSOR;

BEGIN 
 OPEN  cur_data1 FOR 'SELECT count(*) FROM '||tab_name1||'@dblink1';
 FETCH cur_data1 INTO tab_name1_cnt;
 dbms_output.put_line(tab_name1_cnt);
END;
Re: Tablename as input parameter [message #606980 is a reply to message #606868] Fri, 31 January 2014 10:39 Go to previous messageGo to next message
sarma.shp
Messages: 111
Registered: February 2012
Senior Member
Hi Michel Cadot, Thank you for your guidance. I have used execute immediate function successfully!

CREATE OR REPLACE PROCEDURE ABC AS (TAB_NAME1 NVARCHAR2, TAB_NAME2 NVARCHAR2)
AS 
TAB_NAME1_CNT NUMBER ;
TAB_NAME2_CNT NUMBER ;
ABC VARCHAR2(50);
DEF VARCHAR2(50);

BEGIN 
ABC : = 'SELECT COUNT(*)  FROM' || TAB_NAME1||  '@dblink1';

EXECUTE IMMEDIATE (ABC) INTO TAB_NAME1_CNT;
DEF:= 'SELECT COUNT(*) FROM' || TAB_NAME2 ||  '@dblink1';
EXECUTE IMMEDIATE (DEF) INTO TAB_NAME2_CNT ;

INSERT INTO TOT_TAB_CNT (SNO,TABLE1_CNT, TABLE2_CNT)
VALUES (1,TAB_NAME1_CNT,TAB_NAME2_CNT);
COMMIT;
END;




@JUM : Thank you very much for your code, i will try this one also!
Re: Tablename as input parameter [message #606984 is a reply to message #606980] Fri, 31 January 2014 11:00 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Thank you for the feedback and code.

Previous Topic: Remove special characters
Next Topic: Send file to client
Goto Forum:
  


Current Time: Fri Apr 26 01:37:12 CDT 2024