Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 用HTML实体替换特殊字符_Sql Server_Tsql_Sql Server 2012_Bcp - Fatal编程技术网

Sql server 用HTML实体替换特殊字符

Sql server 用HTML实体替换特殊字符,sql-server,tsql,sql-server-2012,bcp,Sql Server,Tsql,Sql Server 2012,Bcp,我在表表 id content ------------------------------------- 1 Hellö world, I äm text 2 ènd there äré many more chars 3 that are speçial in my dat£base 我现在需要使用bcp将这些记录导出到HTML文件中: set @command = 'bcp "select [content] from [TABLE] where [id]

我在表

id    content
-------------------------------------
1     Hellö world, I äm text
2     ènd there äré many more chars
3     that are speçial in my dat£base
我现在需要使用bcp将这些记录导出到HTML文件中:

set @command = 'bcp "select [content] from [TABLE] where [id] = ' + 
            @id queryout +' + @filename + '.html" -S ' + @instance +
            ' -c -U ' + @username + ' -P ' + @password"

exec xp_cmdshell @command, no_ouput
为了使输出看起来正确,我需要首先用它们各自的HTML实体(伪)替换所有特殊字符

但到现在为止,我已经有30个嵌套的
replace
s,它开始看起来很疯狂

经过多次搜索,我找到了,但它太高级了,我无法理解:

  • 该表没有列出特殊字符本身,因为它们在我的文本中(
    ö,á
    etc),而是UnicodeEx。我是否需要将它们添加到表中以进行所需的转换
  • 我无法理解如何更新脚本以替换所有特殊字符。有人能给我看一段(伪)代码吗

  • 使用转换表执行此操作的一种方法是使用递归cte进行替换,再使用一个cte仅获取每个转换值的最后一行

    首先,创建并填充样本表(请在以后的问题中保存此步骤):

    然后,创建并填充转换表(我不知道这些字符的HTML实体,所以我只使用了数字[而且在结果中更容易看到])。此外,请注意,这可以使用链中的另一个cte来完成

    DECLARE @Translations AS TABLE
    (
        str nchar(1),
        replacement nvarchar(10)
    )
    
    INSERT INTO @Translations (str, replacement) VALUES
    ('ö', '-1-'),
    ('ä', '-2-'),
    ('è', '-3-'),
    ('ä', '-4-'),
    ('é', '-5-'),
    ('ç', '-6-'),
    ('£', '-7-')
    
    现在,第一个cte将进行替换,第二个cte只添加一个行号,这样对于每个id,lvl的最后一个值将得到1:

    ;WITH CTETranslations AS
    (
        SELECT id, content, 1 As lvl
        FROM @T
        UNION ALL
        SELECT id, CAST(REPLACE(content, str, replacement) as nvarchar(100)), lvl+1
        FROM CTETranslations
        JOIN @Translations 
            ON content LIKE '%' + str + '%' 
    ), cteNumberedTranslation AS
    (
        SELECT id, content, ROW_NUMBER() OVER(PARTITION BY Id ORDER BY lvl DESC) rn
        FROM CTETranslations
    )
    
    从第二个cte中选择,其中rn=1,我已加入原始表,以并排显示源代码和翻译:

    SELECT r.id, s.content, r.content
    FROM @T s
    JOIN cteNumberedTranslation r
        ON s.Id = r.Id
    WHERE rn = 1
    ORDER BY Id
    
    结果:

    id  content                             content
    1   Hellö world, I äm text              Hell-1- world, I -4-m text
    2   ènd there äré many more chars       -3-nd there -4-r-5- many more chars
    3   that are speçial in my dat£base     that are spe-6-ial in my dat-7-base
    
    请注意,如果您的内容包含超过100个特殊字符,则需要在最终选择中添加
    maxrecursion 0
    提示:

    SELECT r.id, s.content, r.content
    FROM @T s
    JOIN cteNumberedTranslation r
        ON s.Id = r.Id
    WHERE rn = 1
    ORDER BY Id
    OPTION ( MAXRECURSION 0 );
    

    您可以以易于理解的方式制作一个简单的表,其中包含sourcecharacter、destinationcharacter等列,并运行循环(游标)来完成所有替换操作。这将保持您的代码整洁,并完成工作。您还可以有30个更新行,而不是嵌套。这是你的眼睛糖果,你的规则。这个答案可能会有帮助:@MJH链接中的答案对于单个字符串是好的,我认为你不能对表列使用相同的技术。
    id  content                             content
    1   Hellö world, I äm text              Hell-1- world, I -4-m text
    2   ènd there äré many more chars       -3-nd there -4-r-5- many more chars
    3   that are speçial in my dat£base     that are spe-6-ial in my dat-7-base
    
    SELECT r.id, s.content, r.content
    FROM @T s
    JOIN cteNumberedTranslation r
        ON s.Id = r.Id
    WHERE rn = 1
    ORDER BY Id
    OPTION ( MAXRECURSION 0 );