views:

1598

answers:

3

Hello everyone,

I am using SQL Server 2005. Is there any command or GUI tool (e.g. any menu/function from SQL Server management studio) to convert database table into XML schema file (.xsd)?

thanks in advance, George

+2  A: 

I've found this. Give it a try

Select CourseID, Name, CoursePrice
FROM CouseMaster.Course Course
FOR XML AUTO, XMLSCHEMA
Mork0075
@Mork0075, 1. CourseID, Name, CoursePrice are the column names? 2. I think CouseMaster.Course is the table name, but why you add an additional Course after CouseMaster.Course Course?
George2
This was only a copy and paste snippet. The select and from are not really important, the FOR XML AUTO, XMLSCHEMA should be.
Mork0075
Cool, I am using "Select CourseID, Name, CoursePriceFROM CouseMaster.Course FOR XML AUTO, XMLSCHEMA" and it works. The last question, the additiona Course after CouseMaster.Course means?
George2
I dont know, perhaps its a typo and should be FROM x AS y
Mork0075
@Mork0075, how to output schema to a file?
George2
This depends on your enviroment. If the run the command on a shell, you should be able to pipe it into a file.
Mork0075
Currently I run it in SQL Server 2005 Management Studio, how to run it from shell?
George2
A: 

You can write to file like this:

bcp.exe "select top 0 * from (select 1 as iCol) as t for xml auto, xmlschema" queryout outfile.xsd -T -c

I'm Using the TOP 0 to exclude the xml of the actual query data since you only want the schema. The -c causes it to be plain character data in the output, use -w instead if you want utf-16 (unicode) output.

EDIT - and if you want to change the xml structure, look at PATH with FOR XML.

A: 

Declare @SQL nvarchar(1000) SET @SQL= 'bcp.exe '+ '"select * from yourdbname.yourschema.yourtablename for xml path (''record''), ROOT (''tabel'')"' +' queryout '+ 'c:\yourfilename.xsd' +' -w -r -t -SyourServerName -T'

print @SQL EXEC Master..xp_CmdShell @SQL

Replace allvalues starts with 'your', accordingly

rmdussa