SQL/Oracle 10g - 触发器/从表中获取值的问题

发布于 2024-12-12 04:22:15 字数 704 浏览 0 评论 0原文

我目前正在编写的触发器遇到问题。我想做一个简单的触发器,其中在状态字段设置为“已发送”的情况下更新表 STATEMENT 后,它将在表 NOTICE 中创建一个新行,其中包含 id、日期、用户等字段,最后一个字段是消息,它采用某些字段值来创建“通知”。

如果有帮助的话,我的语句表包含以下字段:

  • id
  • 列表项
  • 标题
  • 其他不需要知道

因此,随着要插入的通知的最后一个字段,我想创建一条消息,也许会说“语句,( id) - (标题),于(日期)发布,已发送。”

我目前拥有:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
begin
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001', 'the notice 
    im having trouble constructing');
end send_notice;

我已经在数据库中测试了这个触发器,一切似乎都工作正常。我想知道的另一件事是格式或是否缺少任何内容可能有助于此触发?另外,我想创建一个通知,它从 STATMENT 中获取字段值吗?

任何帮助表示赞赏

I'm currently having an issue with a trigger I'm writing. I want to do a simple trigger in which after an update to table STATEMENT with the status field set to 'Sent', it would create a new row in the table NOTICE with fields such as id, date, user and the last field being a message which takes certain field values to create a "notice".

If it will help, my STATEMENT table contains the following fields:

  • id
  • List item
  • Title
  • Others not needed to know

So, with the last field of the NOTICE to be inserted, I want to create like a message, perhaps saying "The statement, (id) - (title), issued on (date) has been sent."

I currently have at the moment:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
begin
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001', 'the notice 
    im having trouble constructing');
end send_notice;

I have tested this trigger in a database and everything seems to work fine. Another thing I was just wondering is if the formatting or if there is anything missing that might help with this trigger? And also, I would I go about creating that notice, which takes field values from STATEMENT?

Any help is appreciated

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

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

发布评论

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

评论(1

带上头具痛哭 2024-12-19 04:22:15

您可以使用 :new. 在触发器中引用新的 STATEMENT 列值,并将它们连接到您的文本中:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
begin
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001',
     'The statement, ' || :new.id || ' - ' || :new.title || ', issued on '
     || :new.issue_date || ' has been sent');
end send_notice;

有时连接大量文本和值可能会造成混乱,您可能会发现使用这种“模板”方法更容易:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
declare
    l_text varchar2(500);
begin
    l_text := 'The statement, #ID# - #TITLE#, issued on #DATE# has been sent';
    l_text := replace (l_text, '#ID#', :new.id);
    l_text := replace (l_text, '#TITLE#', :new.title);
    l_text := replace (l_text, '#DATE#', :new.issue_date);
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001', l_text);
end send_notice;

You can refer to new STATEMENT column values in the trigger using :new., and concatenate them into your text:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
begin
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001',
     'The statement, ' || :new.id || ' - ' || :new.title || ', issued on '
     || :new.issue_date || ' has been sent');
end send_notice;

Sometimes concatenating a lot of text and values can get confusing, and you may find it easier to use this "template" approach:

create trigger send_notice
    after update on STATEMENT
    for each row
    when (new.status = 'Sent')
declare
    l_text varchar2(500);
begin
    l_text := 'The statement, #ID# - #TITLE#, issued on #DATE# has been sent';
    l_text := replace (l_text, '#ID#', :new.id);
    l_text := replace (l_text, '#TITLE#', :new.title);
    l_text := replace (l_text, '#DATE#', :new.issue_date);
    insert into NOTICE values (notice_seq.nextval, SYSDATE, '10001', l_text);
end send_notice;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文