I have a query that works without errors, but if I try to insert data into a temporary table, then it is empty.
What am I doing wrong?
DO $$DECLARE r record;
begin
FOR r IN select concat('select * from ',schema_name,'.etl_load_log') as sql_str, schema_name
from information_schema.schemata s
where schema_name like '%db_tmd'
and rtrim(ltrim( schema_name,'disd_'),'_db_tmd') <> ''
and rtrim(ltrim( schema_name,'disd_'),'_db_tmd') like '%69%'
LOOP
EXECUTE 'SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
END LOOP;
END$$;
select * from result_table
--EXECUTE 'insert into result_table (package_name, info_system, inst_cd text SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
replace string:
EXECUTE 'insert into result_table (package_name, info_system, inst_cd text SELECT * FROM ' || quote_ident(r.schema_name) || '.' || 'etl_load_log';
Related
postgesql 9.6.17
There are result rows from Renaming multiple columns in PostgreSQL
they contain some commands like
alter table .....
alter table .....
how to immediately exec them in sql like
SELECT
EXEC SQL 'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name)) || ';' commit
FROM (
SELECT
quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub;
but example ↑ fails
You can use the DO statement for executing this. Something like given below:
DO $$
DECLARE rec TEXT;
BEGIN
FOR r in SELECT
'ALTER TABLE ' || tab_name || ' RENAME COLUMN '
|| quote_ident(column_name) || ' TO '
|| lower(quote_ident( column_name))
FROM (
SELECT
quote_ident(table_schema) || '.' ||
quote_ident(table_name) as tab_name,
column_name
FROM information_schema.columns
WHERE
table_schema = 'public'
) sub
LOOP
EXECUTE rec;
END LOOP;
END $$;
For more detail refer the following link:
https://www.postgresql.org/docs/9.6/sql-do.html
select *
from (
select table_name
from user_tab_cols
where column_name='CONTRACT_NUMBER'
)
where contarct_number ='U5490231'
in this select statement, I am trying to get the list of tables from user_tab_cols
and then do a
select *
from table_name
where column_name='U5490231'.
trying to find a select statement on this basis but getting error
Oracle Query:
v_table NVARCHAR(MAX)
v_whereCol NVARCHAR(MAX)
v_WhereParam NVARCHAR(MAX)
v_WhereParam := 'CONTRACT_NUMBER'
v_whereCol := 'U5490231'
SELECT E.table_name INTO v_table
FROM user_tab_cols AS E
WHERE E.column_name = v_WhereParam
v_query NVARCHAR(MAX)
v_query := 'Select * from ' || v_table || ' Where contarct_number =''' || v_whereCol || ''''
EXECUTE IMMEDIATE v_query
This was what i was trying
SET SERVEROUTPUT ON;
DECLARE
sql_query VARCHAR2(32767);
BEGIN
FOR t IN (SELECT table_name, column_name FROM user_tab_columns)
LOOP
EXECUTE IMMEDIATE sql_query := 'SELECT * FROM ' || t.table_name ;
END LOOP;
END;
This query gives you the number of distinct values per column (assuming that statistics are up to date).
select owner, table_name, column_name, num_distinct
from all_tab_col_statistics
Maybe this would be enough.
If you need to have the distinct values, you have to modify the sql_query param in your script as follows:
EXECUTE IMMEDIATE sql_query := 'SELECT distinct '|| t.column_name ||
' FROM ' || t.table_name ;
If I have a table:
create table thisTable (
column1 varchar(20),
column2 varchar(20),
column3 varchar(20)
);
and I want to transfer data to a 1 column XML table:
create table XMLTable (
data1 sys.xmltype
);
<column2>
<column1>..</column1>
<column2>..</column2>
<column3>..</column3>
</column2>
How would I do that?
INSERT INTO XMLTABLE
SELECT
XMLELEMENT(
"column2",
XMLELEMENT("column1", COLUMN1), XMLELEMENT("column2", COLUMN2), XMLELEMENT("column3", COLUMN3)
)
FROM
thisTable;
You can insert it using below procedure.
declare
sourceTable varchar2(80) := 'THISTABLE';
destTable varchar2(80) := 'XMLTABLE';
destColumn varchar2(80) := 'data1';
TYPE cur_typ IS REF CURSOR;
c cur_typ;
colu varchar2(2000);
vsql varchar2(2000) := ' select ';
begin
for r in (select column_name from user_tab_columns where table_name = sourceTable order by column_id)
loop
vsql := vsql || ''' <' || r.column_name || '>'' || ' || r.column_name || ' || ''</' || r.column_name || '> '' || ' ;
end loop;
vsql := substr(vsql, 0 ,length(vsql)-4);
vsql := vsql ||' as x From ' || sourceTable;
open c for vsql ;
loop
FETCH c INTo colu;
EXIT WHEN c%NOTFOUND;
dbms_output.put_line(colu);
execute immediate ' insert into ' || destTable || ' values (xmltype(''<column2>' || colu || '</column2>'')) ';
end loop;
close c;
end;
I had created a lot of tables and users while testing some SQL command and now I dont remember exactly all the tables and user's name.
But now I want to delete all of it for my big project.So is it possible to delete it in SQL command line ?
This PL/SQL block will be useful to delete all the data in oracle data base
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE',
'SYNONYM',
'PACKAGE BODY'
))
LOOP
BEGIN
IF cur_rec.object_type = 'TABLE'
THEN
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '" CASCADE CONSTRAINTS';
ELSE
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'FAILED: DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"'
);
END;
END LOOP;
END;
/
execute this command:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'truncate table ' || table_.owner || '.' || table_.table_name ||' cascade';
END LOOP;
END;
/
BEGIN
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' DISABLE ALL CONSTRAINTS';
--This will disable all the constraint
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'TRUNCATE TABLE '||T.table_name;
END LOOP;
FOR T in (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||T.table_name||' ENABLE ALL CONSTRAINTS';
END LOOP;
END;
Alter table ... disable all constraints throws a ORA-01735: invalid ALTER TABLE option if there is no constraint defined for the table, which would cause the script to fail to truncate every table if there is at least one table without constraint. You might want to place the execute immediate within a begin -exception block
You have two ways:
you can use function delete instead truncate for this function you should not disable constraints, but it work more slowly because you can rollback this operation if you will go this way:
BEGIN
FOR table_ IN (SELECT * FROM dba_tables where owner like 'YOUR_SCHEMA') LOOP
execute immediate 'delete from table ' || table_.owner || '.' || table_.table_name;
END LOOP;
END;
use truncate it is more faster, but you must diable constraints on table:
begin
for disable_constraint_ in
(select * from dba_constraints where owner= 'YOUR_SCHEMA'
)
loop
execute immediate 'alter table ' || disable_constraint_.owner || '.' || disable_constraint_.table_name ||' disable constraint '|| disable_constraint_.constraint_name;
end loop;
for table_ in (select * from dba_tables where owner = 'YOUR_SCHEMA')
loop
execute immediate 'truncate table ' || table_.owner || '.' ||table_.table_name ||' cascade';
end loop;
for enable_constaint_ in (select * from dba_constraints where owner= 'YOUR_SCHEMA')
loop
execute immediate 'alter table ' || enable_constaint_.owner || '.' || enable_constaint_.table_name ||' enable constraint '|| enable_constaint_.constraint_name;
end loop;
end;