VB icon

Completely Reusable SQL Server/XML Generator !

Email
Submitted on: 1/5/2015 12:24:00 PM
By: Brandon McPherson (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: ASP (Active Server Pages)
Views: 764
 
     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!
				
'**************************************
' 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 (from psc cd)
'
' 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).
'**************************************

<%
 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


 There are no comments on this submission.
 

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.