0

我有两个变量。第一个变量@WhereClause包含值: c1='v111' and c2='v222' and c3='v333' and c4='v444'

第二个变量@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 回答