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 2012 Forums
 Transact-SQL (2012)
 XML Query Help row with no data

Author  Topic 

IK1972

56 Posts

Posted - 2014-07-31 : 10:59:57

declare @address table
(
AddressID int,
AddressType varchar(12),
Address1 varchar(20),
Address2 varchar(20),
City varchar(25),
AgentID int
)
insert into @address
select 1, 'Home', 'abc', 'xyz road', 'RJ', 1 union all
select 2, 'Office', 'temp', 'ppp road', 'RJ', 1 union all
select 3, 'Home', 'xxx', 'aaa road', 'NY', 2 union all
select 4, 'Office', 'ccc', 'oli Com', 'CL', 2 union all
select 5, 'Temp', 'eee', 'olkiu road', 'CL', 2 union all
select 6, 'Home', 'ttt', 'loik road', 'NY', 3

SELECT a.* from @address a
where a.AddressID = 1
FOR XML path('Addresses')

SELECT a.* from @address a
where a.AddressID = 9
FOR XML path('Addresses')

Issue:
As you can see for second query where AddressID = 9 is not exists so xml is not generated but my expected result is for second query is

<Addresses>
<AddressID />
<AddressType />
<Address1 />
<Address2 />
<City />
<AgentID />
</Addresses>

Thanks in advance for all your help.
   

- Advertisement -