Solved

Loop through data in SQL Server store proc

Posted on 2008-06-18
3
953 Views
Last Modified: 2012-05-05
Below is a proc that I have. after executing it I get 2 rows of XML. I want to loop through each row and perform some tasks on it. How can I loop through different rows in SQL server store proc
DECLARE @docHandle int

EXEC sp_xml_preparedocument @docHandle OUTPUT, 

'<ClauseGroup>

		<GroupID>1</GroupID>

		<GroupName>Test Group</GroupName>

		<EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>

		<ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>

		<DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>

	 <Clauses>

	  <Clause>

	   <ClauseID>0</ClauseID>

	   <ClauseGroupID>2</ClauseGroupID>

	   <EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>

	   <ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>

	   <DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>

	   <Description></Description>

	  <Operation>

	   <Code>BET</Code>

	  </Operation>

	  <Attribute>

	   <AttributeID>2</AttributeID>

	  </Attribute>

	  <Values>

	   <Value>

	     <value>Test4</value>

	   </Value>

	   <Value>

	     <value>Test5</value>

	   </Value>

	  </Values>

	  </Clause>

	  <Clause>

	   <ClauseID>1</ClauseID>

	   <ClauseGroupID>2</ClauseGroupID>

	   <EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>

	   <ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>

	   <DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>

	   <Description>test</Description>

	  <Operation>

	   <Code>IN</Code>

	  </Operation>

	  <Attribute>

	   <AttributeID>3</AttributeID>

	  </Attribute>

	  <Values>

	   <Value>

	     <value>Test6</value>

	   </Value>

	   <Value>

	     <value>Test7</value>

	   </Value>

	  </Values>

	  </Clause>	

	 </Clauses>

	</ClauseGroup>'
 

Select * 

FROM OPENXML(@docHandle, 'ClauseGroup/Clauses/Clause', 1) 

WITH (Data xml '.')

Open in new window

0
Comment
Question by:pratikshahse
  • 2
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Use a cursor to do the looping:

DECLARE  CursorTemplate CURSOR
FAST_FORWARD FOR       
      SELECT Val1, Val2, Val3 FROM Table1

OPEN CursorTemplate

FETCH NEXT FROM CursorTemplate
INTO      @Var1, @Var2, @Var3

WHILE (@@FETCH_STATUS = 0)
BEGIN
      --do something here w/ your data

      FETCH NEXT FROM CursorTemplate
      INTO      @Var1, @Var2, @Var3

END

CLOSE CursorTemplate
DEALLOCATE CursorTemplate
0
 

Author Comment

by:pratikshahse
Comment Utility
i am kind of confused with this  statement. Can you please explain.

FAST_FORWARD FOR      
      SELECT Val1, Val2, Val3 FROM Table1  (I dont have to select anything from the table over here. The data is coming XML )





0
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 total points
Comment Utility
Right...you'd just swap in your SQL statement that pulls the XML data out...

ECLARE @docHandle int
EXEC sp_xml_preparedocument @docHandle OUTPUT,
'<ClauseGroup>
                <GroupID>1</GroupID>
                <GroupName>Test Group</GroupName>
                <EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>
                <ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>
                <DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>
         <Clauses>
          <Clause>
           <ClauseID>0</ClauseID>
           <ClauseGroupID>2</ClauseGroupID>
           <EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>
           <ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>
           <DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>
           <Description></Description>
          <Operation>
           <Code>BET</Code>
          </Operation>
          <Attribute>
           <AttributeID>2</AttributeID>
          </Attribute>
          <Values>
           <Value>
             <value>Test4</value>
           </Value>
           <Value>
             <value>Test5</value>
           </Value>
          </Values>
          </Clause>
          <Clause>
           <ClauseID>1</ClauseID>
           <ClauseGroupID>2</ClauseGroupID>
           <EffectiveDate>1/1/1900 12:00:00 AM</EffectiveDate>
           <ExpirationDate>1/1/1900 12:00:00 AM</ExpirationDate>
           <DeActivateDate>1/1/1900 12:00:00 AM</DeActivateDate>
           <Description>test</Description>
          <Operation>
           <Code>IN</Code>
          </Operation>
          <Attribute>
           <AttributeID>3</AttributeID>
          </Attribute>
          <Values>
           <Value>
             <value>Test6</value>
           </Value>
           <Value>
             <value>Test7</value>
           </Value>
          </Values>
          </Clause>    
         </Clauses>
        </ClauseGroup>'
 


DECLARE  CursorTemplate CURSOR
FAST_FORWARD FOR      
Select *
FROM OPENXML(@docHandle, 'ClauseGroup/Clauses/Clause', 1)
WITH (Data xml '.')

OPEN CursorTemplate

FETCH NEXT FROM CursorTemplate
INTO      @Var1, @Var2, @Var3

WHILE (@@FETCH_STATUS = 0)
BEGIN
      --do something here w/ your data

      FETCH NEXT FROM CursorTemplate
      INTO      @Var1, @Var2, @Var3

END

CLOSE CursorTemplate
DEALLOCATE CursorTemplate
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now