是否可以构建此sql更新查询而不将其包装在游标中?

时间:2013-06-05 10:58:43

标签: sql-server tsql

我有2张桌子。一个包含键/值对。另一个包含varchar列,需要将文本中的所有键实例替换为相应的值。

我已经使用可以正常工作的游标在存储过程中实现了这一点。但我确实需要存储过程尽可能高效地执行。我的第一个想法是删除光标。那可能吗?也许使用递归cte?示例如下。

设置

DECLARE @Dictionary TABLE 
(
    WordKey NVARCHAR(255),
    WordValue NVARCHAR(4000)
)

INSERT INTO @Dictionary
VALUES
    ('[key1]', 'Value1'),
    ('[key2]', 'Value2'),
    ('[key3]', 'Value3')


DECLARE @Phrasings TABLE
(
    Phrasing1 NVARCHAR(4000),
    Phrasing2 NVARCHAR(4000),
    Phrasing3 NVARCHAR(4000)
)

INSERT INTO @Phrasings
VALUES
    ('[key1]','random text','random text'),
    ('random text','random [key2] text','random text'),
    ('random text [key1]','random [key2] text','random [key1] [key3] text')

主要查询

DECLARE @WordKey NVARCHAR(255)
DECLARE @WordValue NVARCHAR(max)

DECLARE parsing_cursor CURSOR FOR
SELECT WordKey,WordValue FROM @Dictionary

OPEN parsing_cursor

FETCH NEXT FROM parsing_cursor
INTO @WordKey, @WordValue

WHILE @@FETCH_STATUS = 0 
    BEGIN

    UPDATE @Phrasings SET
        Phrasing1 = REPLACE(Phrasing1, @WordKey, @WordValue),
        Phrasing2 = REPLACE(Phrasing2, @WordKey, @WordValue),
        Phrasing3 = REPLACE(Phrasing3, @WordKey, @WordValue)

    FETCH NEXT FROM parsing_cursor
    INTO @WordKey, @WordValue
    END

CLOSE parsing_cursor
DEALLOCATE parsing_cursor

预期结果

Phrasing1               Phrasing2               Phrasing3
^^^^^^^^^               ^^^^^^^^^               ^^^^^^^^^
"Value1"                "random text"           "random text"
"random text"           "random Value2 text"    "random text"
"random text Value1"    "random Value2 text"    "random Value1 Value3 text"

2 个答案:

答案 0 :(得分:1)

试试这个 -

<强>查询:

DECLARE @Dictionary TABLE (WordKey VARCHAR(255), WordValue VARCHAR(400))
INSERT INTO @Dictionary (WordKey, WordValue)
VALUES
    ('[key1]', 'Value1'),
    ('[key2]', 'Value2'),
    ('[key3]', 'Value3')

DECLARE @Phrasings TABLE (Phrasing1 VARCHAR(400), Phrasing2 VARCHAR(400), Phrasing3 VARCHAR(400))
INSERT INTO @Phrasings (Phrasing1, Phrasing2, Phrasing3)
VALUES
    ('[key1]', 'random text', 'random text'),
    ('random text', 'random [key2] text', 'random text'),
    ('random text [key1]', 'random [key2] text', 'random [key1] [key3] text')

;WITH cte AS 
(
    SELECT x = (
        SELECT  
              Phrasing1
            , Phrasing2
            , Phrasing3
        FROM @Phrasings t
        FOR XML PATH('ID')
    ), lvl = 1

    UNION ALL

    SELECT REPLACE(x, t.WordKey, t.WordValue), lvl + 1
    FROM cte
    JOIN (
        SELECT WordKey, WordValue, rn = ROW_NUMBER() OVER (ORDER BY (SELECT 1))
        FROM @Dictionary
    ) t ON rn = lvl
)
SELECT 
      Phrasing1 = t.c.value('Phrasing1[1]', 'VARCHAR(400)')
    , Phrasing2 = t.c.value('Phrasing2[1]', 'VARCHAR(400)')
    , Phrasing3 = t.c.value('Phrasing3[1]', 'VARCHAR(400)')
FROM (
    SELECT TOP 1 x = CAST(x AS XML)
    FROM cte
    ORDER BY lvl DESC
) r
CROSS APPLY x.nodes('/ID') t(c)

<强>结果:

Phrasing1            Phrasing2            Phrasing3
-------------------- -------------------- ---------------------------
Value1               random text          random text
random text          random Value2 text   random text
random text Value1   random Value2 text   random Value1 Value3 text

答案 1 :(得分:1)

使用持久性表Dictionary,您可以使用像这样的用户函数

Create Function F_Replace(@Phrasing NVARCHAR(4000)) RETURNS NVARCHAR(4000)
begin
  Declare @Result NVARCHAR(4000)
  Select @Result=@Phrasing
  Select @Result= REPLACE(@Result,WordKey,WordValue)
  from Dictionary
  Return @Result
end

您可以查询

Select dbo.F_Replace(Phrasing1) ,dbo.F_Replace(Phrasing2)    ,dbo.F_Replace(Phrasing3)       
from @Phrasings