当子查询遵循=,!=,<,<=,>,> =或将子查询用作SQL中的表达式时,不允许这样做

时间:2019-02-09 09:45:16

标签: tsql stored-procedures dynamic-sql

需要子查询的数量

-DM已到达,但过去60天-VIP再次未到达

SET @MainQuery = 'SET @DMNotReachedAgainForLastSixtyDaysVIP = (SELECT COUNT(DISTINCT SMSCH.Cgvak_Comh_Icode) 
FROM Cgvak_Sms_Company_Header SMSCH 
JOIN Cgvak_Sms_Company_Detail SMSCD ON SMSCD.Cgvak_Comd_Comh_Icode = SMSCH.Cgvak_Comh_Icode 
JOIN [dbo].[Cgvak_Sms_User_Master] SMSUM ON SMSUM.Cgvak_User_Icode = SMSCH.Cgvak_Comh_AssignBDE_Icode
WHERE (SMSCD.Cgvak_Comd_Call_date >= DATEADD(DAY, -60, GETDATE())) AND SMSCD.Cgvak_Comd_SpoketoStatus = ''N''  AND ' + RTRIM(@DynamicQuery) + ' 
AND SMSCD.Cgvak_Comd_Comh_Icode IN (SELECT DISTINCT CSCH.Cgvak_Comh_Icode FROM Cgvak_Sms_Company_Header CSCH 
JOIN Cgvak_Sms_Company_Detail CSCD ON CSCD.Cgvak_Comd_Comh_Icode = CSCH.Cgvak_Comh_Icode 
WHERE  CSCH.Cgvak_Comh_VIPAC = ''Y'' AND CSCD.Cgvak_Comd_Call_date <= DATEADD(DAY, -60, GETDATE()) 
AND CSCH.Cgvak_Comh_DMSpokeStatus = 1 AND CSCH.Cgvak_Comh_Icode = CSCD.Cgvak_Comd_Comh_Icode))

EXECUTE sp_executesql @MainQuery , N'@DMNotReachedAgainForLastSixtyDaysVIP BIGINT OUTPUT', @DMNotReachedAgainForLastSixtyDaysVIP = @DMNotReachedAgainForLastSixtyDaysVIP OUTPUT

我正在将动态查询从前端传递到此SP。但它显示,子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

1 个答案:

答案 0 :(得分:0)

您发布的代码中没有任何问题。问题必须出在RTRIM(@DynamicQuery)中。