Is it possible to duplicate all values in a table while updating one or more columns - sql

I have a table with many columns, and what I would like to do is duplicate all of the rows in the table, but also update one of the columns to a new value.
For example lets say I have the table below. I want to add to my table a duplicate of each row, except instead of BASIC access, it will have 'ADVANCED':
Before:
NAME, GENDER, ACCESS
----------------------
STEVE, MALE, BASIC
MOLLY, FEMALE, BASIC
After
NAME, GENDER, ACCESS
----------------------
STEVE, MALE, BASIC
MOLLY, FEMALE, BASIC
STEVE, MALE, ADVANCED
MOLLY, FEMALE, ADVANCED
Is there a way to do this without specifying all columns? I have 60 columns in the table, and the structure can change (meaning columns may be added, removed, renamed, etc).
Is it possible in Oracle SQL to automate this?

Just use insert . . . select:
insert into t (name, gender, access)
select name, gender, 'ADVANCED'
from t;
You need to list all the columns. You can shorten the manual process by using a query to generate the list. If you had to do this a lot and always knew you were leaving out access and access is the last column, you could use a view:
create view v_t as
select . . . -- all but access
from t;
insert into t ( . . . )
select v.*, 'ADVANCED'
from v_t;
Or you could use dynamic SQL to generate the statement.
However, I don't recommend any of those. Instead I would be concerned about a data model where you are regularly adding and modifying the columns in a table. That sounds dangerous.

Without specifying all the columns? With some help of a "temporary" table, here's how:
Your current table:
SQL> create table test
2 (name varchar2(10),
3 gender varchar2(20),
4 caccess varchar2(20));
Table created.
SQL> insert into test
2 select 'steve', 'male', 'basic' from dual union all
3 select 'molly', 'female', 'basic' from dual;
2 rows created.
Create a "temporary" table as a copy of the "original" table
update column you want to modify
copy the whole "temporary" table to the "original"
drop the "temporary" table
SQL> create table test_temp as select * From test;
Table created.
SQL> update test_temp set caccess = 'advanced';
2 rows updated.
SQL> insert into test select * From test_temp;
2 rows created.
SQL> drop table test_Temp;
Table dropped.
SQL> select * From test;
NAME GENDER CACCESS
---------- -------------------- --------------------
steve male basic
molly female basic
steve male advanced
molly female advanced
SQL>
Apparently, that works, but - what if the original table is huge? It takes a lot of space, and its copy takes approximately twice as much. Why are you doing that, anyway?

Try below method with anonymous block to avoid listing columns in insert statements
CREATE TABLE ACCESS_CHN
(NAAME VARCHAR2(100),
GENDER VARCHAR2(20),
ACCCESS VARCHAR2(30))
INSERT into ACCESS_CHN values('STEVE','MALE','BASIC');
INSERT into ACCESS_CHN values('MOLLY','FEMALE','BASIC');
COMMIT;
DECLARE
column_list varchar2(2000):=NULL;
plsql_block VARCHAR2(1000);
BEGIN
select LISTAGG(column_name,',') within group (order by column_id)
into column_list
from user_tab_columns
where table_name='ACCESS_CHN';
plsql_block := 'CREATE TABLE ACCESS_CHN_BKP as select '|| column_list || ' from ACCESS_CHN';
EXECUTE IMMEDIATE plsql_block;
plsql_block := 'UPDATE ACCESS_CHN_BKP set ACCCESS=''ADVANCED'' ';
EXECUTE IMMEDIATE plsql_block;
COMMIT;
plsql_block := 'CREATE TABLE ACCESS_CHN_FINAL as select * from ACCESS_CHN
union all
select * from ACCESS_CHN_BKP';
EXECUTE IMMEDIATE plsql_block;
END;
--To rerun drop tables ACCESS_CHN_BKP and ACCESS_CHN_FINAL

Related

Oracle SQL/PLSQL: change type of specific columns in one time

Assume following table named t1:
create table t1(
clid number,
A number,
B number,
C number
)
insert into t1 values(1, 1, 1, 1);
insert into t1 values(2, 0, 1, 0);
insert into t1 values(3, 1, 0, 1);
clid A B C
1 1 1 1
2 0 1 0
3 1 0 1
Type of columns A, B, and C is number. What I need to do is to change types of those columns to VARCHAR but in a quite tricky way.
In my real table I need to change datatype for hundred of columns so it is not so convenient to write a statement like following hundred of time:
ALTER TABLE table_name
MODIFY column_name datatype;
What i need to do is rather to convert all columns to VARCHAR except CLID column like we can do that in Python or R
Is there any way to do so in Oracle SQL or PLSQL?
Appreciate your help.
Here is a example of procedure that can help...
It accepts two parameters that should be a name of your table and list of columns you do not want to change...
At the begining there is a cursor that gets all the column names for your table except the one that you do not want to change...
Then it loop's though the columns and changes them...
CREATE OR REPLACE procedure test_proc(p_tab_name in varchar2
, p_col_names in varchar2)
IS
v_string varchar2(4000);
cursor c_tab_cols
is
SELECT column_name
FROM ALL_TAB_COLS
WHERE table_name = upper(p_tab_name)
and column_name not in (select regexp_substr(p_col_names,'[^,]+', 1, level) from dual
connect by regexp_substr(p_col_names, '[^,]+', 1, level) is not null);
begin
FOR i_record IN c_tab_cols
loop
v_string := 'alter table ' || p_tab_name || ' modify '
|| i_record.column_name || ' varchar(30)';
EXECUTE IMMEDIATE v_string;
end loop;
end;
/
Here is a demo:
DEMO
You can also extend this procedure with a type of data you want to change into... and with some more options I am sure....
Unfortunately, that isn't as simple as you'd want it to be. It is not a problem to write query which will write query for you (by querying USER_TAB_COLUMNS), but - column must be empty in order to change its datatype:
SQL> create table t1 (a number);
Table created.
SQL> insert into t1 values (1);
1 row created.
SQL> alter table t1 modify a varchar2(1);
alter table t1 modify a varchar2(1)
*
ERROR at line 1:
ORA-01439: column to be modified must be empty to change datatype
SQL>
If there are hundreds of columns involved, maybe you can't even
create additional columns in the same table (of VARCHAR2 datatype)
move values in there
drop "original" columns
rename "new" columns to "old names"
because there'a limit of 1000 columns per table.
Therefore,
creating a new table (with appropriate columns' datatypes),
moving data over there,
dropping the "original" table
renaming the "new" table to "old name"
is probably what you'll finally do. Note that it won't be necessarily easy either, especially if there are foreign keys involved.
A "query that writes query for you" might look like this (Scott's sample tables):
SQL> SELECT 'insert into dept (deptno, dname, loc) '
2 || 'select '
3 || LISTAGG ('to_char(' || column_name || ')', ', ')
4 WITHIN GROUP (ORDER BY column_name)
5 || ' from emp'
6 FROM user_tab_columns
7 WHERE table_name = 'EMP'
8 AND COLUMN_ID <= 3
9 /
insert into dept (deptno, dname, loc) select to_char(EMPNO), to_char(ENAME), to_char(JOB) from emp
SQL>
It'll save you from typing names of hundreds of columns.
I think its not possible to change data type of a column if values are there...
Empty the column by copying values to a dummy column and change data types.

In my table i don't have a column but i want to use in where condition is it possible or not

Suppose i have two database having same table name but columns name(empno,employee_no) are different so, i want to use single query in both of the table query is given below but i want in a single query.
In where condition i have mention there two different column is it possible or not to write in a single query.
select ename
from emp
where empno = '1234'---- in 1st databse
select ename
from emp
where employee_no = '5678' ---- in 2nd database
I want to execute a single query in both database which have two different column of empno and employee_no.
Can anyone please help with same.
One simple method is:
Create view in both the database with same name and same alias for empno and employee_no.
--In first db
CREATE VIEW MY_TAB_V AS
SELECT EMPNO AS ENO,
...
FROM MY_TAB;
--In second db
CREATE VIEW MY_TAB_V AS
SELECT EMPLOYEE_NO AS ENO,
...
FROM MY_TAB;
Now, you will be able to execute following query in both the databases:
Select * from MY_TAB_V where ENO=1234;
Create a simple view so that you will be able to even update the tables using that view.
Cheers!!
It is possible to have a dynamic SQL statement that queries different columns depending on what is available, by combining DBMS_XMLGEN.GETXML with the data dictionary. However, this solution is weird, slow, and buggy, and should be a last resort. It's usually better to put this level of dynamicism in an application or a PL/SQL block.
--Conditional select on EMP depending on which column exists.
--
--Convert XML to regular values.
select extractvalue(xml, '/ROWSET/ROW/ENAME') empname
from
(
--Convert to XMLType.
--(This is nontrivial because NULL must be handled oddly.)
select
case when xml is null then
xmltype('<?xml version="1.0"?><ROWSET><ROW><ENAME></ENAME></ROW></ROWSET>')
else xmltype(xml)
end xml
from
(
--Get dynamic result as XML.
select dbms_xmlgen.getxml('select ename from emp where '||column_name||' = 1234') xml
from dba_tab_columns
where owner = user
and table_name = 'EMP'
and column_name in ('EMPNO', 'EMPLOYEE_NO')
)
);
Use these commands to test the statement:
drop table emp;
create table emp(ename varchar2(100), empno number);
insert into emp values ('asdf', 1234);
commit;
<run the above SELECT>
drop table emp;
create table emp(ename varchar2(100), employee_no number);
insert into emp values ('qwer', 1234);
commit;
<run the above SELECT>

Oracle Object : How to show all the fields in select query?

I have the following Oracle Object :
CREATE TYPE person_typ AS OBJECT (
Id NUMBER,
first_name VARCHAR2(20),
last_name VARCHAR2(25));
And a table :
CREATE TABLE MyTable (
contact person_typ,
contact_date DATE );
I would like to make a select query to show all the fields of the Person_Typ object without specifiying their names.
When I do
select * from MyTable
The column contact is shown as [unsupported data type], I have to use instead :
select T.contact.ID, T.contact.First_name, T.contact.last_name from MyTable T
Is there another way to show the values of the object without specifying the column names ?
Thanks,
Cheers,
I don't use SQL Developer, but according to this article Showing TYPE’d Column Values in SQL Developer you could use option:
Preferences / Database / Advanced / Display Struct Value in Grid
Also you can query user_type_attr (or all_type_attr) to obtain column names. Then copy/paste select part from output and run it or create view as proposed by #sep. Here is my test data and code block:
insert into mytable values (person_typ(1, 'Paulina', 'Thomson'), date '2017-12-17');
insert into mytable values (person_typ(7, 'Keanu', 'Stevens'), date '2017-12-28');
declare
v_sql varchar2(32000);
begin
select listagg('T.CONTACT.'||attr_name||' '||attr_name, ', ')
within group (order by attr_no)
into v_sql
from user_type_attrs
where type_name = 'PERSON_TYP';
v_sql := 'SELECT '||v_sql||' FROM MYTABLE T';
dbms_output.put_line(v_sql);
execute immediate 'CREATE OR REPLACE VIEW VW_CONTACTS AS '||v_sql;
end;
select * from vw_contacts;
Result:
ID FIRST_NAME LAST_NAME
------ -------------------- -------------------------
1 Paulina Thomson
7 Keanu Stevens
I had the same problem and I created a view with all the fields. In your case:
CREATE VIEW v_myTable AS
select T.contact.ID, T.contact.First_name, T.contact.last_name
from MyTable T
and use the view instead of the table
SELECT * from v_myTable

Create Auto Sequence text and number in oracle 11g

How I do create column ID with value JASG1?
I am only find example like this :
select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job
Although what you wrote probably works (if there's a sequence named MTJ_ID_SEQ, you have a privilege to select from it; the same goes for the TALEND_JOB table), I'd say that it isn't what you should use.
Here's why: I'll create a table and a sequence. Table will be pre-populated with some IDs (just to put something in there).
SQL> create sequence mtj_id_seq;
Sequence created.
SQL> create table talend_job as
2 select rownum id from dept;
Table created.
SQL> select * from talend_job;
ID
----------
1
2
3
4
OK; 4 rows so far. Now, run your SELECT:
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG1
JASG2
JASG3
JASG4
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from talend_job;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG5
JASG6
JASG7
JASG8
SQL>
See? You didn't get only 1 JASGx value, but as many as number of rows in the TALEND_JOB table. If there was a million rows, you'd get a million JASGx rows as well.
Therefore, maybe you meant to use DUAL table instead? E.g.
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from dual;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG9
SQL> select 'JASG'||to_char(mtj_id_seq.nextval) from dual;
'JASG'||TO_CHAR(MTJ_ID_SEQ.NEXTVAL)
--------------------------------------------
JASG10
SQL>
See? Only one value.
Also, notice that sequences will provide unique values, but you can't rely on them being gapless.
As you mentioned "how to create column ID" - one option is to use a trigger. Here's an example:
SQL> create table talend_job (id varchar2(20), name varchar2(20)
Table created.
SQL> create or replace trigger trg_bi_tj
2 before insert on talend_job
3 for each row
4 begin
5 :new.id := 'JASG' || mtj_id_seq.nextval;
6 end;
7 /
Trigger created.
Let's insert some names; IDs should be auto-populated by the trigger:
SQL> insert into talend_job (name) values ('littlefoot');
1 row created.
SQL> insert into talend_job (name) values ('Ishak');
1 row created.
SQL> select * From talend_job;
ID NAME
-------------------- --------------------
JASG11 littlefoot
JASG12 Ishak
SQL>
OK then; now you have some more info - read and think about it.
By the way, what is the "compiler-errors" tag used for? Did you write any code and it failed? Perhaps you'd want to share it with us.

How can we figure out that a column in my oracle table is being populated/updated by a trigger of another table?

Consider a scenario, where there are two tables "A" and "B".
Table "A" has a trigger "Ta" [written long before me joining this project and thus I'm completely unaware of the trigger], which updates a column named "colB" in table "B".
Now, since I'm mostly using table "B" and concerned about the way "colB" is getting, I won't know if trigger "Ta" is updating this column.
So my question is, is there a direct oracle query/way to find if a column in one table is getting updated by any trigger running on another table?
Thanks in advance for educating me on this.
Regards
a.b
SELECT *
FROM
user_sources
WHERE
type = 'TRIGGER'
AND UPPER(text) LIKE '%UPDATE A%';
But it won't work if the query is in two lines such as :
UPDATE
A
SET
...
because text matches to a given line in the corresponding object.
Oracle fine grained dependency tracking knows which columns are used. Unfortunately, there is no way to track if that dependency is for reading or writing. And there is no default DBA_DEPENDENCY_COLUMNS view to find this information.
But luckily Rob van Wijk has created such a view. His blog has some more information, including the grants and create view statement, about half-way down the page.
Example:
drop table a;
drop table b;
create table a(colA number);
create table b(colB number, read_only number, not_used number);
create or replace trigger Ta
after update or insert or delete on a
begin
update b set colB = read_only;
end;
/
--What triggers are referencing B's columns?
select owner, name, type, referenced_column
from dba_dependency_columns
where referenced_owner = user
and referenced_name = 'B'
and type = 'TRIGGER';
OWNER NAME TYPE REFERENCED_COLUMN
----- ---- ---- -----------------
JHELLER TA TRIGGER COLB
JHELLER TA TRIGGER READ_ONLY
The view uses several undocumented tables and some advanced SQL features. This view would not be a good idea on a production server. But it is probably much more accurate than any solution that involves parsing SQL.
Simple example:
create table table_a(
id number primary key,
val varchar2( 100 )
);
create table table_b(
len number
);
insert into table_b values ( 0 );
set define off
create or replace trigger after_table_a
after insert on table_a for each row
begin
UpDate
table_B
set len = len + length( :new.val );
end;
/
insert into table_a values ( 1, 'Ala ma kota');
insert into table_a values ( 2, 'As to ali pies');
commit;
select * from table_b;
LEN
----------
25
And the query:
select trigger_name,
regexp_substr( trigger_body, 'update\s+table_b',1,1,'i') update_command
from (
select ut.trigger_name,
dbms_metadata.GET_DDL('TRIGGER', ut.trigger_name) trigger_body
from user_dependencies ud
join user_triggers ut on ( ud.type = 'TRIGGER'
and ut.trigger_name = ud.name
and ut.table_name <> ud.referenced_name )
where ud.referenced_name = 'TABLE_B'
)
where regexp_instr( trigger_body, 'update\s+table_b',1,1,0,'i') > 0 ;
TRIGGER_NAME UPDATE_COMMAND
------------- ------------------
AFTER_TABLE_A UpDate
table_B