0

我刚被扔进一个使用 SQL 的项目。这个存储过程使用表。我正在尝试转换它,使其不使用表。但我对 SQL 太陌生了,我有麻烦..有人可以帮助或指导我吗?或者这样的事情是不可能的?

我正在尝试以这种方式转换它,以便我可以在 SQL 中使用 LINQ/Entity Framework,因为当我将此存储过程拖入 EDML 时它不起作用,因为它正在使用表

    ALTER PROCEDURE [dbo].[GetGame_Passer]
    -- Add the parameters for the stored procedure here
    @GameDate varchar(8),
    @TricodeHome varchar(3)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    CREATE TABLE #Passer(
        GameKey int,
        PlayerName varchar(50),
        Tricode varchar(3),
        StatString varchar(50),
        Position varchar(20),
        FantasyScore int
    )

    DECLARE @gameKey int
    SET @gameKey = (SELECT GameKey FROM Games WHERE GameDate=@GameDate
        AND TricodeHome=@TricodeHome)

    INSERT #Passer
    SELECT TOP 1 p.GameKey, p.PlayerName, p.Tricode, p.StatString, p.Position,
        (p.Yards/25 + p.Touchdowns * 6 - p.Interceptions * 2) AS FantasyScore
    FROM GamePassers AS p
    WHERE p.GameKey=@gameKey
    ORDER BY FantasyScore DESC, p.Yards DESC    

    SELECT g.*, p.PlayerName AS PasserName, p.Tricode AS PasserTricode, 
        p.StatString AS PasserStat, p.Position AS PasserPosition
    FROM Games AS g LEFT OUTER JOIN #Passer AS p ON
        g.GameKey=p.GameKey
    WHERE g.GameDate=@GameDate AND
        g.TricodeHome=@TricodeHome

    SELECT TOP 2 p.GameKey, p.PlayerName, p.Tricode, p.StatString, p.Position
    FROM GamePassers AS p
    WHERE p.GameKey=@gameKey
    ORDER BY p.Yards DESC   

    DROP TABLE #Passer
END
4

1 回答 1

2

任何 SQL 语句都可以变成子查询而不是表。在您提到#passer 的地方,您可以只参考用于将行插入#passer 的sql 语句。

这个说法:

SELECT g.*, p.PlayerName AS PasserName, p.Tricode AS PasserTricode, 
    p.StatString AS PasserStat, p.Position AS PasserPosition
FROM Games AS g LEFT OUTER JOIN #Passer AS p ON
    g.GameKey=p.GameKey
WHERE g.GameDate=@GameDate AND
    g.TricodeHome=@TricodeHome

将#passer 替换为用于填充#passer 的sql 代码。

SELECT g.*, p.PlayerName AS PasserName, p.Tricode AS PasserTricode, 
    p.StatString AS PasserStat, p.Postion AS PasserPosition
FROM Games AS g LEFT OUTER JOIN    
        ( SELECT TOP 1 p.GameKey, p.PlayerName, p.Tricode, p.StatString, p.Position,
        (p.Yards/25 + p.Touchdowns * 6 - p.Interceptions * 2) AS FantasyScore
        FROM GamePassers AS p
        WHERE p.GameKey=@gameKey)  AS p ON
         g.GameKey=p.GameKey
        WHERE g.GameDate=@GameDate AND
        g.TricodeHome=@TricodeHome

希望我正确理解了您的问题。

于 2013-09-30T17:28:50.970 回答