pl/sql中有BEFORE SELECT触发器吗?

发布于 2025-01-06 20:17:19 字数 428 浏览 1 评论 0原文

您好,我正在使用连接到 Oracle 数据库的 sqldeveloper。

我的情况是,我的数据库中有一个客户表,其中有一个名为 start_date 的日期字段,该字段保存用户注册当天的日期,还有一个名为 is_member 的“布尔”字段。每个会员资格仅持续一年,因此如果 SYSDATE > 则 is_member 为 false。开始日期 + 1 年

我想在用户调用 select 语句之前检查用户是否仍然是成员(如果不是,则更改 is_member)。这可能吗?

如果没有,有没有人知道如何保持 is_member 字段最新?您可以做一个每天调用一次的触发器来查看会员资格是否已过期吗?

提前致谢! =]

Hi I am using sqldeveloper conected to an oracle database.

My senario is I have a customer table in my database with a date field called start_date which holds the date of the day the user signed up and a "boolean" field called is_member. Each membership only lasts one year so is_member is false if SYSDATE > start_date + 1year.

I would like to do a check to see if a user is still a member (and change is_member if not) before the user calls a select statement. Is this possible?

If not has anyone got any ideas on how else to keep the is_member field up to date? Can you do a trigger that is called once a day to see if the membership has expiered?

Thanks in advace! =]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉睡月亮 2025-01-13 20:17:19

不要使用表列来存储相关数据,而应使用视图:

CREATE OR REPLACE VIEW customer_v AS
SELECT c.*, 
       CASE WHEN SYSDATE > add_months(c.start_date, 12) 
          THEN 'FALSE' 
          ELSE 'TRUE' 
       END is_member
  FROM customer c

is_member 视图列将始终包含最新数据。

Don't use a table column to store dependent data, use a view:

CREATE OR REPLACE VIEW customer_v AS
SELECT c.*, 
       CASE WHEN SYSDATE > add_months(c.start_date, 12) 
          THEN 'FALSE' 
          ELSE 'TRUE' 
       END is_member
  FROM customer c

The is_member view column will always contain up-to-date data.

诺曦 2025-01-13 20:17:19

PL/SQL 中不存在 BEFORE SELECT 触发器之类的东西。

听起来 is_member 根本不应该是表中的列,而应该是在视图中计算的内容。 像您的代码这样的东西

CREATE OR REPLACE VIEW view_name
AS
  SELECT <<list_of_columns>>
         (CASE WHEN sysdate > add_months(start_date, 12)
               THEN 'Y'
               ELSE 'N'
           END) is_member
    FROM your_table

将简单地查询视图而不是查询基表。

There is no such thing as a BEFORE SELECT trigger in PL/SQL.

It sounds like is_member should not be a column in a table at all but should be something that is computed in a view. Something like

CREATE OR REPLACE VIEW view_name
AS
  SELECT <<list_of_columns>>
         (CASE WHEN sysdate > add_months(start_date, 12)
               THEN 'Y'
               ELSE 'N'
           END) is_member
    FROM your_table

Your code would then simply query the view rather than querying the base table.

终难遇 2025-01-13 20:17:19

这是应用程序执行检查的工作,而不是触发器。 Triiger 不会在 SELECT 上触发。

至于保持该领域的最新状态,有两种可能性。首先设置一项夜间工作,以停用所有会员资格已过期的会员。或者更改您的结构以存储membershipenddate而不是isactive,并在应用程序中使用它来检查成员是否处于活动状态。

This is a job for the application to do the checking not a trigger. Triiger do not fire on SELECT.

As far as keeping the field up-to-date, there are two possibilities. First set up a nightly job to inactivate any members whose meberships have expired. Or change your struture to store membershipenddate rather than isactive and use that in the application check to see if the member is active.

森罗 2025-01-13 20:17:19

您可以将策略添加到表和架构中,然后添加所需的代码。

http://docs.oracle.com/cd/B28359_01 /network.111/b28531/vpd.htm#CIHCAACD

CREATE OR REPLACE FUNCTION auth_orders( 
schema_var IN VARCHAR2,
table_var  IN VARCHAR2)
RETURN VARCHAR2
IS
return_val VARCHAR2 (400);
BEGIN
return_val := 'SALES_REP_ID = 159';
RETURN return_val;
END auth_orders;


BEGIN
DBMS_RLS.ADD_POLICY (
object_schema    => 'oe',
object_name      => 'orders',
policy_name      => 'orders_policy',
function_schema  => 'sys',
policy_function  => 'auth_orders',
statement_types  => 'select, insert, update, delete'
);
END;

You can add a Policy to your table and schema and Add the code that you want.

http://docs.oracle.com/cd/B28359_01/network.111/b28531/vpd.htm#CIHCAACD

CREATE OR REPLACE FUNCTION auth_orders( 
schema_var IN VARCHAR2,
table_var  IN VARCHAR2)
RETURN VARCHAR2
IS
return_val VARCHAR2 (400);
BEGIN
return_val := 'SALES_REP_ID = 159';
RETURN return_val;
END auth_orders;


BEGIN
DBMS_RLS.ADD_POLICY (
object_schema    => 'oe',
object_name      => 'orders',
policy_name      => 'orders_policy',
function_schema  => 'sys',
policy_function  => 'auth_orders',
statement_types  => 'select, insert, update, delete'
);
END;
っ〆星空下的拥抱 2025-01-13 20:17:19

您可以定义每天运行的作业并更新 is_member 字段。

You can define a job which is run everyday and update the is_member field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文