如何在SQL中为2个表编写触发器 [英] How to write trigger in SQL for 2 tables

查看:376
本文介绍了如何在SQL中为2个表编写触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to Write Trigger to Inserting A Table At the Same time Insert to B Table with Same Fields and Same Values.



我提到以下链接

https ://www.c-sharpcorner.com/UploadFile/63f5c2/triggers-in-sql-server/ [ ^ ]



但是我不能UnderStand Pleas任何人都可以帮助我



我尝试过:



https://www.c-sharpcorner.com/UploadFile/ 63f5c2 / triggers-in-sql-server / [ ^ ]


I refered The Following Link
https://www.c-sharpcorner.com/UploadFile/63f5c2/triggers-in-sql-server/[^]

But I can't UnderStand Pleas Any one can help Me

What I have tried:

https://www.c-sharpcorner.com/UploadFile/63f5c2/triggers-in-sql-server/[^]

推荐答案

根据你能够澄清的内容你的意图(历史跟踪)我建议你检查那里的替代品;例如存储过程或时态表。所有选项都可以做你想要的,他们各自有各自的优点和缺点。所以请阅读可用的内容,考虑一下您的应用程序将来会如何变化,然后选择最适合您需求的产品,如果需要可以在明天更改



时态表基本上可以通过设计做你想要的。您有主表,然后是历史表。生成的历史记录表将包含标准化列以及主表的镜像。链接和记录是自动的

时间表| Microsoft Docs [ ^ ]



存储过程是我用于应用程序中几乎所有CRUD操作的过程。您只需要能够在一个命令中运行多个SQL语句;并且它更加灵活,因为您可以同时处理多组表。它还允许表模式中的差异,您可以从历史记录中删除一些列(如已创建信息)或从主表中删除。
Based on what you were able to clarify about your intentions ( history tracking) I would suggest that you review the alternatives out there; such as Stored Procedures or Temporal Tables. All options can do what you want and they each have their respective Pros and Cons. So read up on what's available, think about how your application will change in the future, and then choose what best will fit your needs and can be changed for tomorrow if needed

Temporal Tables basically do what you want by design. You have the main table and then a history table. The generated History table will contain standardized columns along with a mirror of your main table. Linking and logging is automatic
Temporal Tables | Microsoft Docs[^]

Stored Procedures are what I utilize for almost all CRUD operations from applications. You simply just have the ability to run multiple SQL statements within one command; and it is much more flexible as you would be able to work with several groups of tables simultaneously. It would also allow differences in the table schemas, you may to remove some columns from the history (like the "created" information) or from the main table.


我没有找到该链接上的信息也特别清楚!



这个解释得更好我认为 SQL Server:编写SQL Server中的插入后触发器 [ ^ ]但是这个Codeproject可能更好触发器 - SQL Server [ ^ ]

总是一个好主意还要提供实际文档创建触发器(Transact-SQL)| Microsoft Docs [ ^ ]



如果这还不够,请听CodeProject的文章列表搜索 [ ^ ]



不幸的是,这不是一个教程论坛,你的问题太模糊,无法帮助远远超出这个范围。为什么不试一试并写一些代码?您可以回来提出具体问题,我们会尽力提供帮助。
I didn't find the information at that link particularly clear either!

This one explains it better I think SQL Server: Coding the After Insert Trigger in SQL Server[^] but this Codeproject one is probably better Triggers -- SQL Server[^]
It's always a good idea to have the actual documentation to hand too CREATE TRIGGER (Transact-SQL) | Microsoft Docs[^]

If that's not enough hear is a longer list of articles from CodeProject Search[^]

Unfortunately this is not a tutorial forum and your question is too vague to help much beyond this. Why not give it a go and write some code? You can come back with a specific question and we will try to help.


这篇关于如何在SQL中为2个表编写触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆