Dear @Priya i think you want to use same trigger for more then one table but one trigger can be implemented on a single table means you can use this same trigger for more then table like
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 2:15 AM EDT
0
Here is my Trigger for UserGroupMapping When I insert the values into UserId and GroupId it's automatically inserting into UserScores also(Which has same columns in UserScores)..So I need same action for update also
ALTER TRIGGER [dbo].[Trig_UserScore] ON [dbo].[UserGroupMapping]
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 3:11 AM EDT
0
Noo Its not working...I can able to update the UserGroupMapping columns but at the same time it's not updating in UserScores table ..That means Trigger is not working
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 2:42 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 1:20 AM EDT
Types of DML Triggers
Triggers are mainly three types
1. After Triggers
2. Instead Of Triggers
Create Trigger After_Update On Student
AFTER Update
AS
Declare @IId int ;
Declare @Name Nvarchar(50) ;
Declare @Age Int;
Declare @Class int;
Declare @IId1 int ;
Declare @Name1 Nvarchar(50) ;
Declare @Age1 Int;
Declare @Class1 int;
Select @IId= Tab.IId From Deleted Tab;
Select @Name= Tab.Name From Deleted Tab;
Select @Age= Tab.Age From Deleted Tab;
Select @Class= Tab.Class From Deleted Tab;
Select @IId1= Tab.IId From inserted Tab;
Select @Name1= Tab.Name From inserted Tab;
Select @Age1= Tab.Age From inserted Tab;
Select @Class1= Tab.Class From inserted Tab;
Set @IId1=@IId1+1;
if @IId>10
begin
Delete From Student Where IId=@iid
Insert Into Student(IId,Name , Age ,Class) Values (@IId1,@Name1,@Age1,@Class1)
Print 'Data Updated Successfully'
End
else
Begin
Print 'Data not Updated Successfully'
End
Create Trigger Insted_Update On Student
Instead Of Update
as
begin
Declare @IId int ;
Declare @Name Nvarchar(50) ;
Declare @Age Int;
Declare @Class int;
Select @IId= Tab.IId From Deleted Tab;
Select @Name= Tab.Name From Deleted Tab;
Select @Age= Tab.Age From Deleted Tab;
Select @Class= Tab.Class From Deleted Tab;
Delete From Student Where IId=@IId
Set @IId=@IId+1;
Insert Into Student(IId,Name , Age ,Class) Values (@IId,@Name,@Age,@Class)
Print 'Data Updated Successfully'
End
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 1:25 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 1:34 AM EDT
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 1:44 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 2:06 AM EDT
Create Trigger After_Update On Student , Stdeunt1 ,Student3
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 2:15 AM EDT
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 3:11 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 3:24 AM EDT
GO
/****** Object: Trigger [dbo].[Trig_UserScore] Script Date: 6/2/2015 4:49:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create TRIGGER [dbo].[Trig_UserScore] ON [dbo].[UserGroupMapping]
FOR Update
AS
begin
Declare @Userid nvarchar(Max);
Declare @Groupid nvarchar(Max);
Select @UserId= tab.Userid From Inserted Tab;
Select @Groupid = tab.Groupid From Inserted Tab;
Update Userscores Set GroupId=@GroupId Where UserId=@UserId;
end
GO
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 3:26 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 3:27 AM EDT
priya AvanigaddaRank #7,836 · 233 reputationPosted Jun 2, 2015, 4:08 AM EDT
Pankaj Kumar ChoudharyRank #80 · 24.4K reputationPosted Jun 2, 2015, 4:41 AM EDT
Select @UserId= Inserted.Userid from Inserted
Select @UserId = tab.@UserId From Inserted Tab;
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts