简体   繁体   English

根据 SQL Server 中其他列中的数据在字符串后面添加数据

[英]Add data behind string based on data in other column in SQL Server

I have the following challenge which I can't solve at the moment:我目前无法解决以下挑战:

I have a column (LongTextfield1) in, let's say, table 'Data' that contains HTML data.我有一个列 (LongTextfield1),比如说,包含 HTML 数据的表“数据”。 Each row in table 'Data' is basically a document with text and pictures.表“数据”中的每一行基本上都是一个包含文本和图片的文档。 In several occassions multiple pictures are included in the same column (LongTextfield1).在某些情况下,同一列 (LongTextfield1) 中包含多张图片。 In case of a picture, the following code will be shown that refers to the picture:如果是图片,将显示以下代码,引用图片:

\\Download.aspx?DocumentID={SomeGUID} \\Download.aspx?DocumentID={SomeGUID}

To make it a bit more challenging, this piece of code can be included in the same column multiple times, depending on how much pictures are included in the HTML (document).为了让它更具挑战性,这段代码可以多次包含在同一列中,具体取决于 HTML(文档)中包含的图片数量。 All pictures have their own GUID.所有图片都有自己的 GUID。 So far, so good.到现在为止还挺好。 This is already present in the column.这已经存在于列中。

In the application that I would like to show these pictures I also need a 'RecordID'.在我想显示这些图片的应用程序中,我还需要一个“RecordID”。 This is a unique ID per picture.这是每张图片的唯一 ID。 So, outcome should be:所以,结果应该是:

\\Download.aspx?DocumentID={SomeGUID}&Recorid=SomeID \\Download.aspx?DocumentID={SomeGUID}&Recorid=SomeID

I have a table 'DocumentData' that contains the unique GUID (DocumentID) and RecordID for each picture.我有一个表“DocumentData”,其中包含每张图片的唯一 GUID (DocumentID) 和 RecordID。 So, I know which RecordID belongs to each GUID.所以,我知道哪个 RecordID 属于每个 GUID。

I need a stored procedure (or other automatic mechanism) that adds the RecordID behind the corresponding GUID.我需要一个存储过程(或其他自动机制),在相应的 GUID 后面添加 RecordID。 I have tried to use REPLACE '{SomeGUID}' for {SomeGUID}&RecorID=SomeID' but that didn't work.我曾尝试将 REPLACE '{SomeGUID}' 用于 {SomeGUID}&RecorID=SomeID',但这没有用。 Below is the stored procedure, that is fed with data from the 'DocumentData' table.下面是存储过程,它使用来自“DocumentData”表的数据。 It keeps on replacing the same string with every possible GUID+RecordID combination: (DocumentID={SomeGUID}&RecordID=1{SomeGUID2}&RecordID2.. etc. and that's not what I want. @DocumentID and @Origin (RecorID) in the stored procedure refer to the 'DocumentData' table:它不断用每个可能的 GUID+RecordID 组合替换相同的字符串:(DocumentID={SomeGUID}&RecordID=1{SomeGUID2}&RecordID2.. 等等,这不是我想要的。@DocumentID 和 @Origin (RecorID) 在存储程序参考“DocumentData”表:

CREATE PROCEDURE [dbo].[Replace_Picture2] (
@DocumentID UNIQUEIDENTIFIER,
@Origin varchar(100)
) AS

BEGIN

update
data
set longtextfield1 = REPLACE(CAST(LongTextField1 AS VARCHAR(max)), ltrim(cast(@DocumentID as varchar(100))) + '}', ltrim(cast(@DocumentID as varchar(100))) + '}' + '&RecordID=' + @Origin + '"')
where 1=1
and pageid = 1
and groupid = 2
and subgroupid = 8

END

GO

How can I achieve to add the RecordID behind the DocumentID, based on the combinations present in table 'DocumentData'?如何根据表“DocumentData”中的组合在 DocumentID 后面添加 RecordID? Thanks for your help!谢谢你的帮助!

这是通过替换 '@DocumentID' + '}”' 解决的(注意 '}”' 而不是 '}'。这可以防止存储过程继续替换和替换..

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM