Solved

How to use case in insert into select statement?

Posted on 2014-11-20
8
480 Views
Last Modified: 2014-11-20
Dear Experts,

Is below statement is true or can you offer an alternative?

Table TA (x,y,z,t)
Table TB (a,b,c,d)

insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else TB.d end)

Please be careful that if c=1 then assign constant value 'XXX' else assign value of column d)

BR
0
Comment
Question by:GurcanK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 40455218
insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else d end) from tb
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40455221
>>insert into TA (x,y,z,t) select a,b,c,(case when c=1 then 'XXX' else TB.d end)

you are missing the FROM TB after the select.

Just like the last two questions:  Looks good.

Just set up some test tables and try it.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 250 total points
ID: 40455224
insert into TA (x,y,z,t) select a,b,c, decode(c,1,'XXX',d) from tb
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 250 total points
ID: 40455230
Missed something:
then 'XXX' else TB.d

Since 'XXX' is a string, TB.d will need to be a varcahr2 or similar string variable.  if it is a number or a date, you will likely get an error.  You will need to convert TB.d to a string datattype by using TO_CHAR and the appropriate formatting.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40455241
actually, since 'XXX' is a string, and it's the first return result in the CASE, it's determining the data type of the rest of the case results,  so anything else will be implicitly converted to text (char)

so, you probably won't get an error, but you might get funny results if your data converts into a format you don't want.

So, TO_CHAR with an explicit format for non-text data is a good idea.

Same applies to the DECODE alternative
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40455246
Version:  11.2.0.2:

SQL> select case when 1=1 then 'XXX' else 1 end from dual
                                     *
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected CHAR got NUMBER


SQL> select case when 1=1 then 'XXX' else to_char(1) end from dual;
XXX
0
 

Author Comment

by:GurcanK
ID: 40455256
Thanks all, fortunately data-types are the same.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40455267
interesting - I stand corrected.  Thanks!

I know I've seen the first element determine data types before.  Maybe it was a bug in older versions.

Does work with decode though

SELECT DECODE(1, 1, 'XXX', 1) FROM DUAL;


and thanks again!
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question