简体   繁体   English

在Oracle存储过程中使用表变量

[英]Using table variables in Oracle Stored Procedure

I have lots of experience with T-SQL (MS SQL Server). 我在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 表变量或说临时表t ,然后使用此t
throughout the whole SP body using it just like a regular table (for JOINS, sub-queries, etc.). 像常规表(用于JOINS,子查询等)一样在整个SP主体中使用它。

Now I am trying the same thing in Oracle but it's a pain. 现在,我在Oracle中尝试相同的操作,但这很痛苦。 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) ? 我开始考虑使用此表变量根本有什么可能(在SP主体中)不能做什么?

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?! 我现在可以在我的SP中使用此tc在地球上做什么?

This is what I am trying to do in the body of the SP. 这就是我要在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 V_CAMPAIGN是一个数据库视图, tc是我的表变量

Presumably you are trying to update a subset of the V_CAMPAIGN records. 大概您正在尝试更新V_CAMPAIGN记录的子集。 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. 在SQLServer中,定义一个包含子集的“临时”表,然后对其进行操作可能会很有用,因为这在Oracle中是不必要的。 Simply update the table with the where clause you would have used to define the temp table. 只需使用将用于定义临时表的where子句更新表。 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. Oracle使用不同的锁定策略,因此该技术几乎是不必要的。

Echoing a comment above - tell us what you want to achieve in Oracle and you will get suggestions for the best way forward. 在上面回荡一番评论-告诉我们您想在Oracle中实现什么,您将获得有关最佳前进方式的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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