A problem in returning a table with Null On it

后端 未结 3 910
Happy的楠姐
Happy的楠姐 2021-01-28 05:53

i want my function to return a A table with some values

I must fix something in the update but i don\'t know what this is the function FngetTableH BELOW



        
相关标签:
3条回答
  • 2021-01-28 06:25

    your query "Select CLOSING_PRICE from Historique H inner..." may be returning more than 1 value.

    0 讨论(0)
  • 2021-01-28 06:29

    In your innermost query I think MAX(@Date) should be MAX(DATE_NEGO), which might explain why the closing price query is returning multiple results.

    There is still the possibility of multiple results, so you might need to account for that You could select max(closing_price) if it doesn't matter which, though I suspect it might, in which case you need to do some ordering to select the correct price to use.

    EDIT: Try this:

    ALTER FUNCTION [dbo].[FnGetTableH]
    (
        @Date DATETIME
    )
    RETURNS @Listeab_ TABLE
    (
        CLOSING_PRICE MONEY,
        IdValeur INT
    )
    AS
    BEGIN
        WITH prices AS
        (
            SELECT IdValeur,
                CLOSING_PRICE,
                ROW_NUMBER() OVER (PARTITION BY IdValuer ORDER BY DATE_NEGO DESC) rn
            FROM Historique
            WHERE DATE_NEGO <= @Date
        )
        INSERT @Listeab_(CLOSING_PRICE, IdValeur)
        SELECT DISTINCT ISNULL(p.CLOSING_PRICE, 0),
            h.IdValeur
        FROM Historique h
        LEFT OUTER JOIN prices p ON h.IdValeur = p.IdValeur
            AND p.rn = 1
            RETURN;
    END;
    
    0 讨论(0)
  • 2021-01-28 06:47

    Use top to select 1 record. May be it will solve your error.

    select Distinct 0 , IdValeur
    From Historique
    Update @Listeab_
    Set CLOSING_PRICE = (Select  Top 1 CLOSING_PRICE
    from Historique H
    inner join (Select MAX(@Date) As DATEDU,IdValeur FROM Historique
    where 
    DATE_NEGO<= @Date 
    group by IdValeur ) L2
    on 
    H.DATE_NEGO=L2.DATEDU and H.IdValeur=L2.IdValeur
    )
    
    0 讨论(0)
提交回复
热议问题