Problem with FOR XML EXPLICIT

  • Hello everybody!

    I'm new to FOR XML...

    So here is my problem:

    I've got a query that returns the correct result regarding rows, nesting etc...

    SELECT

    TAG,

    Parent,

    [Type!1!Type],

    [Type!1!TypeID],

    [Type!1!TypeName],

    [Type!1!TypeValue],

    [Property!2!Type],

    [Property!2!PropID],

    [Property!2!PropName],

    [Value!3!Type],

    [Value!3!ValID],

    [Value!3!ValName],

    [Value!3!ValValue]

    FROM

    (

    -- get all Types from TBL_Value_Types

    SELECT

    DISTINCT

    1 AS TAG,

    NULL AS Parent,

    'Type' AS [Type!1!Type],

    T.TypeID AS [Type!1!TypeID],

    T.TypeName AS [Type!1!TypeName],

    T.Value AS [Type!1!TypeValue],

    T.DisplayOrder AS [Type!1!TypeDisplayOrder],

    NULL AS [Property!2!Type],

    NULL AS [Property!2!PropID],

    NULL AS [Property!2!PropName],

    NULL AS [Property!2!PropDisplayOrder],

    NULL AS [Value!3!Type],

    NULL AS [Value!3!ValID],

    NULL AS [Value!3!ValName],

    NULL AS [Value!3!ValValue],

    NULL AS [Value!3!ValDisplayOrder]

    FROM dbo.TBL_Value_Types AS T

    LEFT JOIN TBL_Value_Properties AS P ON P.TypeID_FK = T.TypeID

    LEFT JOIN TBL_Value_Property_Values AS V ON V.PropID_FK = P.PropID

    UNION

    -- get all Properties from TBL_Value_Properties

    SELECT

    2 AS TAG,

    1 AS Parent,

    NULL AS [Type!1!Type],

    T.TypeID,

    T.TypeName,

    T.DisplayOrder,

    T.Value,

    'Property' AS [Property!2!Type],

    P.PropID,

    P.PropName,

    P.DisplayOrder,

    NULL AS [Value!3!Type],

    NULL AS [Value!3!ValID],

    NULL AS [Value!3!ValName],

    NULL AS [Value!3!ValValue],

    NULL AS [Value!3!ValDisplayOrder]

    FROM dbo.TBL_Value_Types AS T

    LEFT JOIN TBL_Value_Properties AS P ON P.TypeID_FK = T.TypeID

    LEFT JOIN TBL_Value_Property_Values AS V ON V.PropID_FK = P.PropID

    UNION

    -- get all Values from TBL_Value_Property_Values

    SELECT

    3 AS TAG,

    2 AS Parent,

    NULL AS [Type!1!Type],

    T.TypeID,

    T.TypeName,

    T.DisplayOrder,

    T.Value,

    NULL AS [Property!2!Type],

    P.PropID,

    P.PropName,

    P.DisplayOrder,

    'Value' AS [Value!3!Type],

    V.ValID,

    V.ValName,

    V.ValValue,

    V.DisplayOrder

    FROM dbo.TBL_Value_Types AS T

    LEFT JOIN TBL_Value_Properties AS P ON P.TypeID_FK = T.TypeID

    LEFT JOIN TBL_Value_Property_Values AS V ON V.PropID_FK = P.PropID

    WHERE V.ValID IS NOT NULL

    ) AS TEMP

    ORDER BY

    [Type!1!TypeID],

    [Type!1!TypeDisplayOrder],

    [Property!2!PropDisplayOrder],

    [Value!3!ValDisplayOrder],

    [Type!1!Type]

    If I add "FOR XML AUTO" the result itself (meaning rows, nesting etc...) looks still fine, but it's not the desired XML-structure.

    If I add "FOR XML EXPLICIT" the result is missing several rows. After further investigation it seems that the last "Type" has lost all it's properties and values...

    Any comments are welcome!!!

    Thanks in advance....

    hsackmann

  • Can you provide some sample data for the 3 tables?

    CREATE TABLE TBL_Value_Types

    etc

    INSERT INTO TBL_Value_Types

    etc

    Do this for each of the 3 tables so the query can be tested against actual data.

    Thanks

     

  • Also take a look at this recent thread http://www.sqlservercentral.com/Forums/FindPost1472826.aspx which has some examples which may help.

     

  • Hello Steven,

    yesterday I was running a Little bit out of time... sorry...

    But here are the scrits to generate the tables:

    the first:

    USE [ID1167_LS]

    GO

    /****** Object: Table [dbo].[TBL_Value_Types] Script Date: 07/17/2013 08:46:28 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    SET ANSI_PADDING ON

    GO

    CREATE TABLE [dbo].[TBL_Value_Types](

    [TypeID] [int] IDENTITY(1,1) NOT NULL,

    [TypeName] [nvarchar](100) NOT NULL,

    [DisplayOrder] [int] NULL,

    [Value] [varchar](100) NOT NULL,

    [Timestamp_Upd] [datetime] NULL,

    [Timestamp_Add] [datetime] NOT NULL,

    CONSTRAINT [PK_TBL_Value_Types] PRIMARY KEY CLUSTERED

    (

    [TypeID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    SET ANSI_PADDING OFF

    GO

    the second:

    USE [ID1167_LS]

    GO

    /****** Object: Table [dbo].[TBL_Value_Properties] Script Date: 07/17/2013 08:43:13 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[TBL_Value_Properties](

    [PropID] [int] IDENTITY(1,1) NOT NULL,

    [TypeID_FK] [int] NOT NULL,

    [PropName] [nvarchar](100) NOT NULL,

    [DisplayOrder] [int] NULL,

    [Timestamp_Upd] [datetime] NULL,

    [Timestamp_Add] [datetime] NOT NULL,

    CONSTRAINT [PK_TBL_Value_Properties] PRIMARY KEY CLUSTERED

    (

    [PropID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    ALTER TABLE [dbo].[TBL_Value_Properties] WITH CHECK ADD CONSTRAINT [FK_TBL_Value_Properties_TBL_Value_Types] FOREIGN KEY([TypeID_FK])

    REFERENCES [dbo].[TBL_Value_Types] ([TypeID])

    GO

    ALTER TABLE [dbo].[TBL_Value_Properties] CHECK CONSTRAINT [FK_TBL_Value_Properties_TBL_Value_Types]

    GO

    the third table:

    USE [ID1167_LS]

    GO

    /****** Object: Table [dbo].[TBL_Value_Property_Values] Script Date: 07/17/2013 08:45:18 ******/

    SET ANSI_NULLS ON

    GO

    SET QUOTED_IDENTIFIER ON

    GO

    CREATE TABLE [dbo].[TBL_Value_Property_Values](

    [ValID] [int] IDENTITY(1,1) NOT NULL,

    [PropID_FK] [int] NOT NULL,

    [ValName] [nvarchar](100) NOT NULL,

    [ValValue] [nvarchar](100) NOT NULL,

    [DisplayOrder] [int] NULL,

    [Timestamp_Upd] [datetime] NULL,

    [Timestamp_Add] [datetime] NOT NULL,

    CONSTRAINT [PK_TBL_Value_Property_Values] PRIMARY KEY CLUSTERED

    (

    [ValID] ASC

    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

    ) ON [PRIMARY]

    GO

    ALTER TABLE [dbo].[TBL_Value_Property_Values] WITH CHECK ADD CONSTRAINT [FK_TBL_Value_Property_Values_TBL_Value_Property_Values] FOREIGN KEY([PropID_FK])

    REFERENCES [dbo].[TBL_Value_Properties] ([PropID])

    GO

    ALTER TABLE [dbo].[TBL_Value_Property_Values] CHECK CONSTRAINT [FK_TBL_Value_Property_Values_TBL_Value_Property_Values]

    GO

    Here comes some sample-data tab-delimited:

    For TBL_Value_Types:

    TypeIDTypeNameDisplayOrderValueTimestamp_UpdTimestamp_Add

    2Type_10810NULL2013-02-28 06:00:00.000

    3Type_21810NULL2013-02-28 00:00:00.000

    5Type_330NULL2013-07-10 13:48:23.103

    6Type_440NULL2013-07-10 13:48:36.440

    7Type_550NULL2013-07-10 13:53:40.027

    8Type_660NULL2013-07-10 13:54:54.827

    9Type_770NULL2013-07-10 13:55:10.900

    10Type_880NULL2013-07-10 13:55:29.523

    11Type_990NULL2013-07-10 13:56:00.990

    12Type_10100NULL2013-07-10 13:56:20.117

    13Type_11110NULL2013-07-10 13:56:33.780

    14Type_12120NULL2013-07-10 13:57:30.927

    15Type_13130NULL2013-07-10 13:58:13.590

    16Type_14140NULL2013-07-10 13:58:48.610

    For TBL_Value_Properties:

    PropIDTypeID_FKPropNameDisplayOrderTimestamp_UpdTimestamp_Add

    72Währungskennzeichen0NULL2013-02-28 00:00:00.000

    82Index1NULL2013-02-28 06:00:00.000

    102Typ2NULL2013-02-28 06:00:00.000

    112Ausgabejahr3NULL2013-02-28 06:00:00.000

    122Nominal4NULL2013-02-28 06:00:00.000

    142Summe5NULL2013-02-28 00:00:00.000

    153Währungskennzeichen0NULL2013-02-28 00:00:00.000

    163Index1NULL2013-02-28 00:00:00.000

    173Typ2NULL2013-02-28 00:00:00.000

    183Ausgabejahr, Code 3NULL2013-02-28 00:00:00.000

    193Nominal4NULL2013-02-28 00:00:00.000

    213Anzahl5NULL2013-07-10 14:49:31.027

    223Summe6NULL2013-07-10 14:49:52.360

    245Währungskennzeichen0NULL2013-07-10 14:52:43.577

    255Index1NULL2013-07-10 14:53:07.390

    265Qualitätsmerkmal2NULL2013-07-10 14:53:59.807

    275Nominal3NULL2013-07-10 14:54:16.770

    285Summe4NULL2013-07-10 14:54:27.507

    296Währungskennzeichen0NULL2013-07-10 14:55:25.480

    306Index1NULL2013-07-10 14:55:45.440

    316Code 2NULL2013-07-10 14:56:17.807

    326Nominal3NULL2013-07-10 14:56:42.077

    336Anzahl4NULL2013-07-10 14:56:54.800

    346Summe5NULL2013-07-10 14:57:03.003

    357Art xxx (Code 3 Buchstaben)0NULL2013-07-10 14:57:49.743

    367Index1NULL2013-07-10 14:59:36.993

    377Maß 2NULL2013-07-10 15:00:11.933

    387Masse3NULL2013-07-10 15:00:32.570

    397Anzahl4NULL2013-07-10 15:00:54.463

    407Serie - 2 Zeichen5NULL2013-07-10 15:01:11.660

    417Nummer - 7 Zeichen 6NULL2013-07-10 15:01:30.893

    428Währungskennzeichen (3 Buchstaben)0NULL2013-07-10 15:04:31.683

    438Art xxx1NULL2013-07-10 15:05:17.183

    448Probe2NULL2013-07-10 15:06:00.493

    458Index - Umlauf3NULL2013-07-10 15:06:30.863

    468Nominal6NULL2013-07-10 15:06:49.647

    478Anzahl der Münzen7NULL2013-07-10 15:07:08.363

    488Kaufpreis 1 St.8NULL2013-07-10 15:07:32.910

    498Summe9NULL2013-07-10 15:07:41.220

    508Bezeichnung10NULL2013-07-10 15:07:58.633

    518Ausgabejahr11NULL2013-07-10 15:08:10.810

    528Partienummer12NULL2013-07-10 15:08:23.077

    539Gruppe0NULL2013-07-10 15:18:32.403

    548Index - A4NULL2013-07-10 15:22:36.083

    558Index - B5NULL2013-07-10 15:23:40.273

    569Index - CNULL2013-07-10 15:24:44.317

    579Währungskennzeichen2NULL2013-07-10 15:25:16.633

    589Summe in Box A3NULL2013-07-10 15:26:39.700

    599Währungskennzeichen4NULL2013-07-10 15:26:51.060

    609Summe in Box B5NULL2013-07-10 15:26:58.230

    6110Einheitsname0NULL2013-07-10 15:27:33.630

    6210Inhaltssname1NULL2013-07-10 15:28:02.577

    6310Code2NULL2013-07-10 15:28:37.607

    6411Index0NULL2013-07-10 15:30:10.520

    6511Schlüsselart1NULL2013-07-10 15:30:46.213

    6611Code2NULL2013-07-10 15:31:53.510

    6711Anzahl3NULL2013-07-10 15:32:42.390

    6811Summe4NULL2013-07-10 15:32:50.657

    6912Wertebezeichnung0NULL2013-07-10 15:33:24.950

    7012Index1NULL2013-07-10 15:33:41.027

    7112Name2NULL2013-07-10 15:34:07.610

    7212Serienbenennung3NULL2013-07-10 15:34:26.413

    7312Anzahl4NULL2013-07-10 15:34:33.813

    7412Nominal, falls vorhanden5NULL2013-07-10 15:35:02.057

    7513Wertebezeichnung0NULL2013-07-10 15:35:20.963

    7613Serie, falls vorhanden1NULL2013-07-10 15:35:44.740

    7713Nummer, falls vorhanden2NULL2013-07-10 15:35:55.647

    7813Nominal, falls vorhanden3NULL2013-07-10 15:36:07.867

    7913Anzahl4NULL2013-07-10 15:36:14.070

    8014Code0NULL2013-07-10 15:37:05.983

    8114Wertebezeichnung1NULL2013-07-10 15:37:23.020

    8214Summe2NULL2013-07-10 15:37:50.193

    8315Art der Werte0NULL2013-07-10 15:38:40.270

    8415Anzahl der Taschen1NULL2013-07-10 15:39:26.433

    8515Gesamtbetrag2NULL2013-07-10 15:39:39.517

    8615Nr3NULL2013-07-10 15:40:16.243

    8716Art der Werte0NULL2013-07-10 15:41:03.187

    8816Anzahl der Taschen1NULL2013-07-10 15:41:18.473

    8916Gesamtbetrag2NULL2013-07-10 15:41:33.850

    For TBL_Value_Property_Values:

    ValIDPropID_FKValNameValValueDisplayOrderTimestamp_UpdTimestamp_Add

    17Kronen1110NULL2013-07-11 10:51:00.477

    48Zustand_110NULL2013-07-11 10:54:14.627

    58Zustand_221NULL2013-07-11 10:54:43.487

    610Banknoten10NULL2013-07-11 10:55:57.760

    710Münzen21NULL2013-07-11 10:56:11.797

    811199719970NULL2013-07-11 10:57:46.222

    1211200120014NULL2013-07-11 10:58:20.730

    1511200420047NULL2013-07-11 10:58:42.800

    2512Sack mit Münzen00NULL2013-07-11 11:02:51.883

    2612gesammelt gebündelte Geldscheine 551NULL2013-07-11 11:03:25.523

    2712gesammelt gebündelte Geldscheine 10102NULL2013-07-11 11:07:41.823

    2812gesammelt gebündelte Geldscheine 50503NULL2013-07-11 11:08:03.007

    2912gesammelt gebündelte Geldscheine 1001004NULL2013-07-11 11:08:12.370

    3012gesammelt gebündelte Geldscheine 5005005NULL2013-07-11 11:08:21.640

    3112gesammelt gebündelte Geldscheine 100010006NULL2013-07-11 11:08:35.440

    3212gesammelt gebündelte Geldscheine 500050007NULL2013-07-11 11:08:42.793

    3315Kronen1110NULL2013-07-11 11:11:43.407

    3416Zustand_330NULL2013-07-11 11:12:18.683

    3516Zustand_441NULL2013-07-11 11:12:32.480

    3616Zustand_552NULL2013-07-11 11:12:43.473

    3716Zustand_6 aus XXX63NULL2013-07-11 11:13:10.030

    3817Banknoten10NULL2013-07-11 11:13:48.353

    3917Münzen21NULL2013-07-11 11:13:59.053

    4018Ausgabejahr 199719970NULL2013-07-11 11:17:07.513

    4118Ausgabejahr 200120011NULL2013-07-11 11:17:18.947

    4218Ausgabejahr 200420042NULL2013-07-11 11:17:25.697

    4318XXX00003NULL2013-07-11 11:17:44.167

    4418YYY 123412344NULL2013-07-11 11:18:18.507

    4518YYY 123512355NULL2013-07-11 11:18:41.853

    4619Sack mit Münzen00NULL2013-07-11 11:20:33.933

    4719gesammelt gebündelte Geldscheine 551NULL2013-07-11 11:20:51.097

    4819gesammelt gebündelte Geldscheine 10102NULL2013-07-11 11:20:59.320

    4919gesammelt gebündelte Geldscheine 50503NULL2013-07-11 11:21:25.157

    5019gesammelt gebündelte Geldscheine 1001004NULL2013-07-11 11:21:32.200

    5119gesammelt gebündelte Geldscheine 5005005NULL2013-07-11 11:21:38.333

    5219gesammelt gebündelte Geldscheine 100010006NULL2013-07-11 11:21:45.937

    5319gesammelt gebündelte Geldscheine 500050007NULL2013-07-11 11:21:52.993

    5424US-Dollar2220NULL2013-07-11 11:22:53.223

    5524Euro3331NULL2013-07-11 11:23:10.003

    5625Zustand_A10NULL2013-07-11 11:24:32.303

    5726Merkmal_A10NULL2013-07-11 11:26:51.263

    5826Merkmal_B1NULL2013-07-11 11:27:03.010

    5926Merkmal_C32NULL2013-07-11 11:27:21.450

    6026Merkmal_D43NULL2013-07-11 11:27:43.743

    6127Sack mit Münzen00NULL2013-07-11 11:28:29.860

    6227gesammelt gebündelte GeldscheineNominal bitte eingeben1NULL2013-07-11 12:24:45.327

    6329US-Dollar2220NULL2013-07-11 12:30:02.020

    6429Euro3331NULL2013-07-11 12:30:16.817

    6530Zustand_220NULL2013-07-11 12:31:00.577

    6630Zustand_331NULL2013-07-11 12:31:50.630

    6730Zustand_42NULL2013-07-11 12:32:09.977

    6830Zustand_53NULL2013-07-11 12:32:31.923

    6931YYY 123412340NULL2013-07-11 12:34:52.453

    7031YYY 123512351NULL2013-07-11 12:35:07.410

    7132Sack mit Münzen00NULL2013-07-11 12:36:56.920

    7232gesammelt gebündelte GeldscheineNominal bitte eingeben1NULL2013-07-11 12:37:19.113

    7335GoldAUR0NULL2013-07-11 12:42:36.753

    7435SilberAg1NULL2013-07-11 12:43:33.373

    7536vortefflich10NULL2013-07-11 12:44:54.563

    7636genügend21NULL2013-07-11 12:45:11.793

    7736ungenügend32NULL2013-07-11 12:45:26.750

    7842ZZZZZZ0NULL2013-07-11 12:50:22.390

    7943SilberAg0NULL2013-07-11 12:51:15.300

    8045in Umlauf10NULL2013-07-11 13:02:33.200

    8145nicht in Umlauf21NULL2013-07-11 13:02:42.927

    8254xyz10NULL2013-07-11 13:03:10.670

    8354A/z21NULL2013-07-11 13:03:23.017

    8455vortefflich10NULL2013-07-11 13:03:57.760

    8555genügend21NULL2013-07-11 13:04:12.583

    8655ungenügend32NULL2013-07-11 13:04:24.610

    8753AAAA0NULL2013-07-11 13:32:35.680

    8853CDCD1NULL2013-07-11 13:32:57.383

    8953DEDE2NULL2013-07-11 13:33:09.437

    9056mit Bargeld10NULL2013-07-11 13:33:56.933

    9156leer21NULL2013-07-11 13:34:05.893

    9261Sendung10NULL2013-07-11 14:36:35.827

    9361Paket21NULL2013-07-11 14:37:07.420

    9461Sack32NULL2013-07-11 14:37:16.117

    9561Tasche43NULL2013-07-11 14:37:24.687

    9663YYY 123412340NULL2013-07-11 14:40:09.263

    9763YYY 123512351NULL2013-07-11 14:40:19.537

    9864Duplikate10NULL2013-07-11 14:41:35.253

    9964unverwendbar21NULL2013-07-11 14:41:53.980

    10064defekt32NULL2013-07-11 14:42:04.180

    10164funktionsfähig43NULL2013-07-11 14:42:20.950

    10265ABC010NULL2013-07-11 14:43:13.683

    10365ABD021NULL2013-07-11 14:43:24.480

    10465ABE032NULL2013-07-11 14:43:35.347

    10565ABF043NULL2013-07-11 14:43:58.210

    10665ABG054NULL2013-07-11 14:44:12.017

    10765ABFH065NULL2013-07-11 14:46:16.033

    10865ABI076NULL2013-07-11 14:46:32.500

    10966YYY 123412340NULL2013-07-11 14:48:00.213

    11066YYY 123512351NULL2013-07-11 14:48:04.917

    11170Formular10NULL2013-07-11 14:50:59.123

    11270bezahlt21NULL2013-07-11 14:51:43.973

    11370Ausschuß32NULL2013-07-11 14:51:59.790

    11480YYY 123412340NULL2013-07-11 14:53:14.760

    11580YYY 123512351NULL2013-07-11 14:53:28.237

    11683Kronen010NULL2013-07-11 14:55:33.343

    11783Devisen021NULL2013-07-11 14:55:49.427

    11883andere Werte032NULL2013-07-11 14:56:02.063

    11987Kronen010NULL2013-07-11 14:57:53.020

    12087Devisen021NULL2013-07-11 14:58:02.970

    12187andere Werte032NULL2013-07-11 14:58:13.423

    Hope this helps to solve my Problem...

    Thank you very much!!!

  • I got it!

    Thanks to your link Steven!

    It was a problem of correct ordering...

    I got it to work like this:

    SELECT

    TAG,

    Parent,

    [Type!1!Type],

    [Type!1!TypeID],

    [Type!1!TypeName],

    [Type!1!TypeValue],

    [Property!2!Type],

    [Property!2!PropID],

    [Property!2!PropName],

    [Value!3!Type],

    [Value!3!ValID],

    [Value!3!ValName],

    [Value!3!ValValue]

    FROM

    (

    -- get all Types from TBL!Value!Types

    SELECT

    1 AS TAG,

    NULL AS Parent,

    'Type' AS [Type!1!Type],

    T.TypeID AS [Type!1!TypeID],

    T.TypeName AS [Type!1!TypeName],

    T.DisplayOrder AS [Type!1!TypeDisplayOrder],

    T.Value AS [Type!1!TypeValue],

    NULL AS [Property!2!Type],

    NULL AS [Property!2!PropID],

    NULL AS [Property!2!PropName],

    NULL AS [Property!2!PropDisplayOrder],

    NULL AS [Value!3!Type],

    NULL AS [Value!3!ValID],

    NULL AS [Value!3!ValName],

    NULL AS [Value!3!ValValue],

    NULL AS [Value!3!ValDisplayOrder]

    FROM dbo.TBL_Value_Types AS T

    UNION ALL

    -- get all Properties from TBL_Value_Properties

    SELECT

    2 AS TAG,

    1 AS Parent,

    NULL AS [Type!1!Type],

    T.TypeID,

    T.TypeName,

    T.DisplayOrder,

    T.Value,

    'Property' AS [Property!2!Type],

    P.PropID AS [Property!2!PropID],

    P.PropName AS [Property!2!PropName],

    P.DisplayOrder AS [Property!2!PropDisplayOrder],

    NULL AS [Value!3!Type],

    NULL AS [Value!3!ValID],

    NULL AS [Value!3!ValName],

    NULL AS [Value!3!ValValue],

    NULL AS [Value!3!ValDisplayOrder]

    FROM dbo.TBL_Value_Types AS T

    LEFT JOIN TBL_Value_Properties AS P ON P.TypeID_FK = T.TypeID

    UNION ALL

    -- get all Values from TBL_Value_Property_Values

    SELECT

    3 AS TAG,

    2 AS Parent,

    NULL AS [Type!1!Type],

    T.TypeID,

    T.TypeName,

    T.DisplayOrder,

    T.Value,

    NULL AS [Property!2!Type],

    P.PropID,

    P.PropName,

    P.DisplayOrder,

    'Value' AS [Value!3!Type],

    V.ValID,

    V.ValName,

    V.ValValue,

    V.DisplayOrder

    FROM dbo.TBL_Value_Types AS T

    LEFT JOIN TBL_Value_Properties AS P ON P.TypeID_FK = T.TypeID

    LEFT JOIN TBL_Value_Property_Values AS V ON V.PropID_FK = P.PropID

    WHERE V.ValID IS NOT NULL

    ) AS TEMP

    ORDER BY

    [Type!1!TypeID],

    [Property!2!PropDisplayOrder],

    [Value!3!ValDisplayOrder]

    FOR XML EXPLICIT, ROOT(N'ArticleCharacteristic')

    Thank you very much for your help! 🙂

Viewing 5 posts - 1 through 4 (of 4 total)

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