0

トリガーを作成する最善の方法を見つけようとしていますが、SQL Server 2008 を使用してレンガの壁にぶつかりました。すべての電話番号 (番号) を格納する中央テーブルを作成したいと考えています。Numbers には、番号を 1 つ以上の顧客にリンクする子テーブル 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]

数表をチェックして、存在しない場合は数を挿入するトリガーが欲しいです。次に、トリガーの 2 番目の部分をチェックして、顧客にリンクが提示されていない場合は挿入します。次のようにトリガーで数値テーブルを更新できますが、パフォーマンスが気になります。

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

どんな助けでも大歓迎ですか?

ロブ

4

1 に答える 1