Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop through data in SQL Server store proc

Posted on 2008-06-18
3
Medium Priority
?
961 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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