This trigger updates points based on kills ( K1 )
It fires when K1 is updated in the chars table. I do 5 pts for each kill.
Code:
USE [PS_GameData]
GO
/****** Object: Trigger [dbo].[PointsForKills] Script Date: 09/27/2012 11:57:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create trigger [dbo].[PointsForKills]
on [PS_GameData].[dbo].[Chars]
after update
as
IF (UPDATE (k1))
begin
declare @oldk1 int
declare @newk1 int
set @oldk1 = (select k1 from deleted)
set @newk1 = (select k1 from inserted)
if @oldk1<@newk1
begin
update PS_userdata.dbo.users_master
set point=point+5
from PS_userdata.dbo.users_master m,inserted i where m.userUID=i.useruid
end
/*if I wanted to do 500 points per x000 kills I would do something like this
if (convert ( varchar (5),@newk1) like '%000')
begin
update PS_userdata.dbo.users_master
set point=point+500
from PS_userdata.dbo.users_master m,inserted i where m.userUID=i.useruid
end
This would give 500 points to every k1 value that is "like" 1000 or 2000 or 3000 etc, not just 1000 kills
*/
end
end
GO
Pseudo code of what the trigger is doing:
1. Trigger waits for K1 to be updated
2. If k1 gets updated, go compare the old value ( deleted ) with the new value ( inserted ) that was just added.
3. If old value is less than new value, go update users_master points, where userid of chars table matches userid of Users_master ( this way you don't update everyone's points lol )
Appendix:
A. There is a "from" statement because we are comparing 2 tables and you have to reference both inside. There are many ways of doing this ( joins, full object names etc ) but this is how i did it.
B. /* this begins a comment block in SQL
C. */ this ends a comment block in SQL
D. Understand what you are doing before doing it
E. Added points take a little bit to show on the client side, but a relog is NOT necessary.