MSSQL:使用参数内部值的子字符串值

时间:2013-01-09 15:34:10

标签: sql sql-server

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

第二个变量@ConditionFields包含值:c2,c4

现在,我怎么才能得到c2和c4的值。 类似@NewWhereClause的值将是:c2='v222' and c4='v444'。请帮忙。

2 个答案:

答案 0 :(得分: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 Fiddle

答案 1 :(得分: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