Solved

Loops in SQL

Posted on 2011-09-08
2
204 Views
Last Modified: 2012-05-12
Hello,

I am still trying to grasp some of the topics in SQL, and am wondering what loops are used for? I know how to construct a loop, but I just don't know in what case you would use one. If you could give me some real life examples with a description that would be great. Thanks.
0
Comment
Question by:erp1022
2 Comments
 
LVL 21

Accepted Solution

by:
JestersGrind earned 300 total points
ID: 36505520
Generally loops should be avoid in SQL.  Database theory is based in set mathematics, thus SQL should use set based solutions as much as possible.  That being said the times to use loops are when it's necessary to execute thing row by row.  A common one that I run into is when I have a table of data to process and the stored procedure that processes the data only accepts single values, basically one record.  I would loop through my table and execute the procedure many times.  Not the most efficient way to do it.  Given more time, I would rather rewrite the called stored procedure to accept a table parameter or XML and process all the rows at once.  That's not always possible though.

Greg

0
 

Author Closing Comment

by:erp1022
ID: 36505651
Thanks!
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql splitting name column 5 40
Syntax using Declare 4 38
DATETIMEOFFSET feature 1 32
SQL Server merge records in one table 2 8
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

939 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

4 Experts available now in Live!

Get 1:1 Help Now