简体   繁体   English

存储过程以基于SQL Server中的日期更新临时表

[英]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.

相关问题 SQL Server 2012使用临时表的存储过程 - SQL Server 2012 using temp table of stored procedure SQL Server存储过程将多个变量输入到临时表中 - SQL Server stored procedure input multiple variables into a temp table SQL Server - 使用临时表优化存储过程 - SQL Server - optimise the stored procedure by using temp table 如何删除SQL Server存储过程中使用的@Temp表 - How to drop the @Temp table used in SQL Server stored procedure SQL Server 2008中用于存储过程的临时表,表变量,全局临时表之间的差异 - Difference between temp table, table variable, global temp table to use in stored procedure in SQL Server 2008 SQL-将存储过程结果插入到临时表中 - SQL - Insert Stored Procedure Results into Temp Table 根据存储过程中传递到临时表的月数将日期拆分为月和年 - Split date into month and year based on number of months passed in stored procedure into a temp table SQL Server导入和导出向导在临时表的存储过程中提供无效对象错误 - SQL Server Import and Export wizard gives invalid object error on stored procedure with temp table 如何在不知道SQL Server中架构的情况下从存储过程中填充临时表 - How to fill a temp table from stored procedure without knowing it's schema in SQL Server SQL Server存储过程使用临时表字段作为select的where子句 - SQL Server stored procedure using a temp table field as the where clause for a select
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM