简体   繁体   English

如何用SQL Server中另一个表中的值替换消息中的标记

[英]How to replace tags in a message with values from another table in SQL Server

I have 2 tables, one has messages with some tags in the messages that can be replaced with values for specific areas of my system. 我有2个表,其中一个表的消息中带有一些标记,这些标记可以替换为系统特定区域的值。

The other table contains the tag values. 另一个表包含标签值。

Is there a way I write a SQL select statement so I can view the message description with tags replaced with the actual values. 有没有一种方法可以编写SQL select语句,以便可以查看消息描述,并用标记替换为实际值。 Here is an example of my tables 这是我的桌子的例子

Table 1 - MessageDescriptions 表1- MessageDescriptions

Id      Description
------------------------------------------------------
1       This message is for <<User.Name>>.       
2       <<User.Name>> uses the currency <<Currency.Code>>.

Table 2 - TagValues 表2- TagValues

Id      Tags                                               DescriptionId
----------------------------------------------------------------------------
5       [<<User.Name>>:Mr John Smith]                          1
6       [<<User.Name>>:Mr John Smith][<<Currency.Code>>:GBP]   2

These tags are stored in the same column in the table which is what is causing me issues. 这些标签存储在表的同一列中,这就是导致我出现问题的原因。 Does anyone know a way I can replace the tag values eg Mr John Smith with the tags in the message descriptions? 有谁知道我可以用消息描述中的标签替换标签值(例如John Smith先生)的方法吗?

This is all I have so far, but this only selects the 2 columns from each table. 到目前为止,这就是我所拥有的,但这仅从每个表中选择2列。 I now need to replace the tags with the tag values: 我现在需要将标签替换为标签值:

SELECT
    TagValues.Tags, 
    MessageDescriptions.[Description]
FROM 
    TagValues 
INNER JOIN 
    MessageDescriptions ON TagValues.DescriptionId = MessageDescriptions.Id

For this example, I would like my output to be: 对于此示例,我希望输出为:

Description
------------------------------------------------------
This message is for Mr John Smith.       
Mr John Smith uses the currency GBP.

may be simply using replace we can achieve this one 可能只是使用替换我们就可以实现这一目标

DECLARE @Table1 TABLE 
    (Id int, Description varchar(50))
;

INSERT INTO @Table1
    (Id, Description)
VALUES
    (1, 'This message is for <<User.Name>>.'),
    (2, '<<User.Name>> uses the currency <<Currency.Code>>.')
;
DECLARE  @Table2 TABLE 
    (Id int, Tags varchar(52), DescriptionId int)
;

INSERT INTO @Table2
    (Id, Tags, DescriptionId)
VALUES
    (5, '[<<User.Name>>:Mr John Smith]', 1),
    (6, '[<<User.Name>>:Mr John Smith][<<Currency.Code>>:GBP]', 2)
;

SELECT
TagValues.Tags, 
REPLACE(REPLACE(MessageDescriptions.[Description],'<<User.Name>>','Mr John Smith'),'<<Currency.Code>>','GBP')
FROM @Table2 TagValues 
INNER JOIN @Table1 MessageDescriptions ON TagValues.DescriptionId = MessageDescriptions.Id

Ok, I hope this may help others. 好的,我希望这可以帮助其他人。 I've managed to find a way, but its not pretty. 我设法找到了一种方法,但是它并不漂亮。 Here is the code: 这是代码:

DECLARE @EntityId uniqueidentifier = ''

CREATE TABLE #AuditTags
(
    AuditId uniqueidentifier NOT NULL,
    TagField nvarchar(40),
    TagValue nvarchar(300)
)
CREATE TABLE #AuditDetails
(
    [AuditId] uniqueidentifier NOT NULL,
    [DateCreated] datetime,
    [Description] nvarchar(max),
    [UserId] uniqueidentifier
)

INSERT INTO #AuditTags (AuditId, TagField, TagValue)
SELECT A.AuditId, Split.a.query('field').value('.', 'VARCHAR(100)') AS Tag, Split.a.query('value').value('.', 'VARCHAR(100)') AS Value  
 FROM  
 (
     SELECT event_audit_id AS AuditId,  
         CAST ('<doc>' + REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(tag_value, ':', ''), '|', ''), '<<', '<field>'), '>>', '</field><value>'), '[', '<tag>'), ']', '</value></tag>') + '</doc>' AS XML) AS Data  
     FROM  TagValues
     WHERE [entity_id] = @EntityId
 ) AS A CROSS APPLY Data.nodes ('/doc/tag') AS SPLIT(a);

INSERT INTO #AuditDetails
SELECT event_audit_id, TagValues.date_created, MessageDescriptions.[description], [user_id]
FROM TagValues
INNER JOIN MessageDescriptions ON TagValues.content_template_id = MessageDescriptions.content_template_id 
WHERE TagValues.[entity_id] = @EntityId

DECLARE @AuditId uniqueidentifier, @TagField nvarchar(40), @TagValue nvarchar(300)
DECLARE tag_cursor CURSOR
    FOR SELECT AuditId, TagField, TagValue FROM #AuditTags
OPEN tag_cursor
FETCH NEXT FROM tag_cursor INTO @AuditId, @TagField, @TagValue
WHILE @@FETCH_STATUS = 0
BEGIN
    UPDATE #AuditDetails SET [Description] = REPLACE([Description], '<<'+@TagField+'>>', @TagValue) WHERE AuditId = @AuditId

    FETCH NEXT FROM tag_cursor INTO @AuditId, @TagField, @TagValue
END
CLOSE tag_cursor;
DEALLOCATE tag_cursor;

SELECT
AuditId,
DateCreated AS [Date and time],
[Description], 
[User].salutation + ' ' + [User].first_name + ' ' + [User].last_name AS Actioner
FROM #AuditDetails
INNER JOIN [User] ON #AuditDetails.UserId = [User].[user_id]

DROP TABLE #AuditTags
DROP TABLE #AuditDetails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL从另一个表替换表中的值 - SQL replace values in a table from another table SQL Server 2008-用另一个表中的值替换列中的文本值 - SQL Server 2008 - Replace Text Values in Column with Values from Another Table 如何计算一个表中的值并更新 SQL Server 中同一台服务器上的另一个表? - How to count values from one table and update another table on same server in SQL Server? 用来自另一个表的 id 替换由字符串分隔的列 - SQL Server - Replace columns separated by string with id from another table - SQL Server 从 SQL Server 中的字符串替换 CSS 标记 - Replace CSS tags from string in SQL Server SQL Server 2008更新表包含另一个表中的值 - SQL Server 2008 update table with values from another table 通过从另一个表SQL Server插入值来更新一个表 - update one table by inserting values from another table SQL server SQL Server 2000:使用另一个表中的值更新一个表 - SQL Server 2000: Updating one table with values from another table 如何用存储在 SQL Server 中不同表中的值替换 XML 中的字符串? - How to replace Strings in XML with values stored in different table in SQL Server? 如何从另一个表中插入值以及SQL Server中的其他值? - How to insert values from a table in another along with other values in sql server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM