declare @T table
(
tableName varchar(20),
uqName varchar(20),
col1 varchar(10),
col2 varchar(10)
)
insert into @T values
('TableName', 'UniqueKeyName', null, 'ABC')
insert into @T values
('TableName', 'UniqueKeyName', null, null)
insert into @T values
('TableName', 'UniqueKeyName', '123', '456')
select uqName as "@Name",
tableName as "@TableName",
(select 'col1' as "@Name",
case when col1 is null then 'true' end as "@IsNull",
col1 as "*"
for xml path('Col'), type),
(select 'col2' as "@Name",
case when col2 is null then 'true' end as "@IsNull",
col2 as "*"
for xml path('Col'), type)
from @T
for xml path('UniqueKey')
Результат:
<UniqueKey Name="UniqueKeyName" TableName="TableName">
<Col Name="col1" IsNull="true" />
<Col Name="col2">ABC</Col>
</UniqueKey>
<UniqueKey Name="UniqueKeyName" TableName="TableName">
<Col Name="col1" IsNull="true" />
<Col Name="col2" IsNull="true" />
</UniqueKey>
<UniqueKey Name="UniqueKeyName" TableName="TableName">
<Col Name="col1">123</Col>
<Col Name="col2">456</Col>
</UniqueKey>