I have two tables. They differ only columns order.
First
Table1
(
name,
surname,
age
)
Second
Table2
(
age
surname,
name
)
I want insert data to Table2 from Table1.
If tables column order is the same I can use
insert into Table2
select * from Table1
I know that I can solve this problem with
insert into table2
select age,surname,name from table1
But I don't use it because there are many column in my real table.
is there good idea for it?
The only way is with some dynamic SQL, by relying on column names; for example, say you have the tables
CREATE TABLE Table1
(
name VARCHAR2(100),
surname VARCHAR2(100),
age NUMBER
);
CREATE TABLE Table2
(
name VARCHAR2(100),
age NUMBER,
oneMoreColumn NUMBER,
surname VARCHAR2(100)
);
you can do:
declare
vSQL varchar2(1000);
vCols varchar2(1000);
begin
select listagg(tc1.column_name, ', ') within group (order by tc1.column_name)
into vCols
from user_tab_columns tc1
inner join user_tab_columns tc2
on(tc1.column_name = tc2.column_name)
where tc1.table_name = 'TABLE1'
and tc2.table_name = 'TABLE2';
--
vSQL := 'insert into table2( ' || vCols || ') select ' || vCols || ' from table1';
--
dbms_output.put_line(vSQL);
--
execute immediate vSQL;
end;
this will build and execute the statement:
insert into table2( AGE, NAME, SURNAME) select AGE, NAME, SURNAME from table1
you can do like this
create table EX_EMPLOYEE
(
NAME VARCHAR2(100),
PATH VARCHAR2(1000)
)
SET serveroutput ON size 2000
/
declare T_COL varchar2(50);
CURSOR c1 IS SELECT column_name name FROM user_tab_cols where table_name='EX_EMPLOYEE';
BEGIN
FOR rec IN c1 LOOP
if T_COL is null then
T_COL := T_COL || rec.name;
else
T_COL := T_COL ||' ,' || rec.name;
end if;
END LOOP;
dbms_output.put_line('select '|| T_COL ||' FROM EX_EMPLOYEE');
END;
/
select NAME ,PATH FROM EX_EMPLOYEE
PL/SQL procedure successfully completed
you could do something like this:
create table new_table as
select * from old_table
Related
I have table like this:
Table-1
Table-2
Table-3
Table-4
Table-5
each table is having many columns and one of the column name is employee_id.
Now, I want to write a query which will
1) return all the tables which is having this columns and
2) results should show the tables if the column is having values or empty values by passing employee_id.
e.g. show table name, column name from Table-1, Table-2,Table-3,... where employee_id='1234'.
If one of the table doesn't have this column, then it is not required to show.
I have verified with link, but it shows only table name and column name and not by passing some column values to it.
Also verified this, but here verifies from entire schema which I dont want to do it.
UPDATE:
Found a solution, but by using xmlsequence which is deprecated,
1)how do I make this code as xmltable?
2) If there are no values in the table, then output should have empty/null. or default as "YES" value
WITH char_cols AS
(SELECT /*+materialize */ table_name, column_name
FROM cols
WHERE data_type IN ('CHAR', 'VARCHAR2') and table_name in ('Table-1','Table-2','Table-3','Table-4','Table-5'))
SELECT DISTINCT SUBSTR (:val, 1, 11) "Employee_ID",
SUBSTR (table_name, 1, 14) "Table",
SUBSTR (column_name, 1, 14) "Column"
FROM char_cols,
TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select "'
|| column_name
|| '" from "'
|| table_name
|| '" where upper("'
|| column_name
|| '") like upper(''%'
|| :val
|| '%'')' ).extract ('ROWSET/ROW/*') ) ) t ORDER BY "Table"
/
This query can be done in one step using the (non-deprecated) XMLTABLE.
Sample Schema
--Table-1 and Table-2 match the criteria.
--Table-3 has the right column but not the right value.
--Table-4 does not have the right column.
create table "Table-1" as select '1234' employee_id from dual;
create table "Table-2" as select '1234' employee_id from dual;
create table "Table-3" as select '4321' employee_id from dual;
create table "Table-4" as select 1 id from dual;
Query
--All tables with the column EMPLOYEE_ID, and the number of rows where EMPLOYEE_ID = '1234'.
select table_name, total
from
(
--Get XML results of dynamic query on relevant tables and columns.
select
dbms_xmlgen.getXMLType(
(
--Create a SELECT statement on each table, UNION ALL'ed together.
select listagg(
'select '''||table_name||''' table_name, count(*) total
from "'||table_name||'" where employee_id = ''1234'''
,' union all'||chr(10)) within group (order by table_name) v_sql
from user_tab_columns
where column_name = 'EMPLOYEE_ID'
)
) xml
from dual
) x
cross join
--Convert the XML data to relational.
xmltable('/ROWSET/ROW'
passing x.xml
columns
table_name varchar2(128) path 'TABLE_NAME',
total number path 'TOTAL'
);
Results
TABLE_NAME TOTAL
---------- -----
Table-1 1
Table-2 1
Table-3 0
Just try to use code below.
Pay your attention that may be nessecery clarify scheme name in loop.
This code works for my local db.
set serveroutput on;
DECLARE
ex_query VARCHAR(300);
num NUMBER;
emp_id number;
BEGIN
emp_id := <put your value>;
FOR rec IN
(SELECT table_name
FROM all_tab_columns
WHERE column_name LIKE upper('employee_id')
)
LOOP
num :=0;
ex_query := 'select count(*) from ' || rec.table_name || ' where employee_id = ' || emp_id;
EXECUTE IMMEDIATE ex_query into num;
if (num>0) then
DBMS_OUTPUT.PUT_LINE(rec.table_name);
end if;
END LOOP;
END;
I tried with the xml thing, but I get an error I cannot solve. Something about a zero size result. How difficult is it to solve this instead of raising exception?! Ask Oracle.
Anyway.
What you can do is use the COLS table to know what table has the employee_id column.
1) what table from table TABLE_LIKE_THIS (I assume column with table names is C) has this column?
select *
from COLS, TABLE_LIKE_THIS t
where cols.table_name = t
and cols.column_name = 'EMPLOYEE_ID'
-- think Oracle metadata/ think upper case
2) Which one has the value you are looking for: write a little chunk of Dynamic PL/SQL with EXECUTE IMMEDIATE to count the tables matching above condition
declare
v_id varchar2(10) := 'JP1829'; -- value you are looking for
v_col varchar2(20) := 'EMPLOYEE_ID'; -- column
n_c number := 0;
begin
for x in (
select table_name
from all_tab_columns cols
, TABLE_LIKE_THIS t
where cols.table_name = t.c
and cols.column_name = v_col
) loop
EXECUTE IMMEDIATE
'select count(1) from '||x.table_name
||' where Nvl('||v_col||', ''##'') = ''' ||v_id||'''' -- adding quotes around string is a little specific
INTO n_c;
if n_c > 0 then
dbms_output.put_line(n_C|| ' in ' ||x.table_name||' has '||v_col||'='||v_id);
end if;
-- idem for null values
-- ... ||' where '||v_col||' is null '
-- or
-- ... ||' where Nvl('||v_col||', ''##'') = ''##'' '
end loop;
dbms_output.put_line('done.');
end;
/
Hope this helps
I need to count the number of null values of all the columns in a table in Oracle.
For instance, I execute the following statements to create a table TEST and insert data.
CREATE TABLE TEST
( A VARCHAR2(20 BYTE),
B VARCHAR2(20 BYTE),
C VARCHAR2(20 BYTE)
);
Insert into TEST (A) values ('a');
Insert into TEST (B) values ('b');
Insert into TEST (C) values ('c');
Now, I write the following code to compute the number of null values in the table TEST:
declare
cnt number :=0;
temp number :=0;
begin
for r in ( select column_name, data_type
from user_tab_columns
where table_name = upper('test')
order by column_id )
loop
if r.data_type <> 'NOT NULL' then
select count(*) into temp FROM TEST where r.column_name IS NULL;
cnt := cnt + temp;
END IF;
end loop;
dbms_output.put_line('Total: '||cnt);
end;
/
It returns 0, when the expected value is 6.
Where is the error?
Thanks in advance.
Counting NULLs for each column
In order to count NULL values for all columns of a table T you could run
SELECT COUNT(*) - COUNT(col1) col1_nulls
, COUNT(*) - COUNT(col2) col2_nulls
,..
, COUNT(*) - COUNT(colN) colN_nulls
, COUNT(*) total_rows
FROM T
/
Where col1, col2, .., colN should be replaced with actual names of columns of T table.
Aggregate functions -like COUNT()- ignore NULL values, so COUNT(*) - COUNT(col) will give you how many nulls for each column.
Summarize all NULLs of a table
If you want to know how many fields are NULL, I mean every NULL of every record you can
WITH d as (
SELECT COUNT(*) - COUNT(col1) col1_nulls
, COUNT(*) - COUNT(col2) col2_nulls
,..
, COUNT(*) - COUNT(colN) colN_nulls
, COUNT(*) total_rows
FROM T
) SELECT col1_nulls + col1_nulls +..+ colN_null
FROM d
/
Summarize all NULLs of a table (using Oracle dictionary tables)
Following is an improvement in which you need to now nothing but table name and it is very easy to code a function based on it
DECLARE
T VARCHAR2(64) := '<YOUR TABLE NAME>';
expr VARCHAR2(32767);
q INTEGER;
BEGIN
SELECT 'SELECT /*+FULL(T) PARALLEL(T)*/' || COUNT(*) || ' * COUNT(*) OVER () - ' || LISTAGG('COUNT(' || COLUMN_NAME || ')', ' + ') WITHIN GROUP (ORDER BY COLUMN_ID) || ' FROM ' || T
INTO expr
FROM USER_TAB_COLUMNS
WHERE TABLE_NAME = T;
-- This line is for debugging purposes only
DBMS_OUTPUT.PUT_LINE(expr);
EXECUTE IMMEDIATE expr INTO q;
DBMS_OUTPUT.PUT_LINE(q);
END;
/
Due to calculation implies a full table scan, code produced in expr variable was optimized for parallel running.
User defined function null_fields
Function version, also includes an optional parameter to be able to run on other schemas.
CREATE OR REPLACE FUNCTION null_fields(table_name IN VARCHAR2, owner IN VARCHAR2 DEFAULT USER)
RETURN INTEGER IS
T VARCHAR2(64) := UPPER(table_name);
o VARCHAR2(64) := UPPER(owner);
expr VARCHAR2(32767);
q INTEGER;
BEGIN
SELECT 'SELECT /*+FULL(T) PARALLEL(T)*/' || COUNT(*) || ' * COUNT(*) OVER () - ' || listagg('COUNT(' || column_name || ')', ' + ') WITHIN GROUP (ORDER BY column_id) || ' FROM ' || o || '.' || T || ' t'
INTO expr
FROM all_tab_columns
WHERE table_name = T;
EXECUTE IMMEDIATE expr INTO q;
RETURN q;
END;
/
-- Usage 1
SELECT null_fields('<your table name>') FROM dual
/
-- Usage 2
SELECT null_fields('<your table name>', '<table owner>') FROM dual
/
Thank you #Lord Peter :
The below PL/SQL script works
declare
cnt number :=0;
temp number :=0;
begin
for r in ( select column_name, nullable
from user_tab_columns
where table_name = upper('test')
order by column_id )
loop
if r.nullable = 'Y' then
EXECUTE IMMEDIATE 'SELECT count(*) FROM test where '|| r.column_name ||' IS NULL' into temp ;
cnt := cnt + temp;
END IF;
end loop;
dbms_output.put_line('Total: '||cnt);
end;
/
The table name test may be replaced the name of table of your interest.
I hope this solution is useful!
The dynamic SQL you execute (this is the string used in EXECUTE IMMEDIATE) should be
select sum(
decode(a,null,1,0)
+decode(b,null,1,0)
+decode(c,null,1,0)
) nullcols
from test;
Where each summand corresponds to a NOT NULL column.
Here only one table scan is necessary to get the result.
Use the data dictionary to find the number of NULL values almost instantly:
select sum(num_nulls) sum_num_nulls
from all_tab_columns
where owner = user
and table_name = 'TEST';
SUM_NUM_NULLS
-------------
6
The values will only be correct if optimizer statistics were gathered recently and if they were gathered with the default value for the sample size.
Those may seem like large caveats but it's worth becoming familiar with your database's statistics gathering process anyway. If your database is not automatically gathering statistics or if your database is not using the default sample size those are likely huge problems you need to be aware of.
To manually gather stats for a specific table a statement like this will work:
begin
dbms_stats.gather_table_stats(user, 'TEST');
end;
/
select COUNT(1) TOTAL from table where COLUMN is NULL;
I am trying to find a more efficient way to write PL/SQL Query to
to select insert from a table with 300+ columns, to the back up version of that table (same column names + 2 extra columns).
I could simply type out all the column names in the script (example below), but with that many names, it will bother me... :(
INSERT INTO
TABLE_TEMP
(column1, column2, column3, etc)
(SELECT column1, column2, column3, etc FROM TABLE WHERE id = USER_ID);
Thanks in advance
Specify literals/null for those two extra columns.
INSERT INTO
TABLE_TEMP
SELECT t1.*, null, null FROM TABLE t1 WHERE id = USER_ID
You can pretty easily build a column list for any given table:
select table_catalog
,table_schema
,table_name
,string_agg(column_name, ', ' order by ordinal_position)
from information_schema.columns
where table_catalog = 'catalog_name'
and table_schema = 'schema_name'
and table_name = 'table_name'
group by table_catalog
,table_schema
,table_name
That should get you nearly where you need to be.
The question tag says plsql, which is Oracle or one of its variants. Here is an example of doing it in Oracle:
drop table brianl.deleteme1;
drop table brianl.deleteme2;
CREATE TABLE brianl.deleteme1
(
a INTEGER
, b INTEGER
, c INTEGER
, efg INTEGER
);
CREATE TABLE brianl.deleteme2
(
b INTEGER
, c INTEGER
, d INTEGER
, efg INTEGER
);
DECLARE
l_ownerfrom VARCHAR2 (30) := 'BRIANL';
l_tablefrom VARCHAR2 (30) := 'DELETEME1';
l_ownerto VARCHAR2 (30) := 'BRIANL';
l_tableto VARCHAR2 (30) := 'DELETEME2';
l_comma VARCHAR2 (1) := NULL;
BEGIN
DBMS_OUTPUT.put_line ('insert into ' || l_ownerto || '.' || l_tableto || '(');
FOR eachrec IN ( SELECT f.column_name
FROM all_tab_cols f INNER JOIN all_tab_cols t ON (f.column_name = t.column_name)
WHERE f.owner = l_ownerfrom
AND f.table_name = l_tablefrom
AND t.owner = l_ownerto
AND t.table_name = l_tableto
ORDER BY f.column_name)
LOOP
DBMS_OUTPUT.put_line (l_comma || eachrec.column_name);
l_comma := ',';
END LOOP;
DBMS_OUTPUT.put_line (') select ');
l_comma := NULL;
FOR eachrec IN ( SELECT f.column_name
FROM all_tab_cols f INNER JOIN all_tab_cols t ON (f.column_name = t.column_name)
WHERE f.owner = l_ownerfrom
AND f.table_name = l_tablefrom
AND t.owner = l_ownerto
AND t.table_name = l_tableto
ORDER BY f.column_name)
LOOP
DBMS_OUTPUT.put_line (l_comma || eachrec.column_name);
l_comma := ',';
END LOOP;
DBMS_OUTPUT.put_line (' from ' || l_ownerfrom || '.' || l_tablefrom || ';');
END;
This results in this output:
insert into BRIANL.DELETEME2(
B
,C
,EFG
) select
B
,C
,EFG
from BRIANL.DELETEME1;
Nicely formatted:
INSERT INTO brianl.deleteme2 (b, c, efg)
SELECT b, c, efg
FROM brianl.deleteme1;
I have dinamics tables on my system, it change one number on the name so I do not have a fixed name. I wish to select the lasted table but only the name of the table to make a select to this table.
SELECT * FROM ((SELECT * FROM (select a.tablespace_name || '.' || a.table_name AS TABLE_NAME
from user_tables a
wherE a.tablespace_name='USERNAME'
and a.table_name like '%_DIN'
ORDER BY TABLE_NAME DESC)
WHERE ROWNUM = '1'))
for example.
the table name of
SELECT * FROM (select a.tablespace_name || '.' || a.table_name AS TABLE_NAME
from user_tables a
wherE a.tablespace_name='USERNAME'
and a.table_name like '%_DIN'
ORDER BY TABLE_NAME DESC)
WHERE ROWNUM = '1'
return:
7_DIN
is a name of one table in my tablespace
I want to take this name and make a select of this table.
something like
select * from 7_DIN
All in one statement
Below is code step-by-step, you can test it in this SQLFiddle.
Sample tables used for test:
create table t1(n number)
/
create table t2(n number)
/
create table t13(n number)
/
insert into t1(n) values(1)
/
insert into t2(n) values(2)
/
insert into t13(n) values(13)
/
Declare types used as selection result, same as row type of tables with dynamic names:
create or replace type t_row as object(n number)
/
create or replace type t_rowlist as table of t_row
/
Function which searches for last table and selects data from them into collection, then returns collection as table data:
create or replace function get_last_data return t_rowlist
as
v_table varchar2(30);
v_rows t_rowlist;
begin
select table_name into v_table from (
select * from user_tables where table_name like 'T%'
order by lpad(substr(table_name,2),40,'0') desc
)
where rownum = 1;
execute immediate 'select t_row(n) from '|| v_table
bulk collect into v_rows;
return v_rows;
end;
/
Create view based on function data:
create or replace view last_data_view as
select * from table(get_last_data)
/
This works good only if dynamic tables don't have a big amount of data.
Otherwise it's better to use pipelined functions. To do so, just replace function implementation with code below:
create or replace function get_last_data_pipe
return t_rowlist pipelined
as
v_table varchar2(30);
v_row t_row;
v_cursor sys_refcursor;
begin
select table_name into v_table from (
select * from user_tables where table_name like 'T%'
order by lpad(substr(table_name,2),40,'0') desc
)
where rownum = 1;
open v_cursor for 'select t_row(n) from '|| v_table;
loop
fetch v_cursor into v_row;
exit when v_cursor%notfound;
pipe row(v_row);
end loop;
close v_cursor;
return;
end;
Link to test SQLFiddle.
I have table TABLE_INFO with columns TNAME, CNAME, DTYPE, COLCOMMENT
I need to create a SQL script in Oracle SQL Developer, which creates new table with table name from column TNAME, columns from CNAME, datatype of columns from DTYPE and column comment from COLCOMMENT
How can I do this?
For example table TABLE_INFO has following data:
|TNAME | CNAME | DTYPE | Comment |
1|Employee | Emp_ID | NUMBER | Employee ID |
2|Employee | Emp_Nm| VARCHAR | Employee Name |
3|Job | Job_ID | NUMBER | Job ID | etc.
I need to write many CREATE TABLE statements like :
CREATE TABLE EMLOYEE
(EMP ID NUMBER,
Emp_Nm VARCHAR)
COMMENT ON COLUMN EMP_ID is ''Employee ID''
COMMENT ON COLUMN EMP_NM is ''Employee NAME''
for each TNAME
Oh, I confused.
This is a code for schema:
CREATE TABLE TABLE_INFO
(
TNAME VARCHAR2 (30)
,CNAME VARCHAR2 (30)
,DTYPE VARCHAR2 (30)
,COLCOMMENT VARCHAR2 (100)
);
INSERT INTO TABLE_INFO VALUES ('Employee','Emp_ID','NUMBER','Employee ID');
INSERT INTO TABLE_INFO VALUES ('Employee','Emp_Nm','VARCHAR','Employee Name');
INSERT INTO TABLE_INFO VALUES ('Employee','Emp_Sal','NUMBER','Employee Salary');
INSERT INTO TABLE_INFO VALUES ('Job','Job_ID','NUMBER','Job ID');
INSERT INTO TABLE_INFO VALUES ('Job','Job_Nm','VARCHAR','Job Name');
INSERT INTO TABLE_INFO VALUES ('Department','Dept_ID','VARCHAR','Department ID');
INSERT INTO TABLE_INFO VALUES ('Department','Dept_Nm','VARCHAR','Department Name');
And, this is a code for execution:
-- FOR TABLE CREATION
SELECT
CASE WHEN RNO = 1 THEN 'CREATE TABLE '||T1.TNAME||' ( ' ELSE ' ' END ||
T2.CNAME || ' ' || T2.DTYPE ||
CASE WHEN CNT = RNO THEN ');' ELSE ',' END AS SQLTEXT
FROM ( SELECT TNAME
,COUNT (1) CNT
FROM TABLE_INFO
GROUP BY TNAME) T1
,(SELECT ROW_NUMBER () OVER (PARTITION BY TNAME ORDER BY CNAME) RNO
,X.*
FROM TABLE_INFO X) T2
WHERE T1.TNAME = T2.TNAME
ORDER BY T1.TNAME, T2.RNO;
-- FOR COMMENT CREATION
SELECT 'COMMENT ON TABLE ' || TNAME || '.' || CNAME || ' IS ''' || COLCOMMENT || ''';' AS SQLTEXT
FROM TABLE_INFO;
You could find the result at here
NOTE: I used ROW_NUMBER () function at Oracle only. please be aware of that.
Try this.
SELECT T1.TABLE_NAME
,T1.COLUMN_NAME
,T1.DATA_TYPE
,T2.COMMENTS
FROM ALL_TAB_COLS T1
,ALL_COL_COMMENTS T2
WHERE T1.OWNER = T2.OWNER
AND T1.TABLE_NAME = T2.TABLE_NAME
AND T1.COLUMN_NAME = T2.COLUMN_NAME
It will retrieve all accessible tables information.
To include DBA tables, use DBA_TAB_COLS and DBA_COL_COMMENTS instead of ALL_TAB_COLS and ALL_COL_COMMENTS respectively.
You can also give more conditions to that query.
To insert this,
INSERT INTO TABLE_INFO (
TNAME
,CNAME
,DTYPE
,COLCOMMENT)
SELECT T1.TABLE_NAME
,T1.COLUMN_NAME
,T1.DATA_TYPE
,T2.COMMENTS
FROM ALL_TAB_COLS T1
,ALL_COL_COMMENTS T2
WHERE T1.OWNER = T2.OWNER
AND T1.TABLE_NAME = T2.TABLE_NAME
AND T1.COLUMN_NAME = T2.COLUMN_NAME
I've found anouther solution
CREATE OR REPLACE
PROCEDURE CREATEFROMTABLE AS
BEGIN
for j in (select DISTINCT tname from t_info) loop `
dbms_output.put_line('create table '|| j.tname || ' as (');`
for col in (select CNAME, dtype from T_INFO where tname = j.tname)
loop
dbms_output.put_line(col.cname|| ' ' || col.dtype);
end loop;
dbms_output.put_line(')');`
dbms_output.put_line('');`
for col in (select cname, dcmt from T_INFO where tname = j.tname) `
loop`
dbms_output.put_line('COMMENT ON COLUMN '|| col.cname || ' is ' || col.dcmt || ' ');`
end loop; `
`dbms_output.put_line(' ');`
`end loop;`
END CREATEFROMTABLE;
t_info is main table with columns tname, cname, dcmt
tname is new table name
cname is column name
dcmt is comment on the column