如何创建SQL Server函数以返回int?

时间:2011-06-07 11:21:21

标签: sql sql-server tsql sql-function

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

基本上,如果参数以term开头,则函数返回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 个答案:

答案 0 :(得分:10)

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

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

RETURNS int
AS
....

也;

select Data from @fieldName

无法使用,您需要dynamic SQL从名称中包含变量的对象中进行选择。

答案 1 :(得分:0)

这里有一些问题。我在下面的代码中添加了评论:

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

这可以让你更接近你想要实现的目标。

答案 2 :(得分:0)

  

我正在尝试创建一个SQL函数   测试参数是否开始   有一定期限或含有   术语,但不是从它开始。

我假设如下:

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

如果以上任何一项都不正确,那么这个答案就是无用的。

您将需要使用动态sql,因为select查询中的表无法参数化。您需要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

答案 3 :(得分:0)

供参考,这是与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