2012-02-21 61 views
0

我有一個觸發器,我想從存儲過程中將其啓動。我使用MS訪問,當我從MS訪問運行觸發它給我一個錯誤消息(ODBC)。我想我不能使用ms訪問創建觸發器。這是我的觸發器:使用存儲過程創建觸發器

IF EXISTS 
(SELECT name 
FROM sys.objects 
WHERE name = 'UpdateComments' AND type = 'TR') 
DROP TRIGGER tblEmailHdr_abenit01.UpdateComments; 
GO 
CREATE TRIGGER UpdateComments 
ON tblEmailHdr_abenit01 
AFTER Update 
AS 
IF (UPDATE (Comments)) BEGIN Update ttblEmailHdr_abenit01 
Set UpdateComm = GetDate() 
END; 
GO 

這是我怎麼一直在試圖創建存儲過程的觸發器,但我得到以下錯誤味精的,當我嘗試創建存儲過程:

存儲過程:

CREATE PROCEDURE dbo.SP_AS_tblEmailHdr_Trig (@UserID as varchar(10)) 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
--SET NOCOUNT ON; 

-- Insert statements for procedure here 
Declare @UserTable Varchar(50) 
Declare @UserTable2 Varchar(50) 

Set @UserTable = 'tblEmailHdr_' + @UserID ; 
Set @UserTable2 = 'tblEmailHdr_' + @UserID + '.UpdateComments' ; 

IF EXISTS 
(SELECT name 
FROM sys.objects 
WHERE name = 'UpdateComments' AND type = 'TR') DROP TRIGGER @UserTable2 

GO 
CREATE TRIGGER UpdateComments 
ON @UserTable 
AFTER UPDATE 
AS 
IF (UPDATE (Comments)) 
BEGIN 
    --RAISERROR (50009, 16, 10) 
    Update @UserTable 
    Set UpdatedComm = GetDate() 
END 
GO 

END 

GO 

錯誤味精,我得到:

Msg 102, Level 15, State 1, Procedure SP_AS_tblEmailHdr_Trig, Line 23 
Incorrect syntax near '@UserTable2'. 
Msg 102, Level 15, State 1, Procedure UpdateComments, Line 2 
Incorrect syntax near '@UserTable'. 
Msg 1087, Level 15, State 2, Procedure UpdateComments, Line 8 
Must declare the table variable "@UserTable". 
Msg 102, Level 15, State 1, Line 2 
Incorrect syntax near 'END'. 
+0

從SP – Vikram 2012-02-21 01:34:21

+0

@Vikram內取出GO' GO'可能會阻止程序被創建,但它仍然不會導致觸發器被執行,因爲OP似乎需要。 – Yuck 2012-02-21 01:35:28

+0

@Yuck:你說得對。觸發器會自動執行..但我認爲他正在嘗試創建一個。 – Vikram 2012-02-21 01:39:29

回答

0

從insid刪除所有GO語句e程序。

0

創建過程親(參數) 作爲 開始 申報@trigs爲nvarchar(最大) DECLARE @trip爲nvarchar(最大) 組@的Trig =」 創建dbo.employee 觸發三嵌件 作爲 SELECT * FROM插入 去」 組@跳閘= '降觸發三'

EXEC sp_executesql的@trigs 插入到僱員值(參數) EXEC sp_executesql的@trip 端

高管親PARAM 如: EXEC親80, 'AAA', 'AAS',25000 '2013年2月1日', '它'