Solved

looping through the table

Posted on 2007-04-05
3
199 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
3 Comments
 
LVL 11

Accepted Solution

by:
dready earned 500 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

895 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

18 Experts available now in Live!

Get 1:1 Help Now