Skip to content
Advertisement

Oracle SQL Developer dynamic date table name

I’m hoping to dynamically reference last Fridays date in the weekly sales tables in Oracle SQL Developer i.e. SELECT * FROM Sales_DDMMYY

I can do this in SQL Server (DECLARE / SET / EXECUTE) but haven’t had any joy with SQL Developer.

Even the ability to create a date variable to be referenced within the code would be a great start.

Advertisement

Answer

Stop!

I strongly suggest you not to do that. That’s not the way to create a data model. If you have a table which contains values related to different dates, then date should be a column in that table, such as

Insert rows as

and use it as

That is the way to do it. Naming columns by dates is … well, close to a suicide.


Aha, now I see: it is a table name that contains dates. Doesn’t really matter, my suggestion still stands. Do not do that. Use a date column within a single table.

If you want – and if you can afford it – partition the table on date value. Note that partitioning option exists in Oracle Enterprise Edition which is quite expensive. So – date column it is.


If there’s nothing you can do about it, then dynamic SQL it is. For example:

Sample table:

Function that accepts ddmmyy value as a parameter, composes table name and returns a refcursor:

Testing:


Without creating a function: use substitution variable:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement