В ожидании реального сценария, вот хороший старт для вас. В результате он создает коллекцию схем XML с именем dbo.StateAndCities .
SQL
USE tempdb;
GO
-- DDL and sample data population, start
IF EXISTS (SELECT * FROM sys.xml_schema_collections
WHERE name = N'StateAndCities'
AND schema_id = SCHEMA_ID(N'dbo'))
DROP XML SCHEMA COLLECTION dbo.StateAndCities;
DECLARE @tbl TABLE (
ID INT IDENTITY PRIMARY KEY
, state CHAR(2)
, city VARCHAR(30)
);
INSERT INTO @tbl (state, city)
VALUES
('FL', 'Miami')
, ('CA', 'Los Angeles')
, ('TX', 'Austin');
-- DDL and sample data population, end
DECLARE @xml XML
, @XSD XML;
-- Generate XML plus embedded XSD schema
SET @xml = (SELECT NULL,
(
SELECT *
FROM @tbl AS [row]
FOR XML AUTO, ELEMENTS, TYPE, XMLSCHEMA('MyURI'))
FOR XML PATH(''), TYPE, ROOT('root')
);
-- just to see, XML plus embedded XSD schema
SELECT @xml;
-- retrive just XSD
;WITH xmlnamespaces ('http://www.w3.org/2001/XMLSchema' AS xsd)
SELECT @xsd = (SELECT @xml.query('/root/xsd:schema'));
-- just to see, XSD schema
SELECT @xsd AS xsd;
-- create schema collection
CREATE XML SCHEMA COLLECTION dbo.StateAndCities AS @xsd;