I am trying to execute a PL/SQL procedure without success.
It shows me "inexistent table". What can I do?
CREATE OR REPLACE PROCEDURE INTEGRATION(tableName varchar2) IS
BEGIN
MERGE INTO tableName alert
USING ...
you can try with execute immediate statement and concatenate the tablename passed from procedure
see example from oracle site
CREATE OR REPLACE PROCEDURE delete_rows (
table_name IN VARCHAR2,
condition IN VARCHAR2 DEFAULT NULL) AS
where_clause VARCHAR2(100) := ' WHERE ' || condition;
v_table VARCHAR2(30);
BEGIN
-- first make sure that the table actually exists; if not, raise an exception
SELECT OBJECT_NAME INTO v_table FROM USER_OBJECTS
WHERE OBJECT_NAME = UPPER(table_name) AND OBJECT_TYPE = 'TABLE';
IF condition IS NULL THEN where_clause := NULL; END IF;
EXECUTE IMMEDIATE 'DELETE FROM ' || v_table || where_clause;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE ('Invalid table: ' || table_name);
END;
/
BEGIN
delete_rows('employees_temp', 'employee_id = 111');
END;
/
for other info see oracle site
execute immediate
Related
ORA-00950: invalid DROP option
Above is the error that I am getting. I have written this procedure to remove all indexes from the emp_1 table. So can anyone please help me to find out the error and to fix this error?
CREATE OR REPLACE NONEDITIONABLE PROCEDURE sp_drop_indexes (
p_table_name IN VARCHAR2,
p_errormsg OUT VARCHAR2 )
IS
v_sql VARCHAR2(1000);
CURSOR get_indexes IS
SELECT index_name
FROM user_indexes
WHERE table_name = 'EMP_1';
BEGIN
FOR rec IN get_indexes LOOP
v_sql := 'DROP INDEX' || rec.index_name;
EXECUTE IMMEDIATE v_sql;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
p_errormsg := sqlerrm;
END sp_drop_indexes;
You are missing a space after the DROP INDEX (but still inside the quotes) in your code:
v_sql := 'DROP INDEX ' || rec.index_name;
Make sure you include a space after the DROP INDEX keyword:
v_sql := 'DROP INDEX ' || rec.index_name;
You need a space after drop index and, if you can have case-sensitive index names, you need double quotes:
CREATE OR REPLACE NONEDITIONABLE PROCEDURE sp_drop_indexes (
p_table_name IN VARCHAR2,
p_errormsg OUT VARCHAR2 )
IS
v_sql VARCHAR2(1000);
CURSOR get_indexes IS
SELECT index_name
FROM user_indexes
WHERE table_name = 'EMP_1';
BEGIN
FOR rec IN get_indexes LOOP
v_sql := 'DROP INDEX "' || rec.index_name || '"';
EXECUTE IMMEDIATE v_sql;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
p_errormsg := sqlerrm;
END sp_drop_indexes;
/
I am trying to build a package that will take in a table of table names and either drop from or delete those tables. I am using dynamic sql, and dropping or deleting the tables works, but I need both the procedures to loop through all of the table names passed back to it.
I've tried mulitple ways - including trying to create a FOR Loop and a cursor. Here is a similar function I wrote in PostgreSQL that works but I'm having trouble translating it to Oracle.
Here is my function in PostgreSQL that works:
CREATE OR REPLACE FUNCTION drop_tables_for_stnd_mod_build(tablenames text)
RETURNS void
LANGUAGE plpgsql
AS $function$
DECLARE
tab_name RECORD;
BEGIN
FOR tab_name IN EXECUTE 'SELECT table_name FROM ' || tablenames
LOOP
EXECUTE 'DROP TABLE ' || tab_name.table_name || ' CASCADE';
END LOOP;
END;
$function$
;
And the procedure I'm writing as part of a package in Oracle
CREATE OR REPLACE PACKAGE BODY stnd_build_table_cleanup
AS
PROCEDURE drop_tables(table_in CLOB)
IS
TYPE cur_type is REF CURSOR;
c cur_type;
query_string VARCHAR(300);
loop_string VARCHAR(300);
table_name VARCHAR(100);
BEGIN
loop_string := 'SELECT tablenames FROM :table';
OPEN c FOR loop_string USING table_in;
LOOP
FETCH c INTO table_name;
query_string := 'DROP TABLE ' || table_name || ' CASCADE CONSTRAINTS';
-- dbms_output.PUT_LINE (query_string);
EXECUTE IMMEDIATE query_string;
EXIT WHEN c%NOTFOUND;
END LOOP ;
CLOSE c;
END drop_tables;
Here is the error I get when I try to call my function: Error report -
ORA-00903: invalid table name
ORA-06512: at "AMS_NYS.STND_BUILD_TABLE_CLEANUP", line 13
ORA-06512: at line 2
00903. 00000 - "invalid table name"
*Cause:
*Action:
Thanks!
Here's one possibility. Note that I coded this as a standalone procedure for simplicity.
CREATE OR REPLACE TYPE table_type IS TABLE OF VARCHAR2(128);
CREATE OR REPLACE PROCEDURE drop_tables(tables_to_drop_in table_type)
IS
BEGIN
FOR i IN tables_to_drop_in.FIRST .. tables_to_drop_in.LAST LOOP
--DBMS_OUTPUT.PUT_LINE(tables_to_drop_in(i));
EXECUTE IMMEDIATE 'DROP TABLE ' || tables_to_drop_in(i) || ' CASCADE CONSTRAINTS';
END LOOP;
END drop_tables;
DECLARE
tables_to_drop table_type;
BEGIN
tables_to_drop := table_type('TBL1','TBL2', 'TBL3');
drop_tables(tables_to_drop);
END;
I have created a procedure that will basically take in 3 parameters to drop & re-create tables and indexes. It will start by checking if there is a table existing, if so then it will drop it and proceed. If not, then it will automatically proceed to create the table and index. Below is the my procedure:
CREATE OR REPLACE
PROCEDURE PROC_RFRSH (TBL_NME IN VARCHAR2, TABLE_QRY IN CLOB, CRT_INDX IN CLOB)
IS
-- VARIABLE TO BE USED TO CHECK IF TABLE/INDEX EXISTS
COUNT_TABLE NUMBER;
DRP_TBL VARCHAR2(4000);
BEGIN
-- CHECK IF TABLE EXISTS
SELECT COUNT(*)
INTO COUNT_TABLE
FROM USER_TABLES
WHERE TABLE_NAME = TBL_NME; -- AGAINST THE PASSED IN TABLE NAME
-- IF TABLE EXISTS,
IF COUNT_TABLE > 0 THEN
DBMS_OUTPUT.PUT_LINE('THE FOLLOWING TABLE: ' || TBL_NME || ' EXISTS!');
-- DROP TABLE CASCADE CONSTRAINTS
DRP_TBL := 'DROP TABLE ' || TBL_NME || ' CASCADE CONSTRAINTS';
EXECUTE IMMEDIATE DRP_TBL;
DBMS_OUTPUT.PUT_LINE('TABLE HAS BEEN DROPPED!');
END IF;
-- CREATE TABLE
EXECUTE IMMEDIATE TABLE_QRY;
DBMS_OUTPUT.PUT_LINE(TBL_NME || ' HAS BEEN CREATED');
-- CREATE INDEX
EXECUTE IMMEDIATE CRT_INDX;
DBMS_OUTPUT.PUT_LINE('INDEX HAS BEEN CREATED');
EXCEPTION
WHEN OTHERS THEN
NULL;
END; -- PROCEDURE ENDS HERE
/
And here is the values I am sending to the procedure:
DECLARE
TBL_NME VARCHAR2(30) := 'CUS_TBL';
TABLE_QRY CLOB := 'CREATE TABLE CUS_TBL
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);';
INDX_NME VARCHAR2(30) := 'CUS_INDX';
COL_NME VARCHAR2(55) := 'customer_id ';
CRT_INDX CLOB := 'CREATE INDEX ' || INDX_NME || ' ON CUS_TBL(' || COL_NME || ')';
BEGIN
PROC_RFRSH(TBL_NME, TABLE_QRY, CRT_INDX);
END;
/
My problem is that the table I am trying to create is not actually being created by this procedure and I don't understand why. Can you guys help please?
Thanks in advance!
I need help in creating this anonymous block into a procedure.
I am new to PLSQL. Any help will be much appreciated. Thank you in advance.
I would like this query to run just by calling a procedure.
TRUNCATE TABLE dblink_status_tbl;
set serveroutput on;
-- or equivalent for your client
declare
-- l_dummy dual.dummy%type;
l_dummy VARCHAR2(20);
l_status VARCHAR2(100);
begin
for r in
(select db_link from all_db_links where db_link in
( 'WP1',
'6P1',
'OP3',
'LP1',
'ODS')
and owner = 'CAMPER')
loop
begin
execute immediate 'select 1 from dual#' || r.db_link into l_dummy;
l_status:= 'ACTIVE';
dbms_output.put_line(r.db_link|| ',' || l_status);
rollback work;
execute immediate 'alter session close database link ' || r.db_link;
exception
when others then
l_status:= sqlerrm;
l_status := replace(replace(l_status,CHR(13), ' '),CHR(10),' ');
l_status := '"' || l_status || '"';
dbms_output.put_line(r.db_link|| ',' || l_status);
end;
insert into dblink_status_tbl
values(r.db_link,l_status);
commit;
end loop;
end;
Basically, you need only the first line in my example:
create or replace procedure p_your_proc as
-- from now on, it is your original code
l_dummy VARCHAR2(20);
l_status VARCHAR2(100);
begin
...
end;
/
Once it is created, run it as
begin
p_your_proc;
end;
/
P.S.
At the beginning, you're truncating a table - if it is necessary within the procedure, you'd use dynamic SQL (as it is a DDL):
begin
execute immediate ('TRUNCATE TABLE dblink_status_tbl');
...
end;
Or, simply delete its contents as
begin
delete from dblink_status_tbl;
...
end;
Make it like something:
create or replace procedure proc_name as
l_dummy VARCHAR2(20);
l_status VARCHAR2(100);
begin
...
Using l_dummy,l_status
end;
And run this like :
"Exec proc_name" or "execute proc_name"
I am trying to pass table name and column name to a stored procedure in oracle , but it gives me following error: table or view does not exist
Below is the code:
create or replace procedure jz_dynamic_sql_statement
(p_table_name in varchar2,
p_col1_name in varchar2,
p_check_result out integer)
as
v_error_cd est_runtime_error_log.error_cd%type;
v_error_msg est_runtime_error_log.error_msg%type;
v_sql varchar2(1024);
v_result number(10);
begin
v_result := 0;
v_sql := 'select count(*) from ' || p_table_name ||' WHERE COLUMNNAME=' || p_col1_name;
execute immediate v_sql into v_result;
p_check_result := v_result;
end;
If the error coming back says the table does not exist then that means the table you pass in does not exist or the user that the procedure runs under cannot access it.
You could add a dbms_output.put_line statement to display the query that you are building and then try running it yourself, before you attempt the execute immediate. Then you know what errors you need to fix.
dbms_output.put_line('query : '||v_sql);
Be sure to turn on dbms_output.
Also, from what it looks like you are trying to do, you will need to pass the column name AND column value. Unless the tables you are querying will ALWAYS have the column name "COLUMNNAME".
Try this:
v_sql := 'select count(*) from ' || p_table_name ||' WHERE COLUMNNAME=''' || p_col1_name|| '''';