[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how can i use for loop in stored procedure in sql server ??

Posted on 2011-03-03
13
Medium Priority
?
779 Views
Last Modified: 2012-05-11
i want to know that how can i use for loop in stored procedure in MS SQL Server ??

please refer the below strored procedure ...

CREATE PROCEDURE usp_SaveNewQuestion
    (
      @uid INT ,
      @Question VARCHAR(200) ,
      @correctOpt VARCHAR(50)
    )
AS 
    BEGIN

        INSERT  INTO Details
                SELECT  uname ,
                        pwd ,
                        email ,
                        login_time ,
                        @Question ,
                        @correctOpt ,
                        logout_time ,
                        comment
                FROM    Details
                WHERE   uid = @uid
        

    END
GO

Open in new window


how can i use for loop in above stored procedure ??
0
Comment
Question by:Parth48
[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
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35033870
sql u have to use cursor

here is the simple cursor implementation
syntax:
DECLARE cursor_name [INSENSITIVE] [SCROLL] CURSOR
FOR select_statement
[FOR {READ ONLY | UPDATE [OF column_name [,...n]]}]

Open in new window


here is the complate example
http://www.mssqlcity.com/Articles/General/UseCursor.htm
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 35033873
can u please tell what for you using the loop in your stored procedure ?
for inserting the data ?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35033876
you can use while loop like this

DECLARE @intFlag INT
SET @intFlag = 1
WHILE (@intFlag <=5)
BEGIN
PRINT @intFlag
SET @intFlag = @intFlag + 1
END
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:Parth48
ID: 35034003
hello @pratima_mcs: Thanks , in both the way u r right , but which is the better way according to u , using cursor or while loop ??
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35034023
it really depends on your situation. I almost always use a cursor to loop through records when necessary. The cursor format is a little more intuitive for me and, since I just use the constructs to loop through the result set once, it makes sense to use the FAST_FORWARD cursor. Remember that the type of cursor you use will have a huge impact on the performance of your looping construct.

for more details refer

http://www.techrepublic.com/blog/datacenter/comparing-cursor-vs-while-loop-performance-in-sql-server-2008/1741
0
 

Expert Comment

by:h_aravind
ID: 35034030
You can use a While loop rather than using a cursor. cursors are performance hit.
0
 
LVL 1

Accepted Solution

by:
lalitgada earned 2000 total points
ID: 35034081
USE AdventureWorks;
GO
DECLARE @Flag INT
SET @Flag = 1
WHILE (@Flag < 10)
BEGIN
BEGIN
PRINT @Flag
SET @Flag = @Flag + 1
END
IF(@Flag > 5)
BREAK
ELSE
CONTINUE
END
WHILE loop can use SELECT queries as well. You can find following example of BOL very useful.
USE AdventureWorks;
GO
WHILE (
SELECT AVG(ListPrice)
FROM Production.Product) < $300
BEGIN
UPDATE Production.Product
SET ListPrice = ListPrice * 2
SELECT MAX(ListPrice)
FROM Production.Product
IF (
SELECT MAX(ListPrice)
FROM Production.Product) > $500
BREAK
ELSE
CONTINUE
END
PRINT 'Too much for the market to bear';
0
 

Author Comment

by:Parth48
ID: 35034152
hi @lalitgada: where i can download AdventureWorks database ??

is it on MSDN ??
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35034157
AdventureWorks is default database on SQl server
0
 

Expert Comment

by:h_aravind
ID: 35034171
Adventure works, northwind database etc can be downloaded from Microsoft website or from codeplex.com
0
 

Author Comment

by:Parth48
ID: 35034172
hi @h_aravind: can u please post the download link ??
0
 

Expert Comment

by:h_aravind
ID: 35034179
Can you try this?

http://sqlserversamples.codeplex.com/

This has details for both 2005 & 2008
0
 

Author Comment

by:Parth48
ID: 35034369
hi @h_aravind: thanks for the link ....
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

650 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