Solved

looping through the table

Posted on 2007-04-05
3
204 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 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

724 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