Tablename as input parameter [message #606768] |
Wed, 29 January 2014 03:49 |
|
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 #606868 is a reply to message #606771] |
Thu, 30 January 2014 05:06 |
_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;
|
|
|
|
|