Azure Sql:无法替换HTML字符串

Azure Sql:无法替换HTML字符串,sql,sql-server,azure-sql-database,Sql,Sql Server,Azure Sql Database,我有下面的html在数据库中 <table> <tbody> <tr> <td> <table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspa

我有下面的html在数据库中

<table>
    <tbody>        
        <tr>
            <td>
                <table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
                    <tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <!-- /button -->
        <!-- spacing -->
        <tr>
            <td width="100%" height="15"></td>
        </tr>
    </tbody>
</table>
但是,当我尝试直接在db colmn的html上执行它时,如下所示,它在哪里都不起作用

    <tr>
        <td>
            <table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
                <tbody>
                    <tr>
                        <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                            <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                            </span>
                        </td>
                    </tr>
                    <tr style="visibility: __displayProp__">
                        <td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
                            <span style="color: #FFFFFF; font-weight: 300;">
                                <a style="color: #4d4d4d; text-align: center;"  href="__CancelLink__" target="_blank">notification.templates.key</a>
                            </span>
                        </td>
                    </tr>
                </tbody>
            </table>
        </td>
    </tr>
                DECLARE @html VARCHAR(MAX) =    (SELECT top 1 BodyText
                                                FROM [MessageTemplate]
                                                WHERE body LIKE '%<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">%')
                  SELECT @html = REPLACE(@html,
                '<tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>'
                ,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
                    <tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                        <tr style="visibility: __displayProp__">
                            <td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
                                <span style="color: #FFFFFF; font-weight: 300;">
                                    <a style="color: #4d4d4d; text-align: center;"  href="__CancelLink__" target="_blank">notification.templates.key</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>')
                                  
                print @html            

有人能帮我吗?

请尝试以下解决方案

它将HTML视为XHTML,即XML

之后,MS SQL Server的XQuery通过.modify方法简化了操作

SQL


请尝试以下解决方案

它将HTML视为XHTML,即XML

之后,MS SQL Server的XQuery通过.modify方法简化了操作

SQL

                DECLARE @html VARCHAR(MAX) =    (SELECT top 1 BodyText
                                                FROM [MessageTemplate]
                                                WHERE body LIKE '%<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">%')
                  SELECT @html = REPLACE(@html,
                '<tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>'
                ,'<table height="36" align="center" valign="middle" border="0" cellpadding="0" cellspacing="0" class="tablet-button">
                    <tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle" height="36" style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;" href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                        <tr style="visibility: __displayProp__">
                            <td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
                                <span style="color: #FFFFFF; font-weight: 300;">
                                    <a style="color: #4d4d4d; text-align: center;"  href="__CancelLink__" target="_blank">notification.templates.key</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>')
                                  
                print @html            
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, BodyText XML);
INSERT INTO @tbl (BodyText) VALUES
(N'<table>
    <tbody>
        <tr>
            <td>
                <table height="36" align="center" valign="middle" border="0"
                       cellpadding="0" cellspacing="0" class="tablet-button">
                    <tbody>
                        <tr>
                            <td width="auto" align="center" valign="middle"
                                height="36"
                                style=" background-color: __CustomButtonBackgroundColor__; border-top-left-radius: 4px; border-bottom-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; background-clip: padding-box; font-size: 15px; font-family: Helvetica, arial, sans-serif; text-align: center; color: __CustomButtonForegroundColor__; font-weight: 10; padding-left: 25px; padding-right: 25px;">
                                <span style="color: __CustomButtonForegroundColor__; font-weight: 300;">
                                    <a style="color: __CustomButtonForegroundColor__; text-align: center; text-decoration: none;"
                                       href="__ConfirmLink__" target="_blank">Confirm</a>
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <!-- /button -->
        <!-- spacing -->
        <tr>
            <td width="100%" height="15"></td>
        </tr>
    </tbody>
</table>');

DECLARE @newTR XML =
N'<tr style="visibility: __displayProp__">
    <td style="font-family: Helvetica, arial, sans-serif; font-size: 14px; color: #95a5a6; text-align: left; line-height: 35px; text-align: center">
        <span style="color: #FFFFFF; font-weight: 300;">
            <a style="color: #4d4d4d; text-align: center;" href="__CancelLink__"
               target="_blank">notification.templates.key</a>
        </span>
    </td>
</tr>';

DECLARE @html XML = (SELECT BodyText FROM @tbl);

-- before
SELECT @html;

SET @html.modify('insert sql:variable("@newTR") into (/table/tbody/tr/td/table/tbody)[1]');

-- after
SELECT @html;