参数不能作为硬编码值

时间:2019-09-27 12:46:17

标签: sql sql-server

只需重写一些SQL代码并测试结果即可。当我使用硬编码过滤器运行查询时,它会运行,但是当我使用变量时,它永远不会运行,或者至少直到我期望使用硬编码过滤器的查询时为止。

有人可以帮帮我或解释什么地方错了。

-- THIS DOESN'T RUN
DECLARE @PortfolioCode INT = 519 
    , @ReportDate DATETIME = '2019-09-19' 

SELECT 
    TCSELL.RemainingQuantityPerc
    , SomeVal1 = TCBUY.SettleAmountTranCcy    * TCSELL.RemainingQuantityPerc
    , SomeVal2 = TCBUY.SettleAmountSettCcy * TCSELL.RemainingQuantityPerc
    , TCSELL.*
FROM     
    dbo._Dolfin_vwTradeCost TCBUY
        INNER JOIN dbo._Dolfin_vwTradeSellRunningQty TCSELL
        ON TCBUY.PortfolioCode = TCSELL.PortfolioCode
        AND TCBUY.TitleCode = TCSELL.TitleCode
        AND TCBUY.DocNo = TCSELL.DocNo
        AND TCSELL.RunningQuantity > 0
WHERE 1 = 1
    AND TCBUY.PortfolioCode = @PortfolioCode
    AND TCSELL.ReportDate = @ReportDate


-- THIS WORKS
SELECT 
    TCSELL.RemainingQuantityPerc
    , SomeVal1 = TCBUY.SettleAmountTranCcy    * TCSELL.RemainingQuantityPerc
    , SomeVal2 = TCBUY.SettleAmountSettCcy * TCSELL.RemainingQuantityPerc
    , TCSELL.*
FROM     
    dbo._Dolfin_vwTradeCost TCBUY
        INNER JOIN dbo._Dolfin_vwTradeSellRunningQty TCSELL
        ON TCBUY.PortfolioCode = TCSELL.PortfolioCode
        AND TCBUY.TitleCode = TCSELL.TitleCode
        AND TCBUY.DocNo = TCSELL.DocNo
        AND TCSELL.RunningQuantity > 0
WHERE 1 = 1
    AND TCBUY.PortfolioCode = 519
    AND TCSELL.ReportDate = '2019-09-19'

1 个答案:

答案 0 :(得分:-1)

请检查表或视图中名为dbo._Dolfin_vwTradeSellRunningQty(具有TCSELL别名)的ReportDate列的数据类型是什么。

如果数据类型是DateTime,则应该可以。如果它是某种字符串(例如Varchar或Char),则还必须将变量@ReportDate更改为该数据类型。