Copy tables form schema in another schema in Oracle - sql

I want to write a procedure or a cursor.
Input data -- NAME OWNER.
We know name (OWNER) AND from table ALL_OBJECTS
Take the name of the table.
Tables > 30.
How to write correctly ?
CREATE OR REPLACE PROCEDURE USER_NAME
(
v_USER VARCHAR2
)
AS
v_sysdate VARCHAR2(10) := to_char(SYSDATE ,'MMDDYYYY');
v_table_name VARCHAR2(50);
BEGIN
SELECT
TABLE_NAME INTO v_table_name
FROM
ALL_OBJECTS F -- Table with two columnsю. OWNER AND NAME TABLES
WHERE
F.OWNER = v_USER;
--Name of tables and owner ALL_OBJECTS
EXECUTE IMMEDIATE 'CREATE TABLE USER_BANCU.'||v_USER||'_'||v_table_name||'__'||v_sysdate||to_char(sysdate,'HH24_MI_SS')||' AS
SELECT *
FROM '||v_USER||'.'||v_table_nam;
COMMIT;
END;
/

Try DBMS_METADATA_GET_DDL.
enter link description here

Related

oracle - concatenate in select for drop, create, grant - lost in ''''

I am new to Oracle database, got a task to write several selects, but I do not get somethow principle, how it works (or better to say does not work). Could you please help me to find a mistake and probably you have a link to some info or converter that helps such writings.
v_sql := 'SELECT ''DROP TABLE ''||object_name||'' as
select *
FROM all_objects
WHERE object_name LIKE '''%''|| v_date ||''%'''
and object_type = ''TABLE''
and owner =''||v_owner||''';
Are you trying to generate DROP statements for a specific user? Start with the below anonymous block and add to it.
--Generate DROP statements for all tables for a user.
declare
v_owner varchar2(128) := user;
begin
for objects in
(
select distinct 'DROP TABLE "'||owner||'"."'||object_name||'"' v_sql
from all_objects
where object_type = 'TABLE'
and owner = v_owner
order by v_sql
) loop
dbms_output.put_line(objects.v_sql);
end loop;
end;
/

How to find to which schema or procedure, table and column a particular value belongs to?

I have a 9 digit number, say "234234234", is there a way to find or check its appearance in my database, like in which particular schema or procedure does it fall? and list out all the tables and columns which has that value in pl/sql developer
This query only searches in stored objects that user is allowed to access (procedure, function, package, ...). You could refer to this
Not sure if there is one way to search for that value in all database table.
SELECT *
FROM all_source
WHERE text LIKE '%234234234%';
--AND owner = 'SCHEMA_NAME';
The below block identifies the given string's presence across all the tables in your DB.
declare
num_rows number;
sql_text varchar2(250);
sql_info varchar2(100);
begin
dbms_output.enable(1000000);
for x in (select table_name, column_name from dba_tab_columns
where data_type in ('VARCHAR','VARCHAR2','CHAR')
and owner <> 'SYSTEM')
loop
sql_text:='select count(*) into :num_rows from SYSTEM.'||x.table_name||' where '||x.column_name||' like ''%234234234%''';
-- dbms_output.put_line (sql_text);
execute immediate sql_text into num_rows;
if num_rows>0
then
sql_info:='Table: '||x.table_name||' contains the string';
dbms_output.put_line (sql_info);
end if;
end loop;
end;
/

ORACLE SQL: Looping over table

I have a table that contains a list of table names.
I would like to search each of these tables one by one to see if they contain a particular element (the primary key, specified at the start of the script).
I would like to return a list of all of the tables that this element is present in (ideally distinct).
I'm fairly new to this PL/SQL "not just a query" stuff. so i apologise in advance for the attrocious attempt you are about to see, but hopefully it illustrates what i'm going for:
PROCEDURE CHECK_FOR_ELEMENTS
BEGIN
DECLARE
ELEMENT_KEY varchar(5):=X78ehryfk;
RNUM_MAX int :=167;
----create output table for script
create or replace table ALL_TABLES CONTAINING_&ELEMENT_KEY
(ELEMENT_KEY VARCHAR(255),
TABLE_NAME varchar(255))
/
commit;
---begin loop over rnum;
FOR rnum_counter in 1..&RNUM_MAX
LOOP
--define this statement as variable TABLE_NAME_VAR
select table_name from (select * from (select table_name, rownum as rnum
from all_tables
where owner = 'RMS'
and table_name like 'ABC%'
and table_name not like '%STG'
and table_name not like '%BKP'
and num_rows>0
order by num_rows desc)
where rnum = rnum_counter
)INTO TABLE_NAME_VAR
;
----run below to collect row, if it exists, from table being searched
SQL_STMT:='INSERT INTO ALL_TABLES CONTAINING_&ELEMENT_KEY
SELECT distinct key,'||TABLE_NAME_VAR||' as UMF from
'||TABLE_NAME_VAR||
' where key like 'ELEMENT_KEY-%'
execute immediate SQL_STMT;
commit;
---insert row into table created for output
END LOOP
---loop over all tables
END;
The main error message i get is that TABLE_NAME_VAR is not a valid table name within the dynamic SQL statement. I've googled a bit and i now understand you can't use variables to input table names in this way.
Any help is greatly appreciated!
Thankyou!
Here, I tried to clean it up for you. Let me know if you still get errors.
create or replace PROCEDURE CHECK_FOR_ELEMENTS is
ELEMENT_KEY varchar2(14):='X78ehryfk';
RNUM_MAX int :=167;
TABLE_NAME_VAR varchar2(30);
SQL_STMT varchar2(4000);
BEGIN
----create output table for script
begin
execute immediate 'drop table ALL_TABLES_WITH_' || element_key;
exception when others then null;
end;
execute immediate 'create table ALL_TABLES_WITH_' || element_key || '
(ELEMENT_KEY VARCHAR2(255), -- does this need to be 255 characters?
TABLE_NAME varchar2(30))';
--- implicit cursor loop
FOR rnum_row in (select table_name, rownum as rnum
from all_tables
where owner = 'RMS'
and table_name like 'ABC%'
and table_name not like '%STG'
and table_name not like '%BKP'
and num_rows>0
order by num_rows desc)
LOOP
if rnum_row.rnum > RNUM_MAX
then exit;
end if;
TABLE_NAME_VAR := rnum_row.table_name;
----run below to collect row, if it exists, from table being searched
SQL_STMT:='INSERT INTO ALL_TABLES_WITH_' || element_key || '
(ELEMENT_KEY, TABLE_NAME)
SELECT distinct key, :1 as UMF from
'||TABLE_NAME_VAR||
' where key like :2';
execute immediate SQL_STMT using TABLE_NAME_VAR, element_key || '-%';
---insert row into table created for output
END LOOP;
commit;
---loop over all tables
END CHECK_FOR_ELEMENTS;
/

Get Tables Name Through Plsql Query

I need a procedure that input user_name and password then get to me the exist tables in that schema with the create script for each table?
This is what I tried, but is wrong:
CREATE OR REPLACE procedure TABLE_INFO(P_USER_NAME IN VARCHAR2,P_PASSWORD IN VARCHAR,P_TABLE_NAME OUT VARCHAR2,P_SCRIPT OUT VARCHAR2)
IS
chk_username all_users.username%type;
CURSOR C IS SELECT table_name FROM USER_TABLES;
t_tablename user_tables.table_name%type;
BEGIN
SELECT username into chk_username from all_users
where chk_username=p_user_name;
open c;
loop
fetch c into t_tablename;
exit when c% notfound;
end loop;
exception when no_data_found then
dbms_output.put_line('Wrong Username Or Password');
close c;
end;
/
Try this code. Let me know if this helps.
CREATE OR REPLACE PROCEDURE TABLE_INFO(
P_USER_NAME IN VARCHAR2,
-- P_PASSWORD IN VARCHAR, --Not required
P_TABLE_NAME OUT sys_refcursor)
-- P_SCRIPT OUT VARCHAR2) --Not required
AS
BEGIN
-----------------------For DDL script use dbms_metadata.get_ddl-------------------------
OPEN P_TABLE_NAME FOR
SELECT OWNER,TABLE_NAME,dbms_metadata.get_ddl('TABLE',TABLE_NAME) "table DDL"
FROM all_tables WHERE upper(OWNER) = upper(P_USER_NAME);
END;
--------------------To EXECUTE this proc---------------------------------
var usr VARCHAR2(100);
VAR p_lst refcursor;
exec TABLE_INFO(<username>,:p_lst);
------------------------------------------------------------------------
Check this out. This is a function returning all the values as per requirement.
-- Obj Creation
CREATE OR REPLACE TYPE my_obj
IS
OBJECT
(
TAB_NAME VARCHAR2(100),
DDL_S VARCHAR2(32676) );
-- Table type Creation
CREATE OR REPLACE TYPE my_tab
IS
TABLE OF my_obj;
--Function Creation---
CREATE OR REPLACE FUNCTION TABLE_INFO_FUN(
P_USER_NAME IN VARCHAR2 )
RETURN my_tab
AS
my_otpt my_tab;
BEGIN
-----------------------For DDL script use dbms_metadata.get_ddl-------------------------
SELECT CAST( multiset
(SELECT my_obj(TABLE_NAME,dbms_metadata.get_ddl('TABLE',TABLE_NAME))
FROM all_tables
WHERE upper(OWNER) = upper(P_USER_NAME)
) AS my_tab )
INTO my_otpt
FROM DUAL;
RETURN my_otpt;
END;
-- Using Function to Select Data -------------
SELECT * FROM TABLE(TABLE_INFO_FUN(<USERNAME>));

How to find out which procedures and functions are using a table?

I'm deleting a table (table a), and I want to know which of the functions, procedures and views are using my table (table a).
try this
select * from dba_source where text like '%:tablename%'
You can check in DBA_DEPENDENCIES table using below query:
select * from DBA_DEPENDENCIES where REFERENCED_NAME ='tableA' --YOUR TABLE NAME;
QUICK CHECK:
create table TEST (id number(5), name varchar2(50) );
--Table created
insert into TEST values(1,'mahi');
--1 row created.
commit;
--Commit complete.
create or replace procedure PROC_TEST As
v_name varchar2(50);
BEGIN
select name into v_name from TEST where id=1;
dbms_output.put_line('o/p : ' || ' ' || v_name);
END;
/
--Procedure created.
Exec PROC_TEST();
--o/p : mahi
--PL/SQL procedure successfully completed.
QUERY:
select * from DBA_DEPENDENCIES where REFERENCED_NAME = 'TEST';
i thing bellow query by "davegreen100" will work but need to place upper at both side so that it will work for upper as well as lower case.
select * from dba_source where upper(text) like upper('%:tablename%')