• You might want to look at the Office Open XML File Formats used by Word 2007 onwards, but they are complex.

    You can read introductory articles such as Walkthrough: Word 2007 XML Format and Working With Tables (WordprocessingML).

    I still do not recommend generating WordprocessingML directly from SQL, but you can do it. The following code generates a Word XML document containing a table (with one column coloured) driven by two joined tables:

    -- SQL Server 2008 R2 demonstration code to extract data into WordProcessingML for Microsoft Word 2007+

    -- Hold standard parts of your Word document package in XML variables

    DECLARE @Package1 XML;

    SET @Package1 = '

    <pkg:part xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage" pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">

    <pkg:xmlData>

    <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">

    <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>

    </Relationships>

    </pkg:xmlData>

    </pkg:part>

    ';

    -- An XML variable to hold the WordProcessingML document before it is inserted into the Package.

    DECLARE @Document XML;

    -- Declare XML namespaces for Microsoft Word 2007+ WordprocessingML

    WITH XMLNAMESPACES (

    'http://schemas.openxmlformats.org/markup-compatibility/2006' AS ve,

    'urn:schemas-microsoft-com:office:office' AS o,

    'http://schemas.openxmlformats.org/officeDocument/2006/relationships' AS r,

    'http://schemas.openxmlformats.org/officeDocument/2006/math' AS m,

    'urn:schemas-microsoft-com:vml' AS v,

    'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing' AS wp,

    'urn:schemas-microsoft-com:office:word' AS w10,

    'http://schemas.openxmlformats.org/wordprocessingml/2006/main' AS w,

    'http://schemas.openxmlformats.org/wordprocessingml/2006/main' AS w2, -- second reference to namespace allows duplication of elements with same name in query

    'http://schemas.microsoft.com/office/word/2006/wordml' AS wne

    ),

    -- Courses belong to a cluster. Represents a data table.

    Course AS (

    SELECT *

    FROM (

    VALUES ('HND Art and Design', 1)

    ,('Woodcraft Workshop', 1)

    ,('HNC Fitness Health and Exercise ', 2)

    ,('HND Coaching and Development of Sport', 2)

    ,('NC Intermediate 2 Administration', 3)

    ,('HNC Accounting', 3)

    ,('BA Business Administration', 3)

    ) AS Course(Title, Cluster)

    ),

    -- Clusters group many courses together, each has a different colour. Represents a data table.

    Cluster AS (

    SELECT *

    FROM (

    VALUES (1, 'Creative Industries', '#91278F')

    , (2, 'Sport and Fitness', '#A0CBED')

    , (3, 'Business and Management', '#00467F')

    ) AS Cluster(Identifier, Title, Colour)

    )

    SELECT @Document = (

    SELECT (

    SELECT Cluster.Title AS 'w:tc/w:p/w:r/w:t',

    Cluster.Colour AS 'w:tc/w:tcPr/w:shd/@w:fill',

    Course.Title AS 'w2:tc/w2:p/w2:r/w2:t' -- see note above in XMLNAMESPACES

    FROM Course

    INNER JOIN

    Cluster

    ON Course.Cluster = Cluster.Identifier

    FOR XML PATH('w:tr'), TYPE, ROOT('w:tbl')

    ) FOR XML PATH('w:body'), ROOT('w:document')

    )

    ;

    -- Declare XML namespaces for Microsoft Word 2007+ Packaging

    WITH XMLNAMESPACES (

    'http://schemas.microsoft.com/office/2006/xmlPackage' AS pkg

    )

    SELECT @Package1,

    '/word/document.xml' AS 'pkg:part/@pkg:name',

    'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' AS 'pkg:part/@pkg:contentType',

    @Document AS 'pkg:part/pkg:xmlData' -- insert the XML document you created above.

    FOR XML PATH(''), ROOT('pkg:package')

    /*

    You can save output as document.xml and open it in Word.

    To make Word the default application to open it, you can also manually insert the following as the first two lines of the XML document:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    <?mso-application progid="Word.Document"?>

    */

    It would be easier to generate HTML (which has less complex hierarchies of elements) and open it in Word.

    My personal preference is to generate a (preferably standardized) data-oriented XML format from SQL and transform it (using XSLT) into one or more document-oriented formats (HTML, Word, XSL-FO-into-PDF).