[英]Stored procedure to update temp table based on Date in SQL Server
I have two tables : 我有两个表:
Teams [Id],[name],[nomatches],[owngoals],[othergoals],[points]
and 和
Matches[id],[homeid],[outid],[homegoal],[outgoal],[matchdate]
I have a trigger which fires on INSERT,UPDATE,DELETE
so the Teams
table current score table is always updated. 我有一个触发器,它会在
INSERT,UPDATE,DELETE
上触发INSERT,UPDATE,DELETE
因此Teams
表的当前得分表始终会更新。
Example : 范例:
Select * from teams;
Result : 结果:
Name NumberOfMatches OwnGoals OtherGoals Points
-------------------------------------------------------
FC Chelsea 33 61 22 68
FC Barcelona 33 46 34 59
FC Man UD 33 57 50 52
The problem: 问题:
Table Matches
has a column matchdate
. 表格
Matches
具有列matchdate
。 I want to recalculate the current score table (with my trigger maybe) for all games played before the entered date. 我想重新计算输入日期之前玩过的所有游戏的当前得分表(也许有触发器)。
I don't know how to create a temp table to store the re-calculated data (nomaches, owngoals, othergoals, points for each team) based on the Date
parameter. 我不知道如何创建一个临时表来存储基于
Date
参数重新计算的数据(每个团队的名次,自有进球,其他进球,得分)。
What I have so far : 我到目前为止所拥有的:
CREATE PROCEDURE check_scoretable
(
@pDate DATE = NULL
)
as
DECLARE @Date DATE = COALESCE(@pDate,GETDATE())
declare @homeid char(3);
declare @outid char(3);
declare @id int;
SELECT * INTO #temp_table2 FROM teams;
SET NOCOUNT ON; -- copy of the teams table
declare cur CURSOR LOCAL for
select homeid, outid
from matches where matches.matchdate < @Date
open cur
fetch next from cur into @homeid, @outid
while @@FETCH_STATUS = 0 BEGIN
select @homeid;
select @outid;
--Increment number of matches
update #temp_table2 set #temp_table2.nomatches = #temp_table2.nomatches+1 where #temp_table2.id = @homeid;
update #temp_table2 set #temp_table2.nomatches = #temp_table2.nomatches+1 where #temp_table2.id = @outid;
fetch next from cur into @homeid, @outid
END
close cur
deallocate cur
-- Test the stored procedure
DECLARE @d DATETIME
SELECT @d = GETDATE()
EXEC check_scoretable @date = @d
You can write a stored procedure, like you've done and pass the date to it. 您可以像完成操作一样编写存储过程,并将日期传递给它。
CREATE PROCEDURE check_scoretable
(
@pDate DATE = NULL
)
as
However, rather than a cursor, do something like 但是,不要像游标那样做
SELECT tm.name,sum(tm.noMatches) as NumberMatches,sum(tm.ownGoals) as OwnGoals,
sum(tm.otherGoals) as Othergoals,sum(tm.Points) as Points
FROM Team tm
JOIN Matches mc on mc.homeId=tm.id or mc.outId=tm.id
WHERE mc.matchDate <= @pDate
This will give you the results you are looking for. 这将为您提供所需的结果。
CAVEAT: Your database design is not good, because of the redundant data in it. CAVEAT:您的数据库设计不好,因为其中包含冗余数据。 For example, you are tracking the number of matches in the team table, when you can compute the number of matches by
例如,当您可以通过以下方式计算比赛数时,您正在跟踪团队表中的比赛数
SELECT count(*) FROM matches WHERE homeId=@id or OutId=@id
Same type of operation for total goals, etc. 总体目标的同类操作,等等。
The problem you might run into is, if for some reason, the team record is not updated, the number of matches listed in team could be different than the number of matches from totaling up the matches played. 您可能会遇到的问题是,如果由于某种原因而没有更新团队记录,则团队中列出的比赛数可能不同于将比赛总数相加的比赛数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.