Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle create table based on multiple conditions

Posted on 2011-02-21
4
Medium Priority
?
405 Views
Last Modified: 2012-05-11
Hi,

I'm trying to figure out the best approach to creating an oracle table If a certain condition is true.

for example.
Create table1 if name <> ' ' and address <> ' '
Create table1 if name <> ' ' and address <> ' ' and phone = ' '

Should a case statement be used in this scenario?
0
Comment
Question by:phoen08
4 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 34945643
you can't do conditional creates within a single statement.
you'll have to check your conditions first then issue whatever create is appropriate.

what is the intent here?  It's not standard (and certainly not recommended) practice to create tables on the fly in Oracle.

Are you trying to create "temp" tables to work with?  If so, doing it dynamically is the wrong approach
0
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
ID: 34946746
Are you looking something like this?

File_1.sql
Set Serveroutput on
set echo off
.
.
.
spool &1
begin
  if name <> '' and address <> '' then
    DBMS_OUTPUT.PUT_LINE('CREATE TABLE(col1 varchar2(2));');
  end if;

  if name <> '' and address <> '' then
    DBMS_OUTPUT.PUT_LINE('CREATE TABLE (col1 varchar2(2), col2 varchar(2)); ');
  end if;
end;
spool off;

sqlplus -s username/password @File_1.sql my_spool.sql
sqlplus -s username/password @my_spool.sql
0
 

Author Comment

by:phoen08
ID: 34950691
I'm trying to convert an old process that would export output to a text file to a process that would store the data in oracle tables. One of the sections in the file process outputs columns based on whatever condition is presents.  After further review, I believe this step in the process is only changing the column values based on the condition.  So, I may just need multiple case statements to change the column data based on the condition.  There would only be one table created from all of the condition statements.
0
 
LVL 48

Accepted Solution

by:
schwertner earned 2000 total points
ID: 34950993
Dynamic SQL could be an answer:

.......
v_sql   VARCHAR2(900);
.......

IF  name <> ' ' and address <> ' '  THEN v_sql := '  Create table1 (col1 VARCHAR2(80), .....)';
ELSIF  name = ' some' and address = ' another' and phone = '906377 ' THEN v_sql := '  Create table2 (col1 NUMBER,.....)';
END IF;

execute immediate v_sql;

....

EXCEPTION
   WHEN OTHERS THEN
   .....




0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

824 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