Mounika Tirumala
asked on
insert statement
i have 2 tables like
T1 T2
----- ----
id name id name
--- ------- ---- -------
10 xyz
20 abc
in this the first tabe (T1) id column data type is number
and second column(T2) id column data type is varchar2
Now,
I want to insert data from table T1 to T2
how can i insert
T1 T2
----- ----
id name id name
--- ------- ---- -------
10 xyz
20 abc
in this the first tabe (T1) id column data type is number
and second column(T2) id column data type is varchar2
Now,
I want to insert data from table T1 to T2
how can i insert
--> insert data from table T1 to T2
Pls try ..
Insert into T2(Id,Name)
SELECT Id, CAST(Name AS VARCHAR2(100)) Name FROM T1
Pls try ..
Insert into T2(Id,Name)
SELECT Id, CAST(Name AS VARCHAR2(100)) Name FROM T1
Oracle does implicit conversion
Insert into t2 (id, name) select id, name from t1 will work
Insert into t2 (id, name) select id, name from t1 will work
try
truncate table table2; -- comment out this if you dont want to clear it first
insert into table2 select * from table1;
Or convert explictly -
insert into t2(id, name) select to_char(id), name from t1;
insert into t2(id, name) select to_char(id), name from t1;
You have two options:
1. Use the simple approach that Geert suggested (this is the easiest way).
2. Convert the data explicitly as awking00 suggested (this is the safest way).
Which one is best for you? You can choose. In this simple case, I would use Geert's approach. But, be careful with counting on Oracle to do implicit datatype conversions for you. Going from NUMBER or DATE to VARCHAR2 as in your example here, will always work without error. But, going the other direction, from VARCHAR2 to NUMBER or DATE will work only sometimes, depending on the data. You are always much safer to do any necessary datatype conversions explicitly, as awking00 suggested here.
Why two others suggest deleting (or even truncating!) table 2 first, I don't know. You didn't indicate that you want that functionality. Be *VERY* careful with: "truncate"! That is extremely efficient, especially if a table has lots of records and you want them all to be removed. But, that is permament!. If you ever use that command where you didn't want all records removed, you will have to do a recovery from a database backup.
1. Use the simple approach that Geert suggested (this is the easiest way).
2. Convert the data explicitly as awking00 suggested (this is the safest way).
Which one is best for you? You can choose. In this simple case, I would use Geert's approach. But, be careful with counting on Oracle to do implicit datatype conversions for you. Going from NUMBER or DATE to VARCHAR2 as in your example here, will always work without error. But, going the other direction, from VARCHAR2 to NUMBER or DATE will work only sometimes, depending on the data. You are always much safer to do any necessary datatype conversions explicitly, as awking00 suggested here.
Why two others suggest deleting (or even truncating!) table 2 first, I don't know. You didn't indicate that you want that functionality. Be *VERY* careful with: "truncate"! That is extremely efficient, especially if a table has lots of records and you want them all to be removed. But, that is permament!. If you ever use that command where you didn't want all records removed, you will have to do a recovery from a database backup.
ASKER
thank you so much pawan kumar sir
welcome.
I wonder why you accepted the more-complex (therefore: slower) syntax that Pawan suggested, instead of the simpler syntax that Geert suggested. Pawan's suggestion does not address the different datatype for the ID column that you asked about. He added the "CAST" operator on the NAME column, but that is VARCHAR2 in both tables, so there is no need to do any converting of that column.
I pretty much had the same question.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
create table T2 as select * from T1