|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 17, 2013 12:22 PM
Points: 10,571,
Visits: 11,871
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 10:46 AM
Points: 911,
Visits: 831
|
|
To add the list of recommended readings regarding parsing a delimited string, I find Erland Sommarskog's articles are very helpful:
http://www.sommarskog.se/arrays-in-sql-2005.html http://www.sommarskog.se/arrays-in-sql-2000.html
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 18, 2010 10:36 AM
Points: 10,
Visits: 25
|
|
Hi,
How would I iterate through an XML object to obtain each specific row 1 at a time. I was trying this way, but value requires a literal for both parameters. Not sure what to do?
ALTER PROCEDURE [dbo].[XMLUpdateTest] -- Add the parameters for the stored procedure here @XmlList XML, @RowCount int,
AS BEGIN Declare @Count int Declare @ObjectName varchar(50) Declare @ColumnName varchar(50) Declare @Property varchar(50) Declare @Value varchar(50)
Declare @ParObjectName varchar(50) Declare @ParColumnName varchar(50) Declare @ParProperty varchar(50) Declare @ParValue varchar(50)
set @Count=0
while(@Count<@RowCount) BEGIN set @ParObjectName=@ObjectName+'['+convert(varchar(2), @Count)+']' --@ObjectName[3]
set @ParColumnName=@ColumnName+'['+convert(varchar(2), @Count)+']' --@ObjectName[3]
set @ParProperty=@Property+'['+convert(varchar(2), @Count)+']' --@ObjectName[3]
set @ParValue=@Value+'['+convert(varchar(2), @Count)+']' --@ObjectName[3]
Select XmlList.Row.value(@ParObjectName,'varchar(50)'), XmlList.Row.value(@ParColumnName,'varchar(50)'), XmlList.Row.value(@ParProperty,'varchar(50)'), XmlList.Row.value(@ParValue,'varchar(50)') from @XmlList.nodes('//Rows/Row') as XmlList(Row)
set @Count=@Count +1
END
Thanks for the help
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
You could do this by using a variable. You can refer to an element/attribute as 'sql:variable("@varname")'. Look for sql:variable in books online. I have covered this in "XML Workshop XVII - Writing a LOOP to process XML elements in TSQL". I see it in pending publication list. Hope it will be out in a week or two.
.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 18, 2010 10:36 AM
Points: 10,
Visits: 25
|
|
Hi,
Yes, I agree with parsing large amounts of data. In this case I have a datable in my C# app that I want to pass as an XML parameter. That all works fine. The table may have 15-20 rows. The part I am having diffuculty with is how to iterate the XML table that is passed to the Stored Procedure. I guess I need to use the aliased table that I buid from the XML object. Is there a way to iterate through the XML object to obtain a specific element instead of aliasing another table?
Thanks,
Alan
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 18, 2010 10:36 AM
Points: 10,
Visits: 25
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
Are you looking for something like the following?
DECLARE @x XML SET @x = ' ' -- Total count of Nodes DECLARE @max INT, @i INT SELECT @max = @x.query(' { count(/Employees/Employee) } ').value('e[1]','int') -- Set counter variable to 1 SET @i = 1 -- variable to store employee name DECLARE @EmpName VARCHAR(10) -- loop starts WHILE @i <= @max BEGIN -- select "Name" to the variable SELECT @EmpName = x.value('Name[1]', 'VARCHAR(20)') FROM @x.nodes('/Employees/Employee[position()=sql:variable("@i")]') e(x) -- print the name PRINT @EmpName -- increment counter SET @i = @i + 1 END
.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Saturday, May 11, 2013 8:17 AM
Points: 460,
Visits: 2,521
|
|
aplogies for the bad formatting. the editor does not help me to format correctly. I must be missing something. And my xml data is also missing in the post. But I guess this will help you to get an idea about writing loop to process XML elements
.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 18, 2010 10:36 AM
Points: 10,
Visits: 25
|
|
Hi,
Yes that may work. I was able to pass as a XML values as attributes. Then I just used a CURSOR - I know performance hog and this works OK. I will try your method as well.
Thanks,
Alan
PS: I will forward the final solution as soon as I get it.
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, March 18, 2010 10:36 AM
Points: 10,
Visits: 25
|
|
Hi again,
I have it working, but I could not figure this part out. Query Analyzer throws an exception for XQUERY on this line of code:
SELECT @max = @x.query(' { count(/Employees/Employee) } ').value('e[1]','int')
Right now I am passing the count but would like to dynamically obtain based on your example.
Thanks,
Alan
|
|
|
|