Solved

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

Posted on 2011-03-03
13
770 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
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Parth48
Comment Utility
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
Comment Utility
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
Comment Utility
You can use a While loop rather than using a cursor. cursors are performance hit.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Accepted Solution

by:
lalitgada earned 500 total points
Comment Utility
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
Comment Utility
hi @lalitgada: where i can download AdventureWorks database ??

is it on MSDN ??
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
AdventureWorks is default database on SQl server
0
 

Expert Comment

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

Author Comment

by:Parth48
Comment Utility
hi @h_aravind: can u please post the download link ??
0
 

Expert Comment

by:h_aravind
Comment Utility
Can you try this?

http://sqlserversamples.codeplex.com/

This has details for both 2005 & 2008
0
 

Author Comment

by:Parth48
Comment Utility
hi @h_aravind: thanks for the link ....
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

17 Experts available now in Live!

Get 1:1 Help Now