在具有额外常量列的表上插入触发器到新表中?(Insert trigger on a table with extra constant column in to new table?)

编程入门 行业动态 更新时间:2024-10-21 20:29:17
在具有额外常量列的表上插入触发器到新表中?(Insert trigger on a table with extra constant column in to new table?)

我一直在研究一个数据库,它包含两个模式名称作为前端和备份。 在一个表名称中的位置:

front.Details studID SemID GPA 100 1 4 200 2 3

另一个表名是:

backup.DetailsV studID DEPT SemID GPA

表backup.DetailsV中的输出应如下所示:

studID DEPT SemID GPA 100 1 1 4 200 1 2 3 100 2 1 4 200 2 2 3

如何在表格详细信息上创建触发器以使用dept id 1和2插入到表DetailsV中两次?

I've been working on a database which consists of two schemas names as front and backup. Where in one table name:

front.Details studID SemID GPA 100 1 4 200 2 3

Another table name is:

backup.DetailsV studID DEPT SemID GPA

The output in Table backup.DetailsV should look like below:

studID DEPT SemID GPA 100 1 1 4 200 1 2 3 100 2 1 4 200 2 2 3

How can I create trigger on table Details to insert in to table DetailsV twice with dept id 1 and 2?

最满意答案

要继续Damien的想法,如果使用DetailsV表的唯一原因是生成该输出,则可以使用视图轻松完成。 如果它只是读取数据,则存储过程不知道或不关心源是表还是视图。

Select studID, 1 as Dept, SemID, GPA From front.Details UNION ALL Select studID, 2 as Dept, SemID, GPA From front.Details

如果您需要保留数据流经front.Details表,或者如果您需要在报告之前操作该数据,则只需保留备份表。 如果这确实是你想要的,那么触发器查询非常相似,但是不是寻址表,而是使用特殊的[inserted]表来获取新值。

Select studID, 1 as Dept, SemID, GPA From inserted UNION ALL Select studID, 2 as Dept, SemID, GPA From inserted

To continue Damien's thought, if the only reason to have the DetailsV table is to generate that output, you can easily do that with a view. If it is just reading data, the stored procedure doesn't know or care if the source is a table or a view.

Select studID, 1 as Dept, SemID, GPA From front.Details UNION ALL Select studID, 2 as Dept, SemID, GPA From front.Details

You would only keep a backup table if you needed to keep a history of the data flowing through the front.Details table, or if you needed to manipulate that data before reporting it out. If that is really what you want, the trigger query is very similar, but instead of addressing the table, you use the special [inserted] table to get the new values.

Select studID, 1 as Dept, SemID, GPA From inserted UNION ALL Select studID, 2 as Dept, SemID, GPA From inserted

更多推荐

本文发布于:2023-07-31 01:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340528.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:触发器   常量   到新   表上   Insert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!