Procedure edit for accumulated points

Hello,
I am trying to edit a procedure to add new points to existing points without resetting it.
I have 2 tables. First table is saving the points dynamically. Then the points in first table are saving to second table. But I want the points to get increased in the second tables.
Example: first table has 5 points. Second tables showing 5 points. (fine)
Now first table is truncated, and then it has 1 point. I want the second table to show 6 points.
In my case it will reset the 5 points and show 1 point which I don't want like that.
I am TSQL beginner and I couldn't find a solution. Hope you guys help me.

USE [RANK_User]

GO
/****** Object: StoredProcedure [dbo].[_BARank] Script Date: 4/13/2020 3:43:07 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/**
version : 1
author : xxx
created date : 2020-13-04
description : battle arena rank manager
**/

ALTER PROCEDURE [dbo].[_BARank]
AS
DECLARE @Counter INT = 1
DECLARE @RankTable TABLE (ID INT IDENTITY(1,1), CharName16 VARCHAR(64), Points INT)

INSERT @RankTable
SELECT Data2, COUNT()
FROM RANK_User.dbo._BridgeCommands
WHERE Executor = '_IncreaseKillCountBA' and Data3 = 'BattleArena'
GROUP BY Data2
ORDER BY COUNT(
) DESC

WHILE @Counter <= (SELECT COUNT(*) FROM @RankTable)
BEGIN
DECLARE @CharName16 VARCHAR(64),
@Points INT,
@CharID INT,
@Guild VARCHAR(64)

/**_# INITIALIZE */
SET @CharName16 = NULL
SET @CharID = NULL
SET @Guild = NULL

SELECT @CharName16 = CharName16,
	   @Points = Points
FROM @RankTable
WHERE ID = @Counter

SELECT @CharID = CharID
FROM KSY_VT_SHARD.dbo._Char
WHERE CharName16 = @CharName16

SELECT @Guild = G.Name
FROM KSY_VT_SHARD.dbo._GuildMember GM
	JOIN KSY_VT_SHARD.dbo._Guild G
		ON GM.GuildID = G.ID
WHERE GM.CharName = @CharName16

IF @Guild IS NULL
BEGIN
	SET @Guild = ''
END

UPDATE RANK_User.dbo._DynamicRank_Custom1
SET CharName16 = @CharName16, --POSIBLE CHAR RENAME
	GuildName = @Guild, --POSIBLE GUILD LEAVE
	Point = @Points,
	LastUpdated = GETDATE()
WHERE CharID = @CharID

IF @@ROWCOUNT = 0
BEGIN
	INSERT RANK_User.dbo._DynamicRank_Custom1 (CharID, CharName16, GuildName, Point, LastUpdated)
	VALUES (@CharID, @CharName16, @Guild, @Points, GETDATE())
END

/**_# FOR LOOP COUNTER*/
SET @Counter += 1

END

If you provide DDL, Sample Data and expected results we can provide a better solution. From what I see, you don't even need the while loop.

point1

First table you see 5x counts for "Name" he has 5 points. This data is saved dynamically from a tool.
Then it is saved for the Log table
Now when the first table is truncated, and the tool will save new points to the first table. I want the new points to get saved on the (already) 5 points. So it gets increased over it but in my case it will change to 1 point and lose the 5 points.
I don't have control on the tool, so the table is being truncated automatically at 12:00AM

Create table #Name (ID int,
CommandID int,
Executor Nvarchar(30),
Data1 int,
Data2 nvarchar(20),
Data3 nvarchar(40),
NameDate datetime2(7))

Create table #Points (
CharName16 varchar(16),
GuildName Nvarchar(30),
Points int,
LastUpdatedDate datetime2(7))

insert into #Points (CharName16, GuildName, Points, LastupdatedDate) values
('Name', 'Guild', 5, '4/14/2020 17:23:21')

truncate table #Name

insert into #Name (ID, CommandID, Executor, Data1, Data2, Data3, NameDate) values
(11,22, 'IKCBA',2,'Name','BattleArena', '4/14/2020 17:23:21'),
(12,22, 'IKCBA',2,'Name','BattleArena', '4/14/2020 17:23:22'),
(13,22, 'IKCBA',2,'Name','BattleArena', '4/14/2020 17:23:23'),
(14,22, 'IKCBA',2,'Name','BattleArena', '4/14/2020 17:23:24'),
(15,22, 'IKCBA',2,'Name','BattleArena', '4/14/2020 17:23:25'),
(16,22, 'IKCBA',2,'Name2','BattleArena', '4/14/2020 17:23:25')

-- I Know the union all can be done in 1 step, but I'm having a brain-fart right now :slight_smile:
Merge into #Points as Target
using(
select n.Data2 as name, count(1) as Points, max(NameDate) as lastUpdatedDate
from #Name n
join #Points p
on n.Data2 = p.CharName16
and n.NameDate > p.LastUpdatedDate
group by n.Data2
union all
select n.Data2 as name, count(1) as Points, max(NameDate)
from #Name n
left join #Points p
on n.Data2 = p.CharName16
where p.CharName16 is null
group by n.Data2) as Source
on Target.CharName16 = Source.name
when matched then
update set Points = Target.points + Source.Points,
LastUpdatedDate = Source.LastUpdatedDate
when not matched then
insert (CharName16, GuildName, Points, LastupdatedDate)
values (source.Name, 'GuildName', source.Points, source.LastupdatedDate);

select * from #Points
1 Like

Hi

Hope this helps :slight_smile:

How about ..a TRIGGER !! on the first table !!!!

anything
inserted into first table
is inserted into second table

2 Likes