【问题标题】:How do I create a SQL Server function to return an int?如何创建 SQL Server 函数以返回 int?
【发布时间】:2011-09-10 00:04:02
【问题描述】:

我正在尝试创建一个 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

【问题讨论】:

  • 看起来您正试图返回 BIT(布尔值)而不是 INT。尽管您当前没有处理第三种状态;不以searchTerm开头且不包含@。

标签: sql sql-server tsql sql-function


【解决方案1】:

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

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

RETURNS int
AS
....

还有;

select Data from @fieldName

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

【讨论】:

    【解决方案2】:

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

    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
    

    这应该会让你更接近你想要实现的目标。

    【讨论】:

      【解决方案3】:

      作为参考,这是根据 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
      

      【讨论】:

        【解决方案4】:

        我正在尝试创建一个 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
        

        【讨论】:

          猜你喜欢
          • 2013-06-06
          • 1970-01-01
          • 2012-10-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-09-25
          • 2010-11-21
          相关资源
          最近更新 更多