0

私は2つの変数を持っています。最初の変数@WhereClauseには値が含まれます: c1='v111' and c2='v222' and c3='v333' and c4='v444'.

2 番目の変数には次@ConditionFieldsの値が含まれますc2,c4

さて、どうすればc2とc4の値だけを取得できますか. 同様の@NewWhereClause値は次のようになります: c2='v222' and c4='v444'. 助けてください。

4

2 に答える 2

0

これは奇妙な方法のように思えるかもしれませんが、機能します。これも最適化できると確信しています。文字列を「分割」する方法はいくつかあります。これはかなりクールな方法だと思いました。

DECLARE @WhereClause varchar(MAX)
DECLARE @NewWhereClause varchar(MAX)
DECLARE @ConditionFields varchar(MAX)
DECLARE @Delimiter char(1)
DECLARE @X xml
DECLARE @TempCondition varchar(MAX)
DECLARE @TempWhereClause varchar(MAX)

SET @WhereClause = 'c1=''v111'' and c2=''v222'' and c3=''v333'' and c4=''v444'''
SET @ConditionFields = 'c2,c4'
SET @Delimiter = ','
SET @NewWhereClause = ''

SELECT @X = CONVERT(xml,'<root><s>' + REPLACE(@ConditionFields,@Delimiter,'</s><s>') + '</s></root>')

DECLARE ColCursor CURSOR FOR 
SELECT [Value] = T.c.value('.','varchar(20)')
FROM @X.nodes('/root/s') T(c)
WHERE T.c.value('.','varchar(20)') <> ''

OPEN ColCursor   
FETCH NEXT FROM ColCursor INTO @TempCondition   

WHILE @@FETCH_STATUS = 0   
BEGIN   
    SELECT @TempWhereClause = SUBSTRING(@WhereClause, CHARINDEX(@TempCondition, @WhereClause), CHARINDEX(' ', @WhereClause))
    IF @NewWhereClause = ''
    BEGIN
        SET @NewWhereClause = @TempWhereClause
    END
    ELSE
    BEGIN
        SET @NewWhereClause += ' and ' + @TempWhereClause
    END

    FETCH NEXT FROM ColCursor INTO @TempCondition   
END   

CLOSE ColCursor   
DEALLOCATE ColCursor

SELECT @NewWhereClause

これがSQLフィドルです:

于 2013-01-09T16:09:09.297 に答える
0

まず、投稿に残されたコメントに同意します。この実装はかなり醜いです。とは言え、暇なのでどうぞ。

/*
This will work assuming your variable formats are always consistent with no leading or trailing spaces:
@WhereClause format (SingleValue:"c1='v111'"; MultipleValues:"c1='v111' and c2='v222' and x#='...' and 'c156='anything'"
@ConditionFields format (SingleValue:"c2"; MultipleValues:"c2,c4,c7,...,c156"
*/

Create  Function dbo.ApplyConditionFields (@WhereClause Varchar(Max), @ConditionFields Varchar(Max))
Returns Varchar(Max)
With    Execute As Caller
As
Begin
        Declare @output Varchar(Max)

        If      @WhereClause Like '%=%'
        Begin
                ;With   parsedWhere As
                (
                        Select  Left(@WhereClause,CharIndex('=',@WhereClause)-1) As parsedVar,
                                Substring(@WhereClause,CharIndex('=',@WhereClause)+1,CharIndex(' ',@WhereClause+' ')-CharIndex('=',@WhereClause)-1) As parsedVal,
                                LTrim(Replace(Right(@WhereClause+' and ',Len(@WhereClause+' and ')-CharIndex(' and ',@WhereClause + ' and ')+2),' and ',' ')) As rest
                        Union   All
                        Select  Left(rest,CharIndex('=',rest)-1) As parsedVar,
                                Substring(rest,CharIndex('=',rest)+1,CharIndex(' ',rest)-CharIndex('=',rest)-1) As parsedVal,
                                Right(rest,Len(rest)-CharIndex(' ',rest)+1) As rest
                        From    parsedWhere
                        Where   rest <> ' '
                ),      parsedFields As
                (
                        Select  Left(@ConditionFields+',',CharIndex(',',@ConditionFields + ',')-1) As parsedVar,
                                Right(@ConditionFields+',',Len(@ConditionFields+',')-CharIndex(',',@ConditionFields+',')) As rest
                        Union   All
                        Select  Left(rest,CharIndex(',',rest + ',')-1) As parsedVar,
                                Right(rest,Len(rest)-CharIndex(',',rest+',')) As rest
                        From    parsedFields
                        Where   rest <> ''
                )   
                Select  @output = Coalesce(@output+' and ','')+pw.parsedVar+'='+pw.parsedVal
                From    parsedWhere pw
                Join    parsedFields pf
                        On  pw.parsedVar = pf.parsedVar
        End

        Return(@output)
End
于 2013-01-09T16:52:32.613 に答える