2个数据库之间的SQL Server触发器 [英] Sql Server Trigger between 2 databases

查看:134
本文介绍了2个数据库之间的SQL Server触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据库。一个名为Test的表具有一个名为Vehicles的表。另一个名为Test2的表具有一个名为Clients的表。

I have 2 databases. One, named Test, has a table named Vehicles. Another, named Test2 has a table named Clients.

在Test中的Vehicles表上插入新记录时,我需要更新Test2 Clients表上的NumVehicles字段。

When I insert a new record on the Vehicles table in Test, I need to update the NumVehicles field on the Clients table in Test2.

是否可以使用触发器?

推荐答案

您需要类似

USE Test;
GO
CREATE TRIGGER afterVehicleInsert ON Vehicles AFTER INSERT
AS
BEGIN 
  IF @@rowcount = 0 RETURN;

  UPDATE Test2.[schema_name(default schema is dbo)].Clients 
  SET NumVehicles = NumVehicles +1 -- or whatever it should be
  FROM Test2.[schema_name(default schema is dbo)].Clients c
  INNER JOIN inserted i ON ([your join condition])
END;  
GO

更新当前表和另一个数据库之间的唯一区别是您需要使用[db_name]引用远程表。[schema_name]。[table_name]

The only difference between updating the table in current and another db is that you need to refer a "remote" table using [db_name].[schema_name].[table_name]

这篇关于2个数据库之间的SQL Server触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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