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 を使用する必要があります。「で始まる」とより一般的な「含む」をチェックしたいので、動的SQLの2つの異なるバージョンが必要になります。呼び出しの結果を判断するには、動的 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 に答える