【发布时间】:2017-07-07 10:31:28
【问题描述】:
我有一些数据需要输出为包含标记标签的行,我在表值函数中执行此操作。
使用以下格式的代码,使用search 查询收集我的数据,然后使用results 的输出插入到我返回的表中,这一直运行良好。
我现在需要获取更长的数据字段并将其拆分为多行,而我不知道如何实现这一点。
我的想法是,我想使用 CTE 来处理来自我的查询的数据,但我看不到将数据从我的 search 查询获取到我的 CTE 并从那里获取到我的 @ 987654324@设置。
我想我可以通过在数据库中创建另一个表值函数来看到另一种方法,如果我将其提供给我的comment_text 列,该函数将返回结果集,但这样做似乎很浪费。
有人看到解决方案的路径吗?
“真实”表示例:
DECLARE @Comments TABLE
(
id INT NOT NULL IDENTITY PRIMARY KEY CLUSTERED,
comment_date DATETIME NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
comment_title VARCHAR(50) NOT NULL,
comment_text char(500)
);
添加评论行:
INSERT INTO @Comments VALUES(CURRENT_TIMESTAMP, 'Bob', 'Example','Bob''s Comment', 'Text of Bob''s comment.');
INSERT INTO @Comments VALUES(CURRENT_TIMESTAMP, 'Alice', 'Example','Alice''s Comment', 'Text of Alice''s comment that is much longer and will need to be split over multiple rows.');
返回结果表格式:
DECLARE @return_table TABLE
(
comment_date DATETIME,
commenter_name VARCHAR(101),
markup VARCHAR(100)
);
朴素查询(无法运行,因为SplitComment CTE中的变量comment_text无法识别。
WITH SplitComment(note,start_idx) AS
(
SELECT '<Note>'+SUBSTRING(comment_text,0,50)+'</Note>', 0
UNION ALL
SELECT '<Text>'+SUBSTRING(note,start_idx,50)+'</Text>', start_idx+50 FROM SplitComment WHERE (start_idx+50) < LEN(note)
)
INSERT INTO @return_table
SELECT results.* FROM
(
SELECT
comment_date,
CAST(first_name+' '+last_name AS VARCHAR(101)) commenter,
comment_title,
comment_text
FROM @Comments
) AS search
CROSS APPLY
(
SELECT comment_date, commenter, '<title>'+comment_title+'</title>' markup
UNION ALL SELECT comment_date, commenter, SplitComment
) AS results;
SELECT * FROM @return_table;
结果(当函数在没有 CTE 的情况下运行时):
comment_date commenter_name markup
2017-07-07 11:53:57.240 Bob Example <title>Bob's Comment</title>
2017-07-07 11:53:57.240 Alice Example <title>Alice's Comment</title>
理想情况下,我希望为 Bob 的评论增加一行,为 Alice 的评论增加两行。像这样的:
comment_date commenter_name markup
2017-07-07 11:53:57.240 Bob Example <title>Bob's Comment</title>
2017-07-07 11:53:57.240 Bob Example <Note>Bob's Comment</Note>
2017-07-07 11:53:57.240 Alice Example <title>Alice's Comment</title>
2017-07-07 11:53:57.240 Alice Example <Note>Text of Alice''s comment that is much longer and w</Note>
2017-07-07 11:53:57.240 Alice Example <Text>ill need to be split over multiple rows.</Text>
【问题讨论】:
-
有任何样品说明您正在放入什么以及您希望看到什么?
-
如果你想从 T-SQL 生成 XML/HTML,请使用 FOR XML 而不是字符串连接。
-
@Bogdan_Sahlean 我不想生成正确的 XML/HTML,我需要以这种疯狂的格式返回结果,以便它们可以被业务中其他地方使用的另一个应用程序使用。你还没有见过这个功能最糟糕的东西......
-
您真的需要交叉申请吗?如果日期是评论和评论者之间的联系 - 它不只是在您的 SplitComment 中分配一个行号,并且内部/左加入您的评论者,评论和排序?
-
@AllanS.Hansen 我的问题的主旨是关于如何将
comment_text变量放入SplitComment 函数。目前它根本不知道comment_text存在,所以我不能使用它。
标签: sql-server sql-server-2012 common-table-expression