我正在嘗試創建一個SQL函數,用於測試參數是以某個術語開頭還是包含術語,但不以它開頭。如何創建一個SQL Server函數來返回一個int?
基本上,如果參數與術語開始,該函數返回0,否則它返回1
這是函數的骨頭,我有,這我想從另一個適應函數我發現:
CREATE FUNCTION [dbo].[fnGetRelevance]
(
@fieldName nvarchar(50),
@searchTerm nvarchar(50)
)
RETURNS @value int -- this is showing an error, it seems to expect table but all I want is an int
(
-- does this need to be here? If so, what should it be?
)
AS
BEGIN
declare @field TABLE(Data nvarchar(50))
insert into @field
select Data from @fieldName
if (Data like @searchTerm + '%') -- starts with
begin
return 0
end
else if (Data like '%' + @searchTerm + '%' and Data not like @searchTerm + '%') -- contains, but doesn't start with
begin
return 1
end
END
GO
貌似你試圖返回'BIT'(布爾)不'INT'。雖然有一個你目前沒有處理的第三個狀態,不從一開始,也不包含'searchTerm'。 – Jamiec 2011-06-07 11:33:13