i m trying to add the xml schema to the columns of my table. what i would to do is:
- extract schema from a query
- add the schema to column
- insert xml to this column
my problem is how to exclude some options in the schema, like the possibility to don t check empty elements.
so, for example:
create table #t1 (c1 int not null,c2 int null)
select CONVERT(xml,(select *
from #t1
where 0=1
for xml raw,elements,XMLSCHEMA('XXX')
))
this get me
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="XXX" elementFormDefault="qualified">
<xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="row">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="c1" type="sqltypes:int" />
<xsd:element name="c2" type="sqltypes:int" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
how can define all elements with "minOccurs="0""??
thank you!
There is - for sure! - no easy going! The engine is obliged to return the schema as it was found. You might take the schema as an
XML
and modify it, you might use a string based approach (or some kind of regex). The following is a quirky, but working dynamic approach:The procedure sp_describe_first_result_set is used to get meta data of an unknown result set. You can store its return in a table.
Using the information you can create a copy of your table, where all columns are nullable. Use this new table to get your schema:
--Your use case
The result