循环表(不使用游标)来连接数据

时间:2010-12-15 07:03:57

标签: sql sql-server-2005 tsql sql-server-2008

请参阅/运行以下脚本。

DECLARE @Products Table (   PId int )

INSERT @Products SELECT 100
INSERT @Products SELECT 101
INSERT @Products SELECT 102

DECLARE @Stff Table ( Stid int, StaffName VARCHAR(20) )

INSERT @Stff SELECT 301, 'White'
INSERT @Stff SELECT 302, 'Green'

DECLARE @Items Table
(
    ItemId int,
    PIdFK int,
    StIdFK int,
    CreatedDate DateTime,
    Notes varchar(100),
    NotesHistory varchar(2000)
)

INSERT INTO @Items SELECT 1, 100, 301, GETDATE(), Null, NULL
INSERT INTO @Items SELECT 2, 101, 301, GETDATE(), Null, NULL
INSERT INTO @Items SELECT 3, 101, 301, DATEADD(DD, 1, GETDATE()), 'Hello Dear', NULL
INSERT INTO @Items SELECT 4, 101, 301, DATEADD(DD, 2, GETDATE()), 'Did you get my msg?', NULL
INSERT INTO @Items SELECT 5, 102, 302, GETDATE(), Null, NULL
INSERT INTO @Items SELECT 6, 102, 302, DATEADD(DD, 1, GETDATE()), Null, NULL
INSERT INTO @Items SELECT 7, 102, 302, DATEADD(DD, 3, GETDATE()), 'How are you doing?', NULL
INSERT INTO @Items SELECT 8, 102, 302, DATEADD(DD, 4, GETDATE()), 'Your Items are ready.', NULL

SELECT * FROM @Items

DECLARE @ItemsHistory Table
(
    ItemHisotryId int,
    ItemIdFK int,
    StIdFK int,
    NotesHisotry varchar(200),
    CreatedDate DATETIME
)

INSERT INTO @ItemsHistory SELECT 1, 1, 301, NULL, GETDATE()
INSERT INTO @ItemsHistory SELECT 2, 2, 301, NULL, GETDATE()
INSERT INTO @ItemsHistory SELECT 3, 3, 301, 'Hello Dear', DATEADD(DD, 1, GETDATE())
INSERT INTO @ItemsHistory SELECT 4, 4, 301, 'Dimd you get my msg?', DATEADD(DD, 2, GETDATE())
INSERT INTO @ItemsHistory SELECT 5, 5, 302, NULL, GETDATE()
INSERT INTO @ItemsHistory SELECT 6, 6, 302, NULL, DATEADD(DD, 1, GETDATE())
INSERT INTO @ItemsHistory SELECT 7, 7, 302, 'How are you doing?', DATEADD(DD, 3, GETDATE())
INSERT INTO @ItemsHistory SELECT 8, 8, 302, 'Your Items are ready', DATEADD(DD, 4, GETDATE())

我想将Items表的Notes列和ItemsHistory表的NotsHistory列中的所有数据连接成一个字符串,实际上我想更新Items表的Items.NotesHistory列与ItemsHistory.NotesHisotry

相关的所有PID

请不要使用光标来帮助它。我正在使用sql server 2008,所以CTE可以做到这一点,但我没有得到我如何实现这一点。

结果字符串应如下所示

<b>Items.CreaedDate - Items.StaffName: <b/> Items.Notes <br/><b>ItemsHisotryCreatedDate - ItemsHistory.StaffName <b> ItemsHistory.NotesHistory

以下部分将从ItemsHistory表中添加到为唯一“PId&#39;

添加的每条记录中
<br/><b>ItemsHisotryCreatedDate - ItemsHistory.StaffName <b> ItemsHistory.NotesHistory

感谢。

2 个答案:

答案 0 :(得分:4)

您可以通过下面的内容连接而不循环;只需根据需要添加您自己的查询/联接等:

DECLARE @s varchar(max) = ''

SELECT @s = @s + '<br/><b>' + CONVERT(varchar(10), i.CreatedDate, 101) + '</b>' + ISNULL(i.Notes, '')
FROM @Items i

SELECT @s

必须确保其中没有NULL

但不要这样做!

数据库 构建html的地方;不仅如此,它还为您提供了非常 XSRF漏洞。我会在UI层中执行此操作,正确使用您使用的任何平台提供的html编码函数。盲目地将字符串连接为html几乎与盲目地将用户字符串连接到TSQL(而不是使用参数)相提并论;最好将格式化为borked(没有正确处理<等) - 最糟糕的是,您的用户将面临直接攻击风险。

答案 1 :(得分:1)

这样的东西
;WITH Vals AS (
        SELECT  PIdFK,
                Notes,
                ItemIdFK
        FROM    @ItemsHistory ih INNER JOIN
                @Items i    ON  ih.ItemIdFK = i.ItemId
        WHERE   Notes IS NOT NULL
)
SELECT 
        v.PIdFK,
        STUFF( 
               (SELECT 
                    ', ' + v2.Notes 
                    FROM Vals v2 
                    WHERE v.PIdFK=v2.PIdFK 
                    ORDER BY v2.ItemIdFK 
                    FOR XML PATH(''), TYPE 
               ).value('.','varchar(max)') 
               ,1,2, '' 
          ) AS Notes 
FROM Vals v 
GROUP BY v.PIdFK