我有一个有中间名和中间名的人。我正在尝试编写一个函数来检查给定名称中是否包含某些字母。例如' oistmiahf'将返回史密斯'
我试图用案例来表达:
CREATE FUNCTION dbo.ufn_FindNames(@lettersToSearch nvarchar(50))
RETURNS nvarchar(max)
AS
BEGIN
DECLARE @firstName nvarchar(50) = (SELECT FirstName FROM Employees),
@middleName nvarchar(50) = (SELECT MiddleName FROM Employees),
@ifNoResult nvarchar(50) = 'No result'
RETURN CASE
WHEN (@firstName LIKE '%' + @lettersToSearch + '%') THEN @firstName + ' '
WHEN (@middleName LIKE '%' + @lettersToSearch + '%') THEN @middleName + ' '
ELSE @ifNoResult
END
END
GO
在此之后我用" EXEC dbo.ufn_FindNames' oistmiahf' "我收到错误 的"子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。 "
如果我尝试使用相同的功能,但是使用IF转换我又有同样的错误。
CREATE FUNCTION dbo.ufn_FindNames(@lettersToSearch nvarchar(50))
RETURNS nvarchar(50)
AS
BEGIN
DECLARE @firstName nvarchar(50) = (SELECT FirstName FROM Employees),
@middleName nvarchar(50) = (SELECT MiddleName FROM Employees),
@result nvarchar(50)
IF(@firstName LIKE '%' + @lettersToSearch + '%')
BEGIN
RETURN @firstName + ' '
END
IF(@middleName LIKE '%' + @lettersToSearch + '%')
BEGIN
RETURN @middleName + ' '
END
RETURN @result
END
GO