Solved

Loop through data in SQL Server store proc

Posted on 2008-06-18
3
958 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21812061
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
ID: 21812156
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
ID: 21812223
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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