Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting XML from stored procedure help?

Author  Topic 

DTFan
Yak Posting Veteran

52 Posts

Posted - 2006-01-26 : 08:16:48
OK. Well, I can't see what I'm doing wrong to return this stored procedure as an xml document but hopefully somebody else can point me to it. Basically I've got a stored procedure that creates a table, queries other tables for matching data and inserts that data into the temp table. Then I query that table, and I want to return the records as an xml document and then delete the temp table.

Here's the stored procedure I have right now:
'    stored procedure
' I use 5 parameters to run these queries

CREATE TABLE TmpTable2
(
conID BIGINT,
conString VARCHAR(150),
conDateTime SMALLDATETIME
)

-- here I run the queries on the other tables to and insert into TmpTable2 (working fine)

SELECT conID, conString, conDateTime
FROM TmpTable2
ORDER BY conDateTime DESC
FOR XML AUTO

DROP TABLE TmpTable2

Now, if I comment out the DROP TABLE statement and run the stored procedure, the resulting table looks perfect. There are three columns, with correct IDs, StringData and DateTime fields (no nulls or problems anywhere).

I initially wrote this as it returning a regular dataset/recordset. And it worked OK. The middle field (conString) is a custom string that I have to break up and run some computations on (which can't be run on the SQL Server machine). And for simple recordsets it's OK. But this query has the potential of returning more than 25,000 records - 50,000 records. Not often, but it can do that. So I read that returning large recordsets over the Net (this will be run remotely from the server) is a huge hit and it is best to do so as xml (which, as I'm sure you can tell, I'm new to).

So, here is the code I tried to run to call that recordset:

Private Sub GetReportList()

Dim builder As New SqlConnectionStringBuilder()
Dim dsInitialReportList As DataSet = New DataSet()
Dim dsReportList As New DataSet
Dim dtDateFrom As Date
Dim dtDateTo As Date
Dim dv As DataView
Dim intSQLType As Integer = 1

builder.ConnectionString = strDBConn

Using connection As New SqlConnection(builder.ConnectionString)

Dim command As New SqlCommand("admin_sp_GetReportList", connection)
Dim xmlr As System.Xml.XmlReader

command.CommandType = CommandType.StoredProcedure

command.Parameters.AddWithValue("@param1", txtParam1.Text.Trim)
command.Parameters.AddWithValue("@param2", txtParam2.Text.Trim)
command.Parameters.AddWithValue("@param3", txtParam3.Text.Trim)
command.Parameters.AddWithValue("@param4", txtParam4.Text.Trim)

Try

connection.Open()

xmlr = command.ExecuteXmlReader()
xmlr.Read()

Do While xmlr.ReadState <> Xml.ReadState.EndOfFile

System.Diagnostics.Debug.WriteLine(xmlr.ReadOuterXml())

Loop

MessageBox.Show("Reached the end. Check the output window.")

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

If connection.State <> ConnectionState.Closed Then

connection.Close()

End If

End Try

End Using

Cursor = System.Windows.Forms.Cursors.Default

dsInitialReportList = Nothing
dsReportList = Nothing

End Sub

Right now I just have it outputting the stuff (according to another thread I read about how to handle this). What I want to do is be able to get the recordset as XML (if that is indeed the best way to handle this) and then load that into another DataSet (which is handled later). Then I can start performing my splits/calculation on the middle part and us the first and third fields just as they are (ID and DateTime).

Anyway, when I run this message, I get this error:
quote:
SQLDateTime overflow. Must be between 1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

But the values that are in the table ARE between those dates/times when I look at TmpTable2 (if I remove the DROP TABLE from the stored procedure). So what would be causing that error? Is that something on the windows Form (when getting the xml document)? Do I need to declare/setup some kind of XML schema on my side before calling that stored procedure? I just can't see why the datatime wouldn't be considered "valid" when, in the table, it is accurate?

And when considering the potential number of records that could be returned, is XML the best approach? Using the other approach (not passing it as XML but just as a regular recordset) took about 90 seconds for 2,500 records (but the actual stored proc only took just under 2 seconds).

I'm using VS 2005, MSDE 2000 (SP 4), and the resulting information will be used to create a ReportViewer.

Thanks in advance for any and all help that you can give. It is much appreciated.

DTFan
Ever-hopeful programmer-in-training :)

DTFan
Ever-hopeful programmer-in-training

Kristen
Test

22859 Posts

Posted - 2006-01-26 : 14:58:16
Is it anything to do with you using a SMALLdatetime - what about if you use a regular "datetime" datatype for conDateTime instead?

Kristen
Go to Top of Page

DTFan
Yak Posting Veteran

52 Posts

Posted - 2006-01-26 : 15:43:24
quote:
Originally posted by Kristen

Is it anything to do with you using a SMALLdatetime - what about if you use a regular "datetime" datatype for conDateTime instead?

Kristen



Kristen - I tried that just now but I still got the same error. I don't understand why it is happening unless it's just me not knowing what I'm doing with XML. The tables that I am getting the information from (to put into the temp table) have the datetime stored as a SmallDateTime. And, if I remove the FOR XML part, the table that results shows the accurate records and has the field as a SmallDateTime. I'm just so confused on this.

Here's more of the code:

CREATE TABLE TmpTable2
(
conID BIGINT,
conString VARCHAR(150),
conDateTime SMALLDATETIME
)


INSERT INTO TmpTable2 (conID, conString, conDateTime)
SELECT incoming1.conID, incoming1.conDataString, incoming1.conUpdateDT
FROM incoming1
WHERE conID = @in_intConID
AND CONVERT(DATETIME, CONVERT(VARCHAR(11), incoming1.conUpdateDT, 101)) = @in_dtDateFrom
ORDER BY incoming1.conID ASC

INSERT INTO TmpTable2 (conID, conString, conDateTime)
SELECT incoming2.conID, incoming2.conDataString, incoming2.conUpdateDT
FROM incoming2
WHERE conID = @in_intConID
AND CONVERT(DATETIME, CONVERT(VARCHAR(11), incoming2.conUpdateDT, 101)) = @in_dtDateFrom
ORDER BY incoming2.conID ASC

-- I do this for 16 other tables (and unfortunately the db layout
-- can't be changed because of how the data is coming in)

SELECT conID, conString, conDateTime
FROM TmpTable2
ORDER BY conDateTime DESC
FOR XML AUTO

DROP TABLE TmpTable2


All I'm trying to do is get one properly formatted XML document that I can send back to the program (VB 2005) that is calling procedure. The strange part (or frustrating, from my point of view) is that it isn't a hard one. I mean, it's only 3 fields, two of which are varchars and one smalldatetime.

If there is something I'm doing wrong or some other way I should approach this, I am all ears.

Thank you again for the help. It is very much appreciated.

DTFan
Ever-hopeful programmer-in-training
Go to Top of Page
   

- Advertisement -