Is FOR XML RAW, ELEMENTS = FOR XML PATH, ELEMENTS?

  • Getting ready for 70-461, and reviewing some XML... Running this query makes it seem like the two are the same... is this always the case? Or am I missing something?

    IF OBJECT_ID('XMLForHomework') IS NOT NULL DROP TABLE XMLForHomework

    CREATE TABLE XMLForHomework

    (

    ID INT PRIMARY KEY IDENTITY (1,1),

    FirstName varchar(20),

    LastName varchar(20),

    FavoriteTeam varchar(10)

    )

    -- Add some stuff

    INSERT INTO XMLForHomework VALUES

    ('Ben', 'Jamin', 'Tribe'),

    ('Chris', 'Smith', 'Sox'),

    ('Beth', 'Smith', 'Cubs'),

    ('Your', 'Mom', 'Yanks'),

    ('Your', 'Dad', 'Mets')

    --

    --SELECT * FROM XMLForHomework FOR XML AUTO

    --SELECT * FROM XMLForHomework FOR XML RAW

    SELECT * FROM XMLForHomework FOR XML RAW, ELEMENTS

    --SELECT * FROM XMLForHomework FOR XML PATH('Path')

    SELECT * FROM XMLForHomework FOR XML PATH, ELEMENTS

    --

    DROP TABLE XMLForHomework

  • I'm not sure what you mean by "always the same." There are things that you can't do with Raw that you can with Path.

    IF OBJECT_ID('XMLForHomework') IS NOT NULL DROP TABLE XMLForHomework

    CREATE TABLE XMLForHomework

    (

    ID INT PRIMARY KEY IDENTITY (1,1),

    FirstName varchar(20),

    LastName varchar(20),

    FavoriteTeam varchar(10)

    )

    -- Add some stuff

    INSERT INTO XMLForHomework VALUES

    ('Ben', 'Jamin', 'Tribe'),

    ('Chris', 'Smith', 'Sox'),

    ('Beth', 'Smith', 'Cubs'),

    ('Your', 'Mom', 'Yanks'),

    ('Your', 'Dad', 'Mets')

    --

    --SELECT * FROM XMLForHomework FOR XML AUTO

    --SELECT * FROM XMLForHomework FOR XML RAW

    SELECT * FROM XMLForHomework FOR XML RAW, ELEMENTS

    --SELECT * FROM XMLForHomework FOR XML PATH('Path')

    SELECT * FROM XMLForHomework FOR XML PATH, ELEMENTS

    SELECT FavoriteTeam

    , FirstName as [Name/@FirstName]

    , LastName as [Name/@LastName]

    FOR XML Path, ELEMENTS;

    --

    DROP TABLE XMLForHomework

    This code creates a subelement for each row that has the name called Name, with two attributes. You can't do that with Raw.

    Russel Loski, MCSE Business Intelligence, Data Platform

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply