当我使用特定状态之外的区域代码时创建触发器

时间:2017-12-04 17:30:46

标签: sql sql-server

我正在创建一个触发器,让我知道当我在CA之外输入电话号码时。这是我到目前为止所做的,但我认为无论区号/状态如何,每次输入电话号码时都会产生触发信息。

不确定我是否需要在我的客户电话表中指定更多,如果是这样,我有什么遗漏?

这是我到目前为止的代码......

Create Trigger NumberOutsideCA
On CustomerPhone
After Insert, Update as
    Print 'Phone Number is Outside CA'
Return

1 个答案:

答案 0 :(得分:2)

你可以使用魔术表"插入"检查电话号码是否在CA之外并且可以将该记录记录在某处。

CREATE TRIGGER NumberOutsideCA ON CustomerPhone
AFTER INSERT, UPDATE
AS
BEGIN
    -- Here i have user CustomerPhoneLog table to log the record. You can make your own table
    INSERT INTO CustomerPhoneLog (CustomerPhoneID,Message)
    SELECT CustomerPhoneID
        , 'This phone number doest not belongs to CA State'
    FROM INSERTED
    WHERE STATE != 'CA' -- Here you have to user proper condition to check the phone number outside CA
END