Important alert: (current site time 7/15/2013 11:52:40 PM EDT)
 

VB icon

Completely Reusable SQL Server/XML Generator !

Email
Submitted on: 2/21/2001 4:38:35 PM
By: Brandon McPherson 
Level: Intermediate
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages)
Views: 21357
author picture
(About the author)
 
     This is a really simple way of grabbing the column details for all the user tables in a SQL Server database, and converting those details to XML. If you wanted to get a little creative, you could re-use this with XSL to create SQL and ASP templates for your apps.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Completely Reusable SQL Server/XML Generator !
' Description:This is a really simple way of grabbing the column details for all the user tables in a SQL Server database, and converting those details to XML. If you wanted to get a little creative, you could re-use this with XSL to create SQL and ASP templates for your apps.
' By: Brandon McPherson
'
' Inputs:The only real 'condition' is that you should have a default database set up in your connection.
'
' Returns:XML data outlining the column information for the tables in a database.
'
' Assumes:I know this works in SQL Server 2000 and version 7.0, but I won't guarantee anything before that (I don't have copies of the system tables to check).
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=6504&lngWId=4'for details.'**************************************

<%
 Set Cnn1 = Server.CreateObject("ADODB.Connection")
 Set adoCmd = Server.CreateObject("ADODB.Command")
 Dim sXML
' Set the dsn up to a database
 Cnn1.Open "DSN=nwind;UID=sa;PWD="
 
 Set adoCmd.ActiveConnection = Cnn1
 
' I'm sure there's a much more graceful way to do this....
 adoCmd.CommandText = "select TableName=t1.[name], ColumnName=c1.[name], datatype=(select a1.[name] from dbo.systypes as a1 where a1.xusertype = c1.xusertype), c1.isnullable, c1.length, c1.colid from dbo.syscolumns as c1 inner join dbo.sysobjects as t1 on c1.id = t1.id where t1.xtype = 'U'"
 
 Set tmpRST = adoCmd.Execute
 sXML = "<?xml version=""1.0""?><tables>"
 
 
 Dim sTable
 Dim iColCount
 Dim intIterator
 
 
 iColCount = tmpRST.Fields.Count - 1
 sTable = tmpRST.Fields("TableName")
 
 sXML = sXML & "<table><name>" & tmpRST("TableName") & "</name>"
 Do While Not tmpRST.EOF
If tmpRST.Fields("TableName") <> sTable Then
 sXML = sXML & "</table><table><name>" & tmpRST.Fields("TableName") & "</name>"
End If
sXML = sXML & "<column>"
For intIterator = 1 To iColCount
 sXML = sXML & "<" & tmpRST.Fields(intIterator).Name & ">" & tmpRST.Fields(intIterator) & "</" & tmpRST.Fields(intIterator).Name & ">"
Next
sXML = sXML & "</column>"
sTable = tmpRST.Fields("TableName")
tmpRST.MoveNext
 Loop
 sXML = sXML & "</table></tables>"
 Response.Write sXML
%>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/21/2001 4:40:09 PMBrandon McPherson

An added note: the tag capitalization is happening in PSC's code formatter. You'll need to change the tags to lowercase when you run the demo. And as always, please vote if you like it! :)
(If this comment was disrespectful, please report it.)

 
2/21/2001 7:43:48 PMChris Maynard

That's cool! =D
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.