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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 For XML

Author  Topic 

frans@fairheads.com
Starting Member

2 Posts

Posted - 2011-03-03 : 07:05:21
Hi

I'm batteling to map relational data as xml data. I would like to get the data as below with out using EXPLICIT. Is this possible. I'm baning my head against the wall on this javascript:insertsmilie('')



<audit>
<table name="client" pkValue ="4402">
<ID>
<old>4402</old>
<new>4402</new>
</ID>
<firstname>
<old>FraNs</old>
<new>Frans</new>
</firstname>
<surname>
<old>coetzee</old>
<new>Coetzee</new>
</surname>
</table>
</audit>




create table #clientOLD(ID int, firstname varchar(50), surname varchar(50))
create table #clientNew(ID int, firstname varchar(50), surname varchar(50))

insert into #clientOLD
select 4402, 'FraNs', 'coetzee'

insert into #clientNew
select 4402, 'Frans', 'Coetzee'

select 'client' as [name], ID as 'pkValue',*
from
(select 'OLD' as value,* from #clientOLD
union all
select 'NEW' as value,* from #clientNew) tmp
order by tmp.id, tmp.value
for xml raw('table'), root('AUDIT'), type


--drop table #clientOLD, #clientNew

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-03 : 08:50:15
I agree with you on staying away from EXPLICIT clause. It is deprecated and will be removed in a future version of SQL. But, I prefer PATH clause over RAW. RAW is useful when you want the structure of your XML to be dictated by the structure of your table/query. It does not seem like that is what you want to do here. Besides, it does not let you specify some columns as attributes and some others as elements, which you do want to do.

For using the PATH clause, first get the data as a record set (without any XML formatting). For example, like this:
select
'client',
a.ID,
'OLD',
'NEW',
a.firstname,
b.firstname,
a.surname,
b.surname
from
#clientOLD a
inner join #clientNew b on b.id = a.id
Once you are able to get the data as a record set, the PATH clause makes it very easy to format the data - simply specify the element/attribute structures as aliases shown below and specify the path options.
select
'client' as [@name],
a.ID as '@pkValue',
'OLD' as [ID/old],
'NEW' as [ID/new],
a.firstname as [firstname/old],
b.firstname as [firstname/new],
a.surname as [surname/old],
b.surname as [surname/new]
from
#clientOLD a
inner join #clientNew b on b.id = a.id
for xml
path('table'),
root('AUDIT'),
type

Go to Top of Page

frans@fairheads.com
Starting Member

2 Posts

Posted - 2011-03-03 : 10:14:45
Hi
Thanx, for the help and the way you explained your solution was very easy to understand.

Just so that u know i'm trying to implement this as an audit trigger on tables. One of the probs i have is that each field will have to be "hard coded" with its elements/attribute mapping to xml. I could build up a dynamic piece of SQL string buy using the information_schema.columns
but the prob with that is that inserted and deleted tables in triggers will not be accessable. ARGGGG
I've thought of writing the inserted and deleted tables to "temp" permanate tables prefix with uniqie id..... but that just seems so long winded and slow.... sigh any suggestions



Thanx again for the help, my understanding of the for xml is a lill better now.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-03 : 13:19:54
It may be possible to create a trigger on the table(s) you want to audit as follows:
create Trigger dbo.TestTrigger
on dbo.YourTableName
for update, insert, delete
as

DECLARE @xmlvar xml;

SELECT @xmlvar = isnull((SELECT * FROM deleted FOR XML AUTO), '<DELETED RECORD>');

SELECT @xmlvar = @xmlvar + isnull((SELECT * FROM inserted FOR XML AUTO), '<INSERTED RECORD>');

insert into YourAuditTable values (@xmlvar);

The beauty of AUTO clause is that the XML will have the table name. This way, you could apply the same trigger code to any of the tables.

To find the changes to data in a specific table, you will need to search the entire audit table. But you can always expand on this to add additional columns for table name, time stamp, user etc. to make it easier to search.

I have not tested or tried this, so....
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-03-03 : 15:56:27
ehe...most people I know battle to get XML data as relational

"Watch out for the rocks ahead"

And I do mean rocks

THE XML community has NO desire to know or understand the relational model



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -