如何创建一个SQL Server函数来返回一个int?

如何创建一个SQL Server函数来返回一个int?

问题描述:

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

貌似你试图返回'BIT'(布尔)不'INT'。虽然有一个你目前没有处理的第三个状态,不从一开始,也不包含'searchTerm'。 – Jamiec 2011-06-07 11:33:13

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

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

RETURNS int 
AS 
.... 

另外;

select Data from @fieldName 

都不行,你需要dynamic SQL从对象它的名称是一个变量选择。

这里有几个问题。我已将评论添加到下面的代码中:

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)) 

    [email protected] 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 

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

我试图创建一个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 

以供参考,这是因为与建议,由亚历克斯ķ实现完整的功能

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