Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
soldierfc
Starting Member
5 Posts |
Posted - 2011-04-12 : 19:14:54
|
| Is there anyone out there that can figure this out? Question a. Write a stored procedure to return the following XML output<EmployeeSupervisors> <EmployeeSupervisor> <EmployeeSupervisiorID>1</EmployeeSupervisiorID> <EmployeeSupervisorName>Chris</EmployeeSupervisorName> <Employees> <Employee> <EmployeeID>10</EmployeeID> <EmployeeName>Joseph</EmployeeName> <EmployeeTypeID>3</EmployeeTypeID> </Employee> <Employee> <EmployeeID>11</EmployeeID> <EmployeeName>John</EmployeeName> <EmployeeTypeID>1</EmployeeTypeID> </Employee> </Employees> </EmployeeSupervisor></EmployeeSupervisors> |
|
|
soldierfc
Starting Member
5 Posts |
Posted - 2011-04-12 : 19:18:43
|
| Do I need a CTE, recursion, or can I just use XML functions to Get the desired output? I have already put together 3 tables.Employees, EmployeeSupervisors, and EmployeeType |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-12 : 20:27:21
|
| I don't think you need recursive CTE to do this. Do it in two steps:1. Write a query to select all the columns that are present in the XML. In this case it would be EmployeeSupervisiorID, EmployeeSupervisorName, EmployeeID, EmployeeName, and EmployeeTypeID.2. Use one of the XML constructs (xml auto, xml raw, xml path or xml explicit) to convert the data to XML. I would recommend XML PATH in this case.If you run into difficulties in shaping the XML the way you want, post the query you do have and I am sure some of the people on this forum would be able to help you. |
 |
|
|
soldierfc
Starting Member
5 Posts |
Posted - 2011-04-12 : 21:07:45
|
| Didn't work:Select EmployeeID, EmployeeName,EmployeeTypeID,tblEmployee.EmployeeSupervisorID, EmployeeSupervisorName from tblEmployee join EmployeeSupervisors on tblEmployee.EmployeeSupervisorID = EmployeeSupervisors.EmployeeSupervisorID Where EmployeeSupervisors.EmployeeSupervisorID = 1 FOR XML AUTO, TYPE, ELEMENTS, ROOT('Supervisors')the output was:<Supervisors> <tblEmployee> <EmployeeID>1</EmployeeID> <EmployeeName>Chris</EmployeeName> <EmployeeTypeID>1</EmployeeTypeID> <EmployeeSupervisorID>1</EmployeeSupervisorID> <EmployeeSupervisors> <EmployeeSupervisorName>Chris</EmployeeSupervisorName> </EmployeeSupervisors> </tblEmployee> <tblEmployee> <EmployeeID>10</EmployeeID> <EmployeeName>Joeseph</EmployeeName> <EmployeeTypeID>3</EmployeeTypeID> <EmployeeSupervisorID>1</EmployeeSupervisorID> <EmployeeSupervisors> <EmployeeSupervisorName>Chris</EmployeeSupervisorName> </EmployeeSupervisors> </tblEmployee> <tblEmployee> <EmployeeID>11</EmployeeID> <EmployeeName>John</EmployeeName> <EmployeeTypeID>1</EmployeeTypeID> <EmployeeSupervisorID>1</EmployeeSupervisorID> <EmployeeSupervisors> <EmployeeSupervisorName>Chris</EmployeeSupervisorName> </EmployeeSupervisors> </tblEmployee></Supervisors> |
 |
|
|
soldierfc
Starting Member
5 Posts |
Posted - 2011-04-12 : 21:16:10
|
| The nature of the XML seems to be the results of a query that returns all the employees for a particular supervisor. I created the tables with foreign keys. |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-12 : 21:41:58
|
| You used XML AUTO, which is much less flexible than XML PATH. You will need XML PATH here.I am hesitating to post the query because the way you framed the question it sounded as though this was for a class project or homework or something. If that is not the case please say so, and I will gladly post the query |
 |
|
|
soldierfc
Starting Member
5 Posts |
Posted - 2011-04-12 : 22:59:16
|
| It's not for a class project at all. I am just practicing for an interview, and this is 1 of the questions. |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-04-13 : 08:23:54
|
Sorry about making a judgement where one was probably not called for. I did not mean to sound condescending or offensive.In any case, here is one way of doing it - using XML PATH. I am pretty certain it can be done using XML AUTO as well, but I am less familiar with it.All I am doing is nesting the selects to match the structure of your XML. When FOR XML clause is specified, you can nest the selects as I have done here, and you can even add order by clause within the nested selects (which I have not done).Also, when you work with XML, keep in mind that pretty much everything is case-sensitive.-- TEST DATA--------------------create TABLE #tmp ( EmployeeID int, EmployeeName VARCHAR(255), EmployeeTypeID int, EmployeeSupervisorID int, EmployeeSupervisorName VARCHAR(255));INSERT INTO #tmp VALUES (10,'Joseph',3,1,'Chris');INSERT INTO #tmp VALUES (11,'John',1,1,'Chris');-- QUERY --------------------WITH A AS -- because of the way the elements need to be nested, we need distinct supervisors( SELECT DISTINCT EmployeeSupervisorID, EmployeeSupervisorName FROM #tmp)SELECT -- creating EmployeeSupervisor element. a.EmployeeSuperVisorId AS [EmployeeSuperVisorId], a.EmployeeSupervisorName AS [EmployeeSupervisorName], ( SELECT -- nesting employees element for each supervisor. ( SELECT -- nesting the employees under employees element b.EmployeeId AS [EmployeeID], b.EmployeeName AS [EmployeeName], b.EmployeeTypeID AS [EmployeeTypeID] FROM #tmp b WHERE a.EmployeeSuperVisorId = b.EmployeeSuperVisorId FOR XML PATH('Employee'),TYPE ) AS [Employees] FOR XML PATH(''), TYPE ) FROM aFOR XML PATH('EmployeeSupervisor'),ROOT('EmployeeSupervisors');-- CLEANUP----------------------drop TABLE #tmp |
 |
|
|
|
|
|
|
|