从触发器

时间:2016-11-16 12:48:32

标签: sql triggers

如何获取Other表的值以将数据插入表中 这是我想要从其他表中获取StoreId和LangnageId的代码

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[NewsTrigger]'))
DROP TRIGGER [dbo].[NewsTrigger]
GO
CREATE TRIGGER [dbo].[NewsTrigger]
       ON [dbo].[News]
FOR INSERT, UPDATE, DELETE
AS
BEGIN
       SET NOCOUNT ON;


   DECLARE @EntityId INT
   DECLARE @EntityName varchar
   DECLARE @IsPublish bit
   DECLARE @LanguageId int
   DECLARE @StoreId int
   DECLARE @InTime Datetime   

   SELECT @EntityId = DELETED.Id FROM DELETED    
   Delete from MyCustomtbl where EntityName='News' and EntityId  = @EntityId

   SELECT @EntityId=(Id) FROM INSERTED
   SELECT @IsPublish=(Published) FROM INSERTED
   SELECT @LanguageId=(LanguageId) FROM INSERTED

   INSERT INTO MyCustomtbl(EntityId,EntityName,IsPublish,Status,LanguageId,StoreId,InTime)
                                Values(@EntityId,'News',@IsPublish,1,@LanguageId,@StoreId,GetDate())

END  我想从触发器中的语言表中存储来自Store Table和LangaugeId

1 个答案:

答案 0 :(得分:0)

您可以在声明变量的Trigger aft中设置商店和语言ID。例如:

DECLARE @StoreId INT

SET @StoreId = (Select StoreId from StoreIds as a where a.storeid = inserted.storeid);

如果我正确地解释了你的问题,那应该做好工作。