我正在尝试找出创建触发器的最佳方法,但我已经使用 sql server 2008 碰壁了我希望创建一个存储所有电话号码(号码)的中央表,这应该包含客户表中的所有客户号码。Numbers 有一个子表 numbercustomer,它将一个数字链接到一个或多个客户。我的表:
CREATE TABLE [Campaign].[Number](
[NumberID] [int] IDENTITY(1,1) NOT NULL,
[Number] [varchar](15) NOT NULL,
[LastUpdated] [timestamp] NOT NULL,
CONSTRAINT [Pk_Number] PRIMARY KEY CLUSTERED
(
[NumberID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Campaign].[NumberCustomer](
[CustomerNumberID] [int] IDENTITY(1,1) NOT NULL,
[NumberID] [int] NOT NULL,
[CustomerID] [int] NOT NULL,
CONSTRAINT [PK_CustomerNumber] PRIMARY KEY CLUSTERED
(
[CustomerNumberID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Crm].[Customer](
[CustomerID] [int] IDENTITY(30000,1) NOT NULL,
[FirstName] [varchar](255) NULL,
[LastName] [varchar](255) NULL,
[MobileNumber] [varchar](11) NULL,
[LandlineNumber] [varchar](11) NULL,
CONSTRAINT [PK__Customer__8CB286B91CF15040] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我希望触发器检查数字表并插入数字(如果不存在)。然后触发器的第二部分将检查并插入如果没有向客户提供链接。我可以使用触发器更新数字表,如下所示,但我担心性能。
CREATE TRIGGER [Crm].[Customer_Number_Updated] ON [Crm].[Customer]
AFTER Update, INSERT
NOT FOR REPLICATION AS
SET NoCount On
DECLARE
@AuditTime DATETIME,
@IsDirty BIT
SET @AuditTime = GetDate()
SET @IsDirty = 0
Begin Try
INSERT INTO
[Campaign].[Number]([Number])
select
number
from
(
Select
i.MobileNumber as number
From
inserted i
join
deleted d
on
i.CustomerID = d.CustomerID
and
isnull(i.MobileNumber ,'') <> isnull(d.MobileNumber,'')
UNION
Select
i.LandlineNumber as number
From
inserted i
join
deleted d
on
i.CustomerID = d.CustomerID
and
isnull(i.LandlineNumber ,'') <> isnull(d.LandlineNumber,'')
UNION
Select
i.AlternateContactNumber as number
From
inserted i
join
deleted d
on
i.CustomerID = d.CustomerID
and
isnull(i.AlternateContactNumber ,'') <> isnull(d.AlternateContactNumber,'')
) as nums
WHERE NOT EXISTS
(
SELECT
*
FROM
[Campaign].[Number] cn
WHERE
cn.Number = nums.number
)
End try
Begin Catch
IF ERROR_NUMBER() <> 2627
DECLARE @ErrorMessage NVARCHAR(4000), @ErrorSeverity INT, @ErrorState INT;
SET @ErrorMessage = ERROR_MESSAGE();
SET @ErrorSeverity = ERROR_SEVERITY();
SET @ErrorState = ERROR_STATE();
RAISERROR(@ErrorMessage,@ErrorSeverity,@ErrorState) with log;
End Catch
GO
任何帮助将非常感激?
抢