为什么SQL服务器使用函数将0值插入到我的表中而不是正确的值

时间:2017-06-29 21:55:35

标签: sql sql-server stored-procedures insert udf

希望有人可以帮助我,因为我完全不知道为什么会这样。

我目前正在对英超比赛结果进行一些分析,作为其中的一部分,我创建了一个多语句表UDF。

此功能接受HomeTeam,AwayTeam和MatchDate参数,然后执行在指定的比赛日期之前在主队和客队之间历史赢得,抽签或丢失的每个比赛结果的计数。

通过手动调用此函数可以正常工作,并返回

等值
 Home   Away    Draw
   0      8       4

我想将此信息添加到匹配表中的每个匹配结果中,因此创建了一个查询以从临时表中移动匹配,使用OUTER APPLY并使用函数插入这些值。(我还在OUTER APPLY另一个函数优先这个工作正常。)然后插入我的MatchData表。

如果我只是选择值,则查询有效,但如果我INSERT INTO我的MatchData表,则值都填充为0。

我尝试过多次测试,确认如果我使用SELECT,也会发生这种情况,除非该表是临时的。

要添加,在任何时候都没有相关值的转换,它们一直保持为整数,目标列也是int类型。

希望有人能给我一些关于下一步尝试的想法。代码如下。对任何写得不好的事情表示歉意,因为我已经对代码混淆了很多,现在试图让它插入正确的值

提前致谢!

这是存储过程:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [Load].[MoveToMatchData]
AS 
BEGIN
    INSERT INTO Football.MatchData.PremierLeague
        SELECT * 
        FROM
            (SELECT 
                 [League], [MatchID], [Season], 
                 [MatchDate], [HomeTeam], [AwayTeam],
                 [FTHomeGoals], [FTAwayGoals], [FTResult],
                 [HTHomeGoals], [HTAwayGoals], [HTResult],
                 [Referee], [HomeShots], [AwayShots],
                 [HomeShotsOnTarget], [AwayShotsOnTarget],
                 [HomeFouls], [AwayFouls], [HomeCorners], [AwayCorners],
                 [HomeYellows], [AwayYellows], [HomeReds], [AwayReds]
             FROM 
                 [Football].[Load].[Staging_MatchData] AS a
             WHERE 
                 League = 'E0') AS a

  OUTER APPLY

  (
  SELECT * FROM Football.Load.CreateRelativeTable_Prem
  (a.MatchDate, a.HomeTeam, a.AwayTeam, a.Season, A.League)
  ) as b

  OUTER APPLY 

这是UDF

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION [Load].[GetH2HRecords]
     (@HomeTeam varchar(50), @AwayTeam varchar(50), @MatchDate date)
RETURNS @H2H TABLE
(
Home int,
Away int,
Draw int
)
AS 

BEGIN

DECLARE @FromDate date
SET @FromDate = DATEADD(yyyy,-10,@MatchDate)
INSERT INTO @H2H

SELECT
a.[Number of Matches] as HomeHTH, b.[Number of Matches] as AwayHTH, c.[Number of Matches] as DrawHTH
FROM
    (
    SELECT 
    COUNT(MatchID) as [Number of Matchesh]
    FROM MatchData.PremierLeague
    WHERE HomeTeam = @HomeTeam 
    AND AwayTeam = @AwayTeam
    AND MatchDate > @FromDate
    AND FTResult = 'H'
    ) as a

    OUTER APPLY

    (
    SELECT 
    COUNT(MatchID) as [Number of Matchesa]
    FROM MatchData.PremierLeague
    WHERE HomeTeam = @HomeTeam
    AND AwayTeam = @AwayTeam
    AND MatchDate > @FromDate
    AND FTResult = 'A'
    ) as b

    OUTER APPLY

    (
    SELECT 
    COUNT(MatchID) as [Number of Matchesd]
    FROM MatchData.PremierLeague
    WHERE HomeTeam = @HomeTeam
    AND AwayTeam = @AwayTeam
    AND MatchDate > @FromDate
    AND FTResult = 'D'
    ) as c

RETURN

END


  (
  SELECT * FROM Football.Load.GetH2HRecords
  (a.HomeTeam, a.AwayTeam, a.MatchDate)
  ) as c

  END

1 个答案:

答案 0 :(得分:0)

这只是一个答案,但是......为什么你的Function查询如此复杂?这只在一个SELECT语句中执行相同的操作:

CREATE FUNCTION [Load].[GetH2HRecords]
     (@HomeTeam varchar(50), @AwayTeam varchar(50), @MatchDate date)
RETURNS @H2H TABLE
(
Home int,
Away int,
Draw int
)
AS 

BEGIN

DECLARE @FromDate date
SET @FromDate = DATEADD(yyyy,-10,@MatchDate)
INSERT INTO @H2H
SELECT 
    SUM(
        CASE
            WHEN FTResult = 'H' 
                Then 1
            ELSE 0
        END 
        ) as HomeHTH,
    SUM(
        CASE
            WHEN FTResult = 'A' 
                Then 1
            ELSE 0
        END 
        ) as AwayHTH,
    SUM(
        CASE
            WHEN FTResult = 'D' 
                Then 1
            ELSE 0
        END 
        ) as DrawHTH
FROM MatchData.PremierLeague
WHERE HomeTeam = @HomeTeam 
AND AwayTeam = @AwayTeam
AND MatchDate > @FromDate

RETURN
END