这是因为
SELECT SUM(PayAmount)
FROM TeacherPayment_Detail
WHERE TeacherPaymentId IN ('20,21' )
但 SQL Server 期望
SELECT SUM(PayAmount)
FROM TeacherPayment_Detail
WHERE TeacherPaymentId IN ('20','21' )
因此,您可以执行以下任何一项操作
在 SQL 中编写一些将“20,21”标记为“20”、“21”的函数(最好执行一个返回表的函数)
SELECT SUM(PayAmount) FROM TeacherPayment_Detail WHERE TeacherPaymentId IN(从 Function_Split(TeacherPayment.Advance_IDs 选择 val)
以 'val1','val2' 形式传递值
当它只有一个值时,它将是 'val' 并且可以工作,但是当它是多个值时,它将是 'val1,val2'。所以这不会导致错误。如果你像这样传递'val1','val2',它将起作用
** 功能 **
CREATE FUNCTION GetTokenizeValue
(
@strCSVString VARCHAR(4000)
)
RETURNS
@Result TABLE
(
-- Add the column definitions for the TABLE variable here
ID int
)
AS
BEGIN
; WITH CTE(Start, [Stop]) AS
(
SELECT 1, CHARINDEX(',' , @strCSVString )
UNION ALL
SELECT [Stop] + 1, CHARINDEX(',' ,@strCSVString , [Stop] + 1)
FROM CTE
WHERE [Stop] > 0
)
INSERT INTO @Result
SELECT SUBSTRING(@strCSVString , Start, CASE WHEN stop > 0 THEN [Stop]-Start ELSE 4000 END) AS stringValue
FROM CTE
RETURN
END
您的选择查询将是
SELECT SUM(PayAmount)
FROM TeacherPayment_Detail
WHERE TeacherPaymentId IN (select * from dbo.GetTokenizeValue(TeacherPayment.Advance_IDs))