简体   繁体   中英

Using table variables in Oracle Stored Procedure

I have lots of experience with T-SQL (MS SQL Server).

There it is quite common to first select some set of records into a
table variable or say temp table t , and then work with this t
throughout the whole SP body using it just like a regular table (for JOINS, sub-queries, etc.).

Now I am trying the same thing in Oracle but it's a pain. I get errors all the way and it keeps saying
that it does not recognize my table (ie my table variable).

Error(28,7): PL/SQL: SQL Statement ignored
Error(30,28): PL/SQL: ORA-00942: table or view does not exist

I start thinking what at all is possible to do with this table variable and what not (in the SP body) ?

I have this declaration:

TYPE V_CAMPAIGN_TYPE IS TABLE OF V_CAMPAIGN%ROWTYPE; 
tc V_CAMPAIGN_TYPE;

What on Earth can I do with this tc now in my SP?!

This is what I am trying to do in the body of the SP.

  UPDATE ( SELECT t1.STATUS_ID, t2.CAMPAIGN_ID
            FROM V_CAMPAIGN t1
            INNER JOIN tc t2 ON t1.CAMPAIGN_ID = t2.CAMPAIGN_ID
            ) z
  SET z.STATUS_ID = 4;

V_CAMPAIGN is a DB view, tc is my table variable

Presumably you are trying to update a subset of the V_CAMPAIGN records. While in SQLServer it may be useful to define a 'temporary' table containing the subset and then operate on that it isn't necessary in Oracle. Simply update the table with the where clause you would have used to define the temp table. Eg

UPDATE v_campaign z
   SET z.status_id = 4
 WHERE z.column_name = 'a value'
   AND z.status <> 4

I assume that the technique you are familiar with is to minimise the effect of read locks that are taken while selecting the data. Oracle uses a different locking strategy so the technique is mostly unnecessary.

Echoing a comment above - tell us what you want to achieve in Oracle and you will get suggestions for the best way forward.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM