I’d like to extract an XML schema from a database (a “database schema”) using a T-SQL query in SQL management studio, rather than C# code. The schema should include all tables in the database.
I can get a single table using the following:
DECLARE @schema xml
SET @schema = (SELECT * FROM Student FOR XML AUTO, ELEMENTS, XMLSCHEMA('StudentSchema'))
select @schema
But how do I combine the remaining tables? All of my tables are related in some way.
See also this question, which discusses how to do it in code.
You could alway use
sp_msforeachtableand dynamic sql:You may have a naming issue with the schema name since I think you will have
dbo.or schema prefix to the table name.