9

我正在尝试创建一个 SQL 函数来测试参数是否以某个术语开头或包含该术语,但不以它开头。

基本上,如果参数以术语开头,则函数返回 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
4

4 回答 4

12

您不需要为返回值提供变量名,只提供其类型,并且不需要括号;

CREATE FUNCTION [dbo].[fnGetRelevance] 
(   
    @fieldName nvarchar(50),
    @searchTerm nvarchar(50)
)

RETURNS int
AS
....

还;

select Data from @fieldName

不起作用,您需要动态 SQL从名称位于变量中的对象中进行选择。

于 2011-06-07T11:26:47.747 回答
2

作为参考,这是根据 Alex K 的建议实现的完整功能

CREATE FUNCTION [dbo].[fnGetRelevance] 
(   
    @fieldName nvarchar(50),
    @searchTerm nvarchar(50)
)

RETURNS  int
AS
BEGIN
    if (@fieldName like @searchTerm + '%') -- starts with
    begin       
        return 0
    end
    else if ((@fieldName like '%' + @searchTerm + '%') and (@fieldName not like @searchTerm + '%')) -- contains, but doesn't start with 
    begin       
        return 1
    end

    return 1
END

GO
于 2011-06-07T13:39:10.397 回答
1

这里有几个问题。我在下面的代码中添加了注释:

CREATE FUNCTION [dbo].[fnGetRelevance] 
(   
    @fieldName nvarchar(50),
    @searchTerm nvarchar(50)
)

RETURNS @value int --Returning an int is fine, but you don't need the @value variable
(
    --This isn't required (unless you're returning a table)
)

AS

BEGIN

    declare @field TABLE(Data nvarchar(50))

    --@fieldname is a varchar, not a table (is this where your error is coming from).     
    --If @fieldname is the name of a table you're going to need to exec a sql string and concat @fieldname into the string
    insert into @field
        select Data from @fieldName 

    --You need a variable to contain the value from Data 
    --(ie declare @Data and select @Data = Data)
    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

这应该让你更接近你想要达到的目标。

于 2011-06-07T11:31:13.363 回答
0

我正在尝试创建一个 SQL 函数来测试参数是否以某个术语开头或包含该术语,但不以它开头。

我假设以下内容:

  • @fieldName实际上是一个表名(根据您的尝试使用情况判断)。
  • @searchterm是您要查找的术语
  • Data是表中的一列@fieldName

如果上述任何一项不正确,则此答案几乎毫无用处。

您将需要使用动态 sql,因为无法对选择查询中的表进行参数化。您将需要 2 个不同版本的动态 sql,因为您要检查“开始于”和更一般的“包含”。您将需要来自动态 sql 的输出变量以确定调用的结果。

顺便说一句,INT就尺寸而言,这完全是矫枉过正。如果你只有 2 个州(我怀疑)你想要BIT,如果你有 3 个州(我怀疑)你想要TINYINT。我现在将坚持使用 int 以接近您的原始示例,但请考虑更改它。

CREATE FUNCTION [dbo].[fnGetRelevance] 
(   
    @fieldName nvarchar(50),
    @searchTerm nvarchar(50)
)
RETURNS INT
AS
BEGIN

    DECLARE @startsWithResult INT,
            @containsResult INT
    DECLARE @startsWithSQL NVARCHAR(MAX) = N'SELECT @result=1 FROM ' + @fieldName + ' WHERE Data  LIKE '' + @searchTerm + '%'''
    DECLARE @containsSQL NVARCHAR(MAX) = N'SELECT @result=1 FROM ' + @fieldName + ' WHERE Data LIKE ''%' + @searchTerm + '%'''

   EXEC sp_ExecuteSQL @startsWithSQL, N'@result int output', @result = @startsWithResult OUTPUT

  IF @startsWithResult = 1
    RETURN 0

  EXEC sp_ExecuteSQL @containsSQL, N'@result int output', @result = @containsResult OUTPUT

  IF @containsResult = 1
    RETURN 1

END
于 2011-06-07T11:50:55.367 回答