Transfer data from table into XML object table - sql

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;

Related

inserting data into a temporary table in a loop postgresql

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';

Check if first column is populated but all other columns are null Teradata

Please what is the effective way for getting the rows where the first column (primary index) is populated but all other columns are null? The table has 25 columns and I want to avoid putting all column names in the WHERE clause.
Thanks.
One of many methods (this will give you a generated SQL to run, but you can amend the code just to get its results through insert select, for instance)
create table test_table_2000 ( charcol1 varchar(2000) );
replace procedure sp_find_blank_rows( in_database varchar(50), in_tablename varchar(50) )
begin
declare
l_sql varchar(2000);
declare
l_int integer;
set l_sql = 'select * from '||in_database||'.'||in_tablename||' where 1=1 ';
FOR fReq AS cReq CURSOR FOR
select * from Dbc.Columns where databaseName=in_database and TableName=in_tablename order by ColumnId
DO
IF l_int is null THEN
SET l_sql = l_sql || ' and ' || fReq.ColumnName || ' is not null ';
ELSE
SET l_sql = l_sql || ' and ' || fReq.ColumnName || ' is null ';
END IF;
set l_int = 1;
END FOR;
insert into test_table_2000 values ( l_sql );
end;
call sp_find_blank_rows('<your_database>','<your_table>');

postgres execute sql in resulting rows

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

How can I use `USING` clause with `LIKE` operator in PL/SQL?

I have a PL/SQL in which I want to perform a search.
The PL/SQL is
DECLARE
match_count INTEGER;
BEGIN
FOR t IN (SELECT owner, table_name, column_name
FROM all_tab_columns
WHERE table_name='GETS_TS_EGU_LOOKUP' and data_type LIKE '%CHAR%') LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE '||t.column_name||' = :1'
INTO match_count
USING 'NONE';
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
/
This PL/SQL can perform look up on the keyword NONE. I want to perform a LIKE search on NONE like %NONE%.
How can I do it?
Is it even possible?
That should be
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE ' || t.column_name || ' like ''%'' || :1 || ''%'''
INTO match_count
USING 'NONE';
But with 'NONE' being a constant, why not simply:
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE ' || t.column_name || ' like ''%NONE%'''
INTO match_count;
You can declare a new variable to assign the string you want to search and then you cane use it in the query as shown below
DECLARE
match_count INTEGER;
srch_str varchar2(20);
BEGIN
srch_str:= 'NONE'
FOR t IN (SELECT owner, table_name, column_name
FROM all_tab_columns
WHERE table_name='GETS_TS_EGU_LOOKUP' and data_type LIKE '%CHAR%') LOOP
EXECUTE IMMEDIATE
'SELECT COUNT(*) FROM ' || t.owner || '.' || t.table_name ||
' WHERE '||t.column_name||' like ''%'||srch_str||'%'''
INTO match_count;
IF match_count > 0 THEN
dbms_output.put_line( t.table_name ||' '||t.column_name||' '||match_count );
END IF;
END LOOP;
END;
It will work perfectly fine.

Cannot run 'select into' from dynamic-named temporary table within plpgsql function

I am dynamcially naming temporary table, inserting some data to this dynamic-named temporary table.
But I am not able to get back the data from dynamic-named temporary table to function variable to do my calculation.
How can I execute 'select into ....' inside plpgsql function from/with dynamic-named temporary table?
drop table dummy;
drop table mytable;
create table dummy (id bigint,parent_id bigint);
insert into dummy values(600,null);
insert into dummy values(12,600);
insert into dummy values(700,null);
DROP FUNCTION total_test(bigint,text,text,date,bigint[],text);
CREATE OR REPLACE FUNCTION total_test(bigint,text,text,date,dep bigint[],tname text) RETURNS double precision AS '
DECLARE pid BIGINT;
DECLARE total DOUBLE PRECISION;
DECLARE array_len int;
DECLARE myDepIds bigint[];
BEGIN
IF dep IS NOT NULL THEN
total :=0;
array_len := array_upper(DEP, 1);
EXECUTE ''CREATE TEMPORARY TABLE '' || tname || '' (dep_id bigint )'';
FOR i IN 1 .. array_len
LOOP
EXECUTE ''INSERT INTO '' || tname || '' values (''|| DEP[i] ||'')'';
select into pid id from dummy where parent_id in (DEP[i]);
IF pid IS NOT NULL THEN
EXECUTE ''INSERT INTO '' || tname || '' values (''|| pid || '')'';
END IF;
END LOOP;
--works where tname:=''mytable''; select into myDepIds array(select distinct(dep_id) from mytable where dep_id is not null);
--not working; EXECUTE ''select into myDepIds array(select distinct(dep_id) from '' || $7 || '' where dep_id is not null)'';
--not working; EXECUTE ''select into myDepIds array(select distinct(dep_id) from '' || tname || '' where dep_id is not null)'';
EXECUTE ''select into '' || myDepIds || '' array(select distinct(dep_id) from '' || tname || '' where dep_id is not null)'';
--not working; EXECUTE ''select into cast(myDepIds as bigint[]) array(select distinct(dep_id) from '' || tname || '' where dep_id is not null)'';
--calculation....
--EXECUTE ''DROP TABLE '' || tname;
RETURN total;
END IF;
END;
' LANGUAGE plpgsql;
select total_test(11269, 'sales', 'A', date('06/02/2011'), ARRAY[600], 'mytable') as value;
select * from mytable;
Should be:
EXECUTE sql_string INTO var