替换SQL

时间:2015-06-07 00:14:07

标签: sql sql-server string replace

我尝试使用while循环和替换函数来更改SQL中的某些字符串值。 我有两个表,从中我得到的值和公式我必须改变。主要问题是当我执行代码时,我可以改变第一个公式的第一个匹配模式,但其他模式不会改变。 我不知道问题是我设置计数器的方式还是"选择"用于搜索值。 我的代码:

IF OBJECT_ID('tempdb..#TABLA_TEMP') IS NOT NULL BEGIN
    DROP TABLE #TABLA_TEMP
END
IF OBJECT_ID('tempdb..#TABLE_VALUES') IS NOT NULL BEGIN
    DROP TABLE #TABLE_VALUES
END

CREATE TABLE #TABLA_TEMP (
    ID INT IDENTITY NOT NULL PRIMARY KEY, 
    NUMERO VARCHAR(max)
)

INSERT INTO #TABLA_TEMP(NUMERO)VALUES('X=''A01TGR.420.JHNB''+''A02TGR.421.ZASD''')
INSERT INTO #TABLA_TEMP(NUMERO)VALUES('X=''A08TGR.427.YUJK''+''A03TGR.422.CVBN''')
INSERT INTO #TABLA_TEMP(NUMERO)VALUES('X=''A04TGR.423.TYUI''+''A05TGR.424.QWER''')

CREATE TABLE #TABLE_VALUES (
    ID INT IDENTITY NOT NULL PRIMARY KEY, 
    ID_INDICADOR INT, 
    CODIGO VARCHAR(max), 
    MtoValor DECIMAL(18,2)
)

INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19520, 'A01TGR.420.JHNB', 50.00)
INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19521, 'A02TGR.421.ZASD', 25.00)
INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19522, 'A03TGR.422.CVBN', 15.00)
INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19523, 'A04TGR.423.TYUI', 65.00)
INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19524, 'A05TGR.424.QWER', 30.00)
INSERT INTO #TABLE_VALUES(ID_INDICADOR, CODIGO, MtoValor) VALUES (19527, 'A08TGR.427.YUJK', 45.00)

select * from #TABLA_TEMP
--select * from #TABLE_VALUES

DECLARE @counter int
SET @counter = 1
DECLARE @counterInterno int
SET @counterInterno = 1

WHILE(@counter <= (SELECT MAX(ID) FROM #TABLA_TEMP)) BEGIN  
    DECLARE @MatchExpression VARCHAR(max)
    SET @MatchExpression = (''+ CAST(((SELECT CODIGO FROM #TABLE_VALUES
                                WHERE ID = @counter)) AS VARCHAR) +'')

    --WHILE 1 = 1
    WHILE(@counterInterno <= (SELECT MAX(ID) FROM #TABLE_VALUES)) BEGIN
        DECLARE @valorInterno VARCHAR(MAX)
        SET @valorInterno = (''+ CAST(((SELECT MtoValor FROM #TABLE_VALUES V
                                WHERE V.ID = @counterInterno)) AS VARCHAR) +'')

        DECLARE @RetVal varchar(max)
        SET @RetVal =  (SELECT REPLACE(NUMERO, @MatchExpression, @valorInterno)
                        FROM #TABLA_TEMP T WHERE T.ID = @counterInterno)

        IF(@RetVal IS NOT NULL)     
            UPDATE #TABLA_TEMP 
            SET NUMERO = @RetVal
            WHERE ID = @counter
        ELSE
            break

        SET @counterInterno = @counterInterno + 1
        SET @counter = @counter + 1
    END
END

SELECT * FROM #TABLA_TEMP

最后,我的想法是得到这样的东西:

X='65'+'15'+'25'

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:3)

试试这个:

SELECT ROW_NUMBER() OVER (ORDER BY t1.ID, t2.ID) As RowNo, t1.ID, t1.NUMERO, t2.CODIGO, t2.MtoValor 
INTO #NewTemp
FROM #TABLA_TEMP t1 JOIN #TABLE_VALUES t2 ON t1.NUMERO LIKE '%' + t2.CODIGO + '%'

DECLARE @Counter int = 1, @ID as int, @Codigo varchar(max), @MtoValor decimal(18, 2)

WHILE (EXISTS (SELECT 1 FROM #NewTemp WHERE RowNo = @Counter))
BEGIN

    SELECT @ID = nt.ID, @Codigo = nt.CODIGO, @MtoValor = nt.MtoValor
    FROM #NewTemp nt
    WHERE nt.RowNo = @Counter

    UPDATE #TABLA_TEMP 
    SET NUMERO = REPLACE(NUMERO, @Codigo, CAST(@MtoValor AS varchar(25)))
    WHERE ID = @ID

    SET @Counter = @Counter + 1
END

SELECT *
FROM #TABLA_TEMP

为此:

ID  | NUMERO
----+-----------------------
1   | X='50.00'+'25.00'
2   | X='45.00'+'15.00'
3   | X='65.00'+'30.00'