Solved

Oracle create table based on multiple conditions

Posted on 2011-02-21
4
402 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
[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 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fill Null values 5 55
oracle collections 2 42
date show only hh:mm 2 50
How can track error data while copeing data from external file to database using utl_file. 5 43
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

739 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