在触发器内部我想知道触发器触发的表的名称:是否可以通过T-SQL以编程方式获取它?
答案 0 :(得分:5)
您可以使用@@ procid执行此操作,例如
USE tempdb
GO
CREATE TABLE dbo.triggerTest ( rowId INT IDENTITY PRIMARY KEY, someData VARCHAR(50) DEFAULT NEWID() )
GO
CREATE TRIGGER dbo.itrg_triggerTest ON dbo.triggerTest
FOR INSERT
AS
--SELECT @@PROCID, OBJECT_NAME( @@PROCID )
SELECT OBJECT_SCHEMA_NAME( parent_id ) + '.' + OBJECT_NAME( parent_id ) tableName FROM sys.triggers WHERE object_id = @@PROCID
GO
INSERT INTO dbo.triggerTest DEFAULT VALUES
GO
DROP TABLE dbo.triggerTest
GO