执行时where子句参数错误sp_executesql
Param error in where clause when executing sp_executesql
我搜索了答案,但没有找到任何答案。如何解决这个错误?
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@SDP".
这是我的代码:
declare @SQL NVARCHAR(4000),
@dfilter NVARCHAR(100)
@SDP DateTime
set @dfilter =
(case when @SDP is null
then '@SDP BETWEEN CAST(sp.StartDate AS DATE) AND CAST(sp.EndDate AS DATE) '
else '@SDP = CAST(sp.StartDate AS DATE) '
end)
set @SQL = N'UPDATE p SET PriceWithVAT = promo.Price, IsPromo=1
FROM #preturi p
JOIN
(SELECT pp.ItemId, Price
FROM SalesPromotion sp (NOLOCK)
JOIN SalesPromotionXSite sps (NOLOCK) ON sp.SalesPromotionId = sps.SalesPromotionId
JOIN SalesPromotionDetail spd (NOLOCK) ON sps.SalesPromotionId = spd.SalesPromotionId
JOIN #preturi pp on pp.ItemId = spd.ItemId
WHERE sps.SiteId = '+quotename(@SiteId)+
' AND spd.IsActive = 1 AND ' + @dfilter+
') promo ON p.ItemId = promo.ItemId'
BEGIN
EXEC sp_executesql @SQL
END
首先,更正语法(在DECLARE
语句中缺少',')并声明@SiteId
。
之后,请注意,您在 T-SQL 语句中使用了 @SDP 变量,因此您必须将此变量发送到 sp_executesql
:
...
EXEC sp_executesql @SQL, N'@SDP DateTime', @SDP
谢谢 Zhorow。
在这个答案中,我替换了
WHERE sps.SiteId = '+quotename(@SiteId)+
和
WHERE sps.SiteId = @SiteId'
我运行
EXEC sp_executesql @SQL, N'@StartDataPret DateTime, @SiteId int', @StartDataPret, @SiteId
效果很好
我搜索了答案,但没有找到任何答案。如何解决这个错误?
Msg 137, Level 15, State 2, Line 10
Must declare the scalar variable "@SDP".
这是我的代码:
declare @SQL NVARCHAR(4000),
@dfilter NVARCHAR(100)
@SDP DateTime
set @dfilter =
(case when @SDP is null
then '@SDP BETWEEN CAST(sp.StartDate AS DATE) AND CAST(sp.EndDate AS DATE) '
else '@SDP = CAST(sp.StartDate AS DATE) '
end)
set @SQL = N'UPDATE p SET PriceWithVAT = promo.Price, IsPromo=1
FROM #preturi p
JOIN
(SELECT pp.ItemId, Price
FROM SalesPromotion sp (NOLOCK)
JOIN SalesPromotionXSite sps (NOLOCK) ON sp.SalesPromotionId = sps.SalesPromotionId
JOIN SalesPromotionDetail spd (NOLOCK) ON sps.SalesPromotionId = spd.SalesPromotionId
JOIN #preturi pp on pp.ItemId = spd.ItemId
WHERE sps.SiteId = '+quotename(@SiteId)+
' AND spd.IsActive = 1 AND ' + @dfilter+
') promo ON p.ItemId = promo.ItemId'
BEGIN
EXEC sp_executesql @SQL
END
首先,更正语法(在DECLARE
语句中缺少',')并声明@SiteId
。
之后,请注意,您在 T-SQL 语句中使用了 @SDP 变量,因此您必须将此变量发送到 sp_executesql
:
...
EXEC sp_executesql @SQL, N'@SDP DateTime', @SDP
谢谢 Zhorow。 在这个答案中,我替换了
WHERE sps.SiteId = '+quotename(@SiteId)+
和
WHERE sps.SiteId = @SiteId'
我运行
EXEC sp_executesql @SQL, N'@StartDataPret DateTime, @SiteId int', @StartDataPret, @SiteId
效果很好