SQL Server For XML Explicit
I am trying to use SQL Server to create my XML for me for certain applications. I have written a query that works perfect in Query Analyzer. However, when I try to use ASP to call the stored procedure with this query, everything goes bust. I've tried to loop through the recordset and response.write out each row, I've created a stream object, streamed from my command object into it, and then tried loading it into an XML object (which gives me an invalid character error), and I've tried streaming the results directly to the response object. In every case, I receive either no results on the screen, or in some cases I receive a Greek Letter Beta. I'm not sure exactly what I'm doing wrong, but I've read a buncjh of the articles available and I get the same results every time.
Below are the query and the asp code I used, The query attempts to get table and column information from the database and place it into XML. So I should get results like:
<rootElement>
<table name="tablename">
<column name="col1"/>
<column name="col2"/>
<column name="col3"/>
</table>
.
.
.
</rootElement>
Please HELP!!!
***********************************************
***********************************************
***********************************************
SELECT 1 as Tag,
NULL as Parent,
'RootElement' as [rootElement!1!elementname],
NULL as [table!2!tName],
NULL as [table!2!sysid],
NULL as [column!3!cName],
NULL as [column!3!type],
NULL as [column!3!length],
NULL as [column!3!isNullable],
NULL as [column!3!isIdentity],
NULL as [column!3!isPrimaryKey],
NULL as [column!3!isForeignKey],
NULL as [column!3!fkTable],
NULL as [column!3!fkColumn]
UNION ALL
SELECT 2 as Tag,
1 as Parent,
'RootElement' as [rootElement!1!elementname],
so.Name as [table!2!tName],
so.id as [table!21!sysid],
NULL as [column!3!cName],
NULL as [column!3!type],
NULL as [column!3!length],
NULL as [column!3!isNullable],
NULL as [column!3!isIdentity],
NULL as [column!3!isPrimaryKey],
NULL as [column!3!isForeignKey],
NULL as [column!3!fkTable],
NULL as [column!3!fkColumn]
FROM SysObjects so
WHERE so.xtype = 'U' AND
NOT LEFT(so.Name, 2) Like 'dt'
UNION ALL
SELECT 3,
2,
'RootElement' as [rootElement!1!elementname],
so.Name,
so.id,
sc.Name,
(SELECT NAME FROM SysTypes WHERE xusertype = sc.xusertype),
sc.length,
sc.isNullable,
(CASE columnproperty(sc.id, sc.Name, 'IsIdentity')
WHEN 1 THEN 'Yes' ELSE 'No'
END),
(CASE sc.typestat
WHEN 1 THEN 'Yes' ELSE 'No'
END),
(CASE
WHEN sf.fkey1 IS NULL THEN 'No' ELSE 'Yes'
END),
(SELECT Name FROM SysObjects WHERE id = sf.rkeyid),
(SELECT Name FROM SysColumns WHERE id = sf.rkeyid AND colid=sf.rkey1)
FROM SysColumns sc
INNER JOIN SysObjects so ON sc.id = so.id
LEFT JOIN SysReferences sf ON so.id = sf.fkeyid AND
sc.colid = sf.fkey1
ORDER BY [table!2!tName], [column!3!cName]
FOR XML EXPLICIT
***********************************************
***********************************************
***********************************************
ASP CODE:
<%
On Error Resume Next
dim objStream
dim cn
dim com
dim objXML
set cn = Server.CreateObject("ADODB.Connection")
set com = Server.CreateObject("ADODB.Command")
cn.Open ODBCConnectString 'CONST variable from Globals2.asp
cn.CursorLocation = adUseClient
com.ActiveConnection = cn
com.CommandType = adCmdStoredProc
com.CommandText = "dbo.spGetTableXML" --Stored Procedure on Server.
com.Properties("Output Stream").Value = Response
com.Execute ,, adExecuteStream
%>
|