This is my trigger:
Create trigger Points
on Posts
after insert, update
As
declare @Id int;
declare @value int;
select @value= Count(i.Message) from Posts i;
select @Id = [PostedBy] from inserted;
update AspNetUsers set User_points = @value * 3
where @Id = @Id
Here, at the last line, where condition always fails.Its not picking correct Id and updating same value in User_Points column in all rows not in particular rows.
I have written an insert statement to check what value i get back like this:
insert into Employee_Demo(PostedBy, TotalCount)
values (@postedby,@value );
here, i am getting correct @postedby value in table. Previosly, i was trying this:
create trigger Points
on Posts
after insert, update
As
declare @value int
declare @postedby int
select @value= Count(Message) from Posts
select @postedby = PostedBy from inserted
update AspNetUsers set User_points = @value * 3
where Id = @postedby
please please someone help me. how to update only single row based on Id. One more thing, PostedBy is not the primary key in post table.It is foreign key to aspnetuser table and it contains id value of user who has posted the message as u can see in below image.
tried this too:
update AspNetUsers set User_points = @value * 3
FROM INSERTED INNER JOIN Posts ON INSERTED.PostedBy = Posts.PostedBy
INNER JOIN AspNetUsers ON AspNetUsers.Id = inserted.PostedBy
当前,您正在where子句中比较相同的Variable,应该在其中将DB字段与Variable进行比较。
Finally got it working with this query dont know how it happens
update AspNetUsers set User_points = @value * 3
where Id = (Select PostedBy from inserted)
and removing the declaration of @Id or @PostedBy. when i declared those variable and then assign value from inserted to those variable and then use that variable value in where then it was not working. Instead, directly getting value from inserted table in where condition works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.