473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Server 200 XML Parsing Cursor Problem

radcaesar
759 Expert 512MB
Hi,
I am reading a xml file using T-SQL. I want the xml data to use in openxml. So i was using cursor here.

Here i am concatenating the xml data into a variable called @FileContents. When i print this variable content inside the WHILE FetchStatus of CURSOR, its printing fine. If i print after the cursor execution s over, it doen't shows anything and its blank.

Where am i missing ?


DECLARE @FileName varchar(255)
DECLARE @ExecCmd VARCHAR(255)
DECLARE @y INT
DECLARE @x INT
DECLARE @FileContents VARCHAR(8000)
DECLARE @idoc int

CREATE TABLE #tempXML(PK INT NOT NULL IDENTITY(1,1), ThisLine VARCHAR(255))

SET @FileContents = ''
SET @FileName = 'd:\CCCLoginConfig.xml'
SET @ExecCmd = 'type ' + @FileName

INSERT INTO #tempXML EXEC master.dbo.xp_cmdshell @ExecCmd

DECLARE @AccountID varchar(8000)
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR select ThisLine from #tempxml
OPEN @getAccountID
FETCH NEXT FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @FileContents = @FileContents + cast(@AccountID as varchar(8000))
FETCH NEXT FROM @getAccountID INTO @AccountID
--print @FileContents
END
CLOSE @getAccountID
DEALLOCATE @getAccountID

select @FileContents
EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents

SELECT * FROM OPENXML (@idoc,'/DTSConfiguration/ConfiguredValue ',2) WITH (ConfiguredValue varchar(10))
-- ContactName varchar(20))

DROP TABLE #tempXML
Oct 20 '07 #1
12 5990
Jim Doherty
897 Expert 512MB
Hi,
I am reading a xml file using T-SQL. I want the xml data to use in openxml. So i was using cursor here.

Here i am concatenating the xml data into a variable called @FileContents. When i print this variable content inside the WHILE FetchStatus of CURSOR, its printing fine. If i print after the cursor execution s over, it doen't shows anything and its blank.

Where am i missing ?


DECLARE @FileName varchar(255)
DECLARE @ExecCmd VARCHAR(255)
DECLARE @y INT
DECLARE @x INT
DECLARE @FileContents VARCHAR(8000)
DECLARE @idoc int

CREATE TABLE #tempXML(PK INT NOT NULL IDENTITY(1,1), ThisLine VARCHAR(255))

SET @FileContents = ''
SET @FileName = 'd:\CCCLoginConfig.xml'
SET @ExecCmd = 'type ' + @FileName

INSERT INTO #tempXML EXEC master.dbo.xp_cmdshell @ExecCmd

DECLARE @AccountID varchar(8000)
DECLARE @getAccountID CURSOR
SET @getAccountID = CURSOR FOR select ThisLine from #tempxml
OPEN @getAccountID
FETCH NEXT FROM @getAccountID INTO @AccountID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @FileContents = @FileContents + cast(@AccountID as varchar(8000))
FETCH NEXT FROM @getAccountID INTO @AccountID
--print @FileContents
END
CLOSE @getAccountID
DEALLOCATE @getAccountID

select @FileContents
EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents

SELECT * FROM OPENXML (@idoc,'/DTSConfiguration/ConfiguredValue ',2) WITH (ConfiguredValue varchar(10))
-- ContactName varchar(20))

DROP TABLE #tempXML
Having replicated this my end I suspect you're encountering a concatenation of a null that is cancelling out the variable assignment.

I have placed the 'print' after the deallocation at my end and it works with this amendment

Expand|Select|Wrap|Line Numbers
  1.  
  2. WHILE @@FETCH_STATUS = 0
  3. BEGIN
  4. SET @FileContents = ISNULL(@FileContents + cast(@AccountID as varchar(8000)),@FileContents)
  5. FETCH NEXT FROM @getAccountID INTO @AccountID
  6. --print len(@FileContents)
  7. END
  8. CLOSE @getAccountID
  9. DEALLOCATE @getAccountID
  10. print @FileContents
  11.  

Regards

Jim :)
Oct 20 '07 #2
iburyak
1,017 Expert 512MB
If Jim is right and you have null at the end then do following:


Expand|Select|Wrap|Line Numbers
  1. SET @FileContents = @FileContents + isnull(cast(@AccountID as varchar(8000)),'')
Good Luck.
Oct 20 '07 #3
iburyak
1,017 Expert 512MB
I hate cursors.

Maybe you want to try reading whole file at once.
I don't have time changing this code for you (going on vacation for couple of weeks) but you can try inserting into a variable instead of a table or read after initial insert into a variable.

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE SomeTable (XML varchar(8000) null)
  2.  
  3. BULK INSERT [SomeTable]
  4. FROM '\\nyinfrep001\ftproot$\Import\test.xml'

Good Luck.
Oct 20 '07 #4
Jim Doherty
897 Expert 512MB
If Jim is right and you have null at the end then do following:


Expand|Select|Wrap|Line Numbers
  1. SET @FileContents = @FileContents + isnull(cast(@AccountID as varchar(8000)),'')
Good Luck.
If I am right Iburyak is there something I have missed? in the While loop? does not the isnull function return the current non null value of Filecontent 'as is' without the necessity for appending a zero length string as well?

Regards

Jim :)
Oct 20 '07 #5
iburyak
1,017 Expert 512MB
If I am right Iburyak is there something I have missed? in the While loop? does not the isnull function return the current non null value of Filecontent 'as is' without the necessity for appending a zero length string as well?

Regards

Jim :)
I am not sure it is a question or a statement.
If you add null to a string you get null, if you replace that null with an empty string you get your string as is. It should work the way you wrote it and the way i wrote it but it is a mater of programmers preference. I don't think it is necessary appending null to a string and then checking if it is not what you wanted to get and then replacing it with original string. I prefer not to make unnecessary manipulations and check what I add to the string and if it is null not to do it.
If you ask me I wouldn't write this while loop in a first place. See my second suggestion which in my opinion is more efficient.


Nice talking to you Jim.... :)
By the way where are you from?
Oct 21 '07 #6
Jim Doherty
897 Expert 512MB
I am not sure it is a question or a statement.
If you add null to a string you get null, if you replace that null with an empty string you get your string as is. It should work the way you wrote it and the way i wrote it but it is a mater of programmers preference. I don't think it is necessary appending null to a string and then checking if it is not what you wanted to get and then replacing it with original string. I prefer not to make unnecessary manipulations and check what I add to the string and if it is null not to do it.
If you ask me I wouldn't write this while loop in a first place. See my second suggestion which in my opinion is more efficient.


Nice talking to you Jim.... :)
By the way where are you from?
I gave up making statements eons ago in my teens LOL! so no it was a question to a fellow programmer on a question of specific mindset consideratons thats all :) you've answered it 'both' isnulls perform in exactly the same way.
the cursor useage wouldnt be my preference either I have to say but there you go thats differences for you :)) The poster seems to be interested as to 'why' it wasnt working so I dealt with it 'I think' in the context of his question as opposed to benchmarking performance differences.

Nice talking to you too and I am in the UK, the midlands, in the leafy suburbs of Derbyshire

Jim :)
Oct 21 '07 #7
iburyak
1,017 Expert 512MB
I am in NY. My company has a branch in UK and I work with Londoners every day... :)
English is not my first language and I possibly misunderstood your question.
So good you've got what you were looking for from my answer... :)
Sometimes I do what you did just respond to specific problem and not go into changing whole approach to solution. But this case seem more advanced and interesting I would say. It made me thinking out loud I guess.

In real life I receive whole XML file as a parameter to a stored procedure. So in my case front end takes care of reading XML file and passing it's content to the stored procedure. But I thought it is interesting to read file itself from a stored procedure.
But the problem here is that you always have to remember that server's C or D drive is not the same as your computer's C or D drive it could be access rights violations and lots more.

Nice talking to you.
Irina.
Oct 21 '07 #8
radcaesar
759 Expert 512MB
See my code, since i was using the sp_xml_preparedocument, it will not acept its @FileContent Parameter with a WhiteSpace at end.

The error thrown was

'The error description is 'Required white space was missing.'

Any workaround for this ?

EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents
Oct 21 '07 #9
iburyak
1,017 Expert 512MB
sp_xml_preparedocument is very strict and requires well formed XML only.

If you have XML like this:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?> 
  2. <downloads> 
  3.         <products> 
  4.  
  5.         </products> 
  6. </downloads> 

The answer is there should be a space before the ?

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" ?> 
  2. <downloads> 
  3.         <products> 
  4.  
  5.         </products> 
  6. </downloads>
Oct 21 '07 #10
Jim Doherty
897 Expert 512MB
See my code, since i was using the sp_xml_preparedocument, it will not acept its @FileContent Parameter with a WhiteSpace at end.

The error thrown was

'The error description is 'Required white space was missing.'

Any workaround for this ?

EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents

Given we don't have your xml file to go on referenced by this bit

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT * FROM OPENXML (@idoc,'/DTSConfiguration/ConfiguredValue ',2) WITH (ConfiguredValue varchar(10))
  3. -- ContactName varchar(20))
  4.  
Then we are left to guess and are obliged to subsitute with something that is well formed in order to replicate your issue so.....

This simple bunch of text if saved in the root of C as MyCustomers.xml using a text editor here:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <ROOT>
  3. <Customer CustomerID="VINET" ContactName="Paul Henriot">
  4. <Order CustomerID="VINET" EmployeeID="5" OrderDate="1996-07-04T00:00:00">
  5.      <OrderDetail OrderID="10248" ProductID="11" Quantity="12"/>
  6.      <OrderDetail OrderID="10248" ProductID="42" Quantity="10"/>
  7. </Order>
  8. </Customer>
  9. <Customer CustomerID="LILAS" ContactName="Carlos Gonzlez">
  10. <Order CustomerID="LILAS" EmployeeID="3" OrderDate="1996-08-16T00:00:00">
  11.      <OrderDetail OrderID="10283" ProductID="72" Quantity="3"/>
  12. </Order>
  13. </Customer>
  14. </ROOT>
  15.  
Will produce the output you require when this code is run in Query analyser (Your Select * FROM OPENXML statement is commented out obviously to reference the example shown the attribute/element centric mapping flag is set to 1. You need to compare yours against this because this works within the constructs of the program flow)

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE @FileName varchar(255)
  3. DECLARE @ExecCmd VARCHAR(255)
  4. DECLARE @y INT
  5. DECLARE @x INT
  6. DECLARE @FileContents VARCHAR(8000)
  7. DECLARE @idoc int
  8. CREATE TABLE #tempXML(PK INT NOT NULL IDENTITY(1,1), ThisLine VARCHAR(255))
  9. SET @FileContents = ''
  10. SET @FileName = 'c:\MyCustomers.xml'
  11. SET @ExecCmd = 'type ' + @FileName
  12. INSERT INTO #tempXML EXEC master.dbo.xp_cmdshell @ExecCmd
  13. DECLARE @AccountID varchar(8000)
  14. DECLARE @getAccountID CURSOR
  15. SET @getAccountID = CURSOR FOR select ThisLine from #tempXML
  16. OPEN @getAccountID
  17. FETCH NEXT FROM @getAccountID INTO @AccountID
  18. WHILE @@FETCH_STATUS = 0
  19. BEGIN
  20. SET @FileContents = isnull(@FileContents + cast(@AccountID as varchar(8000)),@FileContents)
  21. FETCH NEXT FROM @getAccountID INTO @AccountID
  22. --print @FileContents
  23. END
  24. CLOSE @getAccountID
  25. DEALLOCATE @getAccountID
  26. --print @FileContents
  27. select @FileContents
  28. EXEC sp_xml_preparedocument @idoc OUTPUT, @FileContents
  29. --SELECT * FROM OPENXML (@idoc,'/DTSConfiguration/ConfiguredValue ',2) WITH (ConfiguredValue varchar(10))
  30. -- ContactName varchar(20))
  31. SELECT * FROM OPENXML (@idoc,'/ROOT/Customer',1) WITH (CustomerID varchar(10),ContactName varchar(20))
  32. DROP TABLE #tempXML
Hope this helps you

Regards

Jim :)
Oct 21 '07 #11
radcaesar
759 Expert 512MB
Thanks JIM,

I Had solved this immediately after 5 mins when i posted this reply. But i had forgot to revert back with you guys. Sorry.

Thanks.
Oct 22 '07 #12
Jim Doherty
897 Expert 512MB
Thanks JIM,

I Had solved this immediately after 5 mins when i posted this reply. But i had forgot to revert back with you guys. Sorry.

Thanks.
Thanks for posting anyway glad you sorted it

Jim
Oct 22 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Timo Virkkala | last post by:
I'm creating a system with Python CGIs, that connect to a database. I'm wondering about input validation. Of course I will check the length of the passed parameters, to (hopefully) prevent any DOS...
4
by: Martin Franklin | last post by:
I am trying to get a total record count on a Acess 97 mdb database. However I get the following error when I try to load the page: Provider error '80040154' Class not registered...
3
by: Michael | last post by:
Dear All I have problem with my database server which running SQL server 2000. The server running very slow. The worst case, to save a record required more than 20-30 seconds. Since this...
1
by: mrcraze | last post by:
Hi Everyone! We are using a cursor for paging results in SQL server, mainly due to the performance gains achieved when working with large results sets. We have found this to be of great benefit...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
9
by: KDawg44 | last post by:
Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to...
1
by: Nils | last post by:
I would like to have the SPinner() to return the cursor (cc) to SPouter, which returns it to caller (client). My simple procedures is as follows: CREATE PROCEDURE SPinner () LANGUAGE SQL...
9
balabaster
by: balabaster | last post by:
I'm looking for some ideas regarding string parsing and brackets. Say I have the following string: 56*(73+23/(28+(7/14)-(3/2)) What would be the best way to parse the string for each opening...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.