我正在尝试计算表中使我创建的函数返回 1 的记录数。这是我所拥有的:
CREATE FUNCTION isErrorMismatch
(@theType nvarchar(1), @theExCode nvarchar(2))
RETURNS bit
AS
BEGIN
DECLARE @theTypeAsInt int
SET @theTypeAsInt = CAST(@theExCode AS INT)
DECLARE @returnValue bit
SET @returnValue = 0
IF @theType = 'A'
IF @theTypeAsInt >= 10 AND @theTypeAsInt <= 17
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE IF @theType = 'B'
IF @theTypeAsInt >= 18 AND @theTypeAsInt <= 26
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE IF @theType = 'C'
IF @theTypeAsInt >= 30 AND @theTypeAsInt <= 38
SET @returnValue = 0
ELSE
SET @returnValue = 1
ELSE
SET @returnValue = 1
RETURN @returnValue
END
GO
SELECT (SELECT COUNT(*)
FROM isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category],2))
As MismatchCount
FROM dbo.[All Service Ticket Data 2012_final]
我想计算使函数返回 1 的每条记录。当我调用该函数时,我的 FROM 中出现语法错误。有人有想法么?谢谢!
***更新:
为了获得使函数返回 1 的计数:
SELECT COUNT(dbo.isErrorMismatch(LEFT(Type, 1), LEFT([Exception Code/Category],2))) As MismatchCount
FROM dbo.[All Service Ticket Data 2012_final]
WHERE dbo.isErrorMismatch(LEFT(Type, 1), LEFT([Exception Code/Category],2)) = 1
为了获取使函数返回 1 的所有记录:
SELECT Type, [Exception Code/Category],
dbo.isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category] ,2)) as Mismatch
FROM dbo.[All Service Ticket Data 2012_final]
WHERE dbo.isErrorMismatch(LEFT(Type, 1),LEFT([Exception Code/Category] ,2)) = 1