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

x
?
Solved

looping through the table

Posted on 2007-04-05
3
Medium Priority
?
207 Views
Last Modified: 2010-03-19
I am using SQL 2005, Below are steps it is the pseudo code but i want a sql for looping around if statement.
1) optid (int) is in the table tblOptid --this is already done imagine 4 integer values in this table
2) CREATE TABLE #tmpMissingData(OptID int)
3) Need to loop through the tblOptid passing each optID to the if statement below and insert in the optID passed to the if statement when the if condition is satisfied, inserting records in #tmpMissingData

--loop here till the end of tblOptID
IF (  (SELECT COUNT(a.start) FROM Times a AND a.OptId  IN (121)  
        <> 
        (SELECT 24 * COUNT(WeekID) FROM DayofWeek WHERE OptId IN (121))
    )
BEGIN
   --Here i want to insert the optid in temporary table that comes in from above.
    INSERT INTO #tmpMissingData(OptID) values (@optID)
END
Go the the next row of tblOptID

Thanks in advance.
0
Comment
Question by:Junior_Developer
[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
3 Comments
 
LVL 11

Accepted Solution

by:
dready earned 2000 total points
ID: 18861501
You can use a cursor to loop through a table:

DECLARE @optID int
CREATE TABLE #tmpMissingData(OptID int)

DECLARE c1 CURSOR FOR
SELECT OptID
FROM tblOptID

OPEN c1

FETCH NEXT FROM c1
INTO @optID

WHILE @@FETCH_STATUS = 0
BEGIN

-- here you can put your logic. I do not understand that completely as i dont know the tables, but sth like:
IF (  (SELECT COUNT(a.start) FROM Times where a.Optid = @optID)
        <> 
        (SELECT 24 * COUNT(WeekID) FROM DayofWeek WHERE OptId  = @optID)
    )
BEGIN
   --Here i want to insert the optid in temporary table that comes in from above.
    INSERT INTO #tmpMissingData(OptID) values (@optID)
END

FETCH NEXT FROM c1
INTO @optID
END

CLOSE c1
DEALLOCATE c1


Hope this helps
0
 
LVL 6

Expert Comment

by:chiragkhabaria
ID: 18861545
How about somthing like this
I dont think you require to loop..

Insert #tmpMissingData(OptID)
Select a.optID From
(
      SELECT OptId,COUNT(a.start) as Cnt1 FROM Times
      Group by OptID
) As a
Inner Join
(
      SELECT OptID, (24 * COUNT(WeekID))As Cnt2 FROM DayofWeek
      Group by OptID
) As b
On a.OptID = b.OptID
And a.Cnt1 <> b.Cnt2
0
 

Author Comment

by:Junior_Developer
ID: 18862557
Execellent this is exactly what i was looking for...
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

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