简体   繁体   中英

Why is SQL server inserting 0 values into my table instead of the correct values using function

Hope somebody can help me with this as I'm completely out of ideas as to why it's happening.

I am currently conducting some analysis on Premier League Match Results and as part of this, I have created a Multi-Statement Table UDF.

This function accepts a HomeTeam, AwayTeam and a MatchDate parameter, and then performs a count of each match result that was won, drawn or lost historically between the home and away team prior to the up match date specified.

This function works fine by manually calling it, and returns values such as

 Home   Away    Draw
   0      8       4

I wanted to add this information to every match result in my match table, so created a query to move the matches from a staging table, using OUTER APPLY with the function to insert these values.(I also OUTER APPLY another function prior to this which works fine.) and then insert into my MatchData table.

The query works if I just select the values, but if I INSERT INTO my MatchData table, the values are all populating as 0s.

I have tried numerous tests, which confirm that this also happens if I use a SELECT into, unless the table is temporary.

To add, there is no conversion of the values in question at any point, they remain as integers all the way through and the destination column is of type int also.

Hope somebody can give me some ideas on what to try next. Code is below. apologies for anything that isn't well written, as I have muddled with the code a lot up to now trying to get it to insert the right value

Thanks in advance!

Here's the stored procedure:

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 

Here's the 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

This is only potentially an answer, but... why is your Function query so complex? This does the same thing in only one SELECT statement:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM