English 中文(简体)
PL/SQL Query with Variables
原标题:

I have a fairly complex query that will be referencing a single date as a start or stop date multiple times throughout. I m going to have to run this query for 3 different fiscal years, and don t want to have to hunt down the date 17 times in order to change it throughout my query.

Is there a way to set a variable at the beginning of my query and reference it throughout? I m not looking to write a whole function, just reference a variable throughout my query.

最佳回答

You re not really saying how you reference this so I ll just show from SQL*Plus point of view.

Two ways

Have it prompt you for the value. Since you use the same variable many times you ll want to use the && operator.


    SQL> SELECT &&var, &&var FROM Dual;
    Enter value for var:  PUMPKIN 
    old   1: SELECT &&var, &&var FROM Dual
    new   1: SELECT  PUMPKIN ,  PUMPKIN  FROM Dual

     PUMPKI  PUMPKI
    ------- -------
    PUMPKIN PUMPKIN

Alternatively you could set it before you ran your SQL.


    SQL> VARIABLE new_var VARCHAR2(20);
    SQL> EXECUTE :new_var :=  PUMPKIN PIE ;

    PL/SQL procedure successfully completed.

    SQL> SELECT :new_var, :new_var FROM DUAL;

    :NEW_VAR                         :NEW_VAR
    -------------------------------- --------------------------------
    PUMPKIN PIE                      PUMPKIN PIE
问题回答

Yes, depends how you want to do it.

You could use an anonymous procedure IE:

BEGIN

   v_date DATE := TO_DATE(your_date, your_date_mask);

   [your query referencing v_date where ever you need];

END;

Or if you run the query in SQLPlus, you use & to note variables (IE: &your_date), and will be prompted for the value when you run the script.

As OMG Ponies says, inside PL/SQL you can always refer to any PL/SQL variable (including parameters) right in the SQL as long as it s static SQL. Outside PL/SQL, or if your SQL is dynamic (because native dynamic SQL doesn t support reusable named parameters at least as of 10g) you can use the following trick. Add the following before the WHERE clause in your query:

CROSS JOIN (SELECT :dateparam Mydate FROM dual) Dateview

And everywhere you want to refer to that value in your main query, call it Dateview.Mydate Then when you execute the query, you need only pass in the one bind parameter.

If you use Toad with second mouse button -> Execute as Script, so not prompt you for values:

var myVar varchar2(20);

exec :req := x ;

delete from MYTable where Field = :myVar;





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

Connecting to Oracle 10g with ODBC from Excel VBA

The following code works. the connection opens fine but recordset.recordCount always returns -1 when there is data in the table. ANd If I try to call any methods/properties on recordset it crashes ...

How to make a one to one left outer join?

I was wondering, is there a way to make a kind of one to one left outer join: I need a join that matches say table A with table B, for each record on table A it must search for its pair on table B, ...

Insert if not exists Oracle

I need to be able to run an Oracle query which goes to insert a number of rows, but it also checks to see if a primary key exists and if it does, then it skips that insert. Something like: INSERT ALL ...

How can I store NULLs in NOT NULL field?

I just came across NULL values in NOT-NULL fields in our test database. How could they get there? I know that NOT-NULL constraints can be altered with NOVALIDATE clause, but that would change table s ...

Type reference scope

I m studying databases and am currently working on a object-relational DB project and I ve encountered a small problem with the number of possible constraints in an object table. I m using "Database ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

热门标签