Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 16th, 2007, 09:50 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default CREATE CUSTOM XML WITHIN STORED PROC

Hi,

I have been using the OPENXML function in SQL to insert, update and delete records. Now, I have a requirement to create a custom hierarchical XML within a Stored Proc but I cannot figure out how to do that.

Consider two tables: Employees and Departments

Employees: EmployeeID, EmployeeName, EmployeeSalary, EmployeeDetails, DeptID

Departments: DeptID, DeptName

(EmployeeID and DeptID are entity fields)

This is what the XML should look like:

<RECORD>

 <DEPTID DEPTID="1">
  <EMPLOYEE EMPID=”..” NAME=".." SALARY=".." DEPTNAME="..">
    <DETAILS>...<DETAIL>
  </EMPLOYEE>
  <EMPLOYEE EMPID=”..” NAME=".." SALARY=".." DEPTNAME="..">
    <DETAILS>...<DETAIL>
  </EMPLOYEE>
 </DEPTID>

 <DEPTID DEPTID="2">
  <EMPLOYEE EMPID=”..” NAME=".." SALARY=".." DEPTNAME="..">
    <DETAILS>...<DETAIL>
  </EMPLOYEE>
  <EMPLOYEE EMPID=”..” NAME=".." SALARY=".." DEPTNAME="..">
    <DETAILS>...<DETAIL>
  </EMPLOYEE>
 </DEPTID>

</RECORD>

I have created the select query that returns the recordset like that:

SELECT e.EmployeeID, e.EmployeeName, e.EmployeeSalary, e.EmployeeDetail, e.DeptID, d.DeptName
FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID
ORDER BY e.DeptID

I would appreciate your help.

Cheers

CPALL
 
Old April 16th, 2007, 10:31 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You will need to use a FOR XML query. Are you using SQL Server 2000 as the forum indicates? If so then I think you'll need FOR XML EXPLICIT which is a real pain, with SQL 2005 you can use FOR XML PATH which is easier.

--

Joe (Microsoft MVP - XML)
 
Old April 17th, 2007, 06:36 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Joe,

I am using SQL Server 2000. Do you have any ideas on how to do that ?

Cheers
 
Old April 17th, 2007, 06:43 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

I have got a good start using this code below. However, I have a big problem. Since the Employees.EmployeeDetail field is of ntext type, I cannot use the keyword DISTINCT with it.

Does anyone know how to get around this?

I need this field to be ntext type because I will be storing xml in there.

SELECT DISTINCT
            Tag = 1,
            Parent = NULL,
            [deptid!1!id] = d.DeptID,
            [employee!2!id] = NULL,
            [employee!2!name] = NULL,
            [employee!2!salary] = NULL,
            [employee!2!Detail] = NULL,
            [employee!2!deptid] = NULL
FROM dbo.Departments d INNER JOIN dbo.Employees e ON d.DeptID = e.DeptID
 
Old April 17th, 2007, 06:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Then you should be doing a Distinct excluding EmployeeDetail column and do a join with the Employees table on EmpoyeeID to get a work around for this.

Moreover I dont understand why you do a join there when you set all the other columns to NULL except the deptid.
_________________________
- Vijay G
Strive for Perfection
 
Old April 20th, 2007, 09:49 AM
Friend of Wrox
 
Join Date: Oct 2003
Posts: 290
Thanks: 24
Thanked 0 Times in 0 Posts
Default

Hi Vijay G,

Many thanks for your reply.

This is the full working code if I use a nvarchar type field to store an xml. However, the nvarchar type is too small to store xml. Therefore, I will have to change it back to ntext but as I said before, with ntext I cannot use the distinct function.

I did not quite understand your suggestion. I would appreciate if you could have a look at my code and make the necessary changes for the distinct to work.

Cheers

CPALL

SELECT DISTINCT
            Tag = 1,
            Parent = NULL,
            [deptid!1!id] = d.DeptID,
            [employee!2!id] = NULL,
            [employee!2!name] = NULL,
            [employee!2!salary] = NULL,
            --[employee!2!Detail] = NULL,
            [employee!2!!xml] = NULL,
            [employee!2!deptid] = NULL

FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID

UNION ALL

SELECT DISTINCT
            Tag = 2,
            Parent = 1,
            [deptid!1!id] = d.DeptID,
        [employee!2!id] = e.EmployeeID,
            [employee!2!name] = e.EmployeeName,
            [employee!2!salary] = e.EmployeeSalary,
        e.EmployeeDetail as [employee!2!!xml],
            [employee!2!deptid] = e.DeptID

FROM Employees e INNER JOIN Departments d ON e.DeptID = d.DeptID

ORDER BY [deptid!1!id], [employee!2!deptid], [employee!2!!xml]





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Proc that returns a value elygp SQL Server 2000 4 May 9th, 2007 01:05 AM
Calling an insert stored proc from a select stored dzitam SQL Language 10 April 2nd, 2007 12:39 PM
How to get value from stored proc busybee ASP.NET 1.0 and 1.1 Basics 4 April 2nd, 2006 01:06 AM
Very Complicated Stored Proc monfu SQL Server 2000 7 November 29th, 2005 09:02 PM
Simplify the stored proc stephanel SQL Server 2000 3 August 5th, 2003 08:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.