Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Combina due tavoli in uno

Forse qualcosa del genere (se stai usando MSSQL 2005+):

CREATE TABLE NewTable
(
    messageID INT,
    sender VARCHAR(100),
    recipient VARCHAR(MAX),
    date DATETIME
)
INSERT INTO NewTable(messageID,sender,recipient,date)
SELECT
    [Message].messageID,
    [Message].sender,
    STUFF
    (
        (
            SELECT 
                ',' +recipient
            FROM
                recipientInfo
            WHERE
                recipientInfo.messageID=[Message].messageID
            FOR XML PATH('')
        )
    ,1,1,'') AS recipient,
    [Message].date
FROM
    [Message]