3

I am trying to create 1 PL/SQL statement that would allow me to get multiple table outputs after each successive datetime iteration and rename each table with the year of that datetime iteration. Example below with desired results. Thanks

  Create table MY_TIME_XX AS 
   ( Select 
    X.* 
    FROM Metadata X )
    Where X.Datetime between '01/01/2014' and '12/31/2014'

So in the end my schema will have

  1. MY_TIME_14
  2. MY_TIME_15
  3. MY_TIME_16

etc....

5
  • Why do you like to do that? Most likely it does not make any sense. Commented Oct 27, 2016 at 17:55
  • the (Select * FROM Metadata X) is more than just what I showed for a five year span it is quite large as well. My temp file doesn't hold it well So I would like to Make a able for each year then put it into a partition single table. Commented Oct 27, 2016 at 18:16
  • 1
    Don't use string literals in date comparisons use either date literals or the to_date function: e.g. use to_date('01/01/2014','mm/dd/yyyy') or date '2014-1-1' instead of '01/01/2014' Commented Oct 27, 2016 at 20:32
  • 1
    Why not create the partitioned table to begin with. You can still insert a years worth of data at a time instead of all the data at once. Commented Oct 27, 2016 at 20:36
  • Don't understand the logic of wanting to do it. But wouldn't be hard to do. Have a look at docs.oracle.com/cd/B19306_01/appdev.../…. Just build your create table statement as a string then use execute immediate to fire it off. Commented Oct 28, 2016 at 5:21

1 Answer 1

5

Jobs like this require dynamic SQL. Assuming you know the years in scope something like this should do it for you.

begin
    for idx in 2014..2016 loop
        execute immediate 
            'Create table MY_TIME_'|| idx ||' AS  
                Select  X.* 
                FROM Metadata X 
                Where to_char(X.Datetime, ''yyyy'') = '''|| idx||'''';
    end loop;
end;  

Note the use of double quotes to escape literals in the string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.