?
Solved

looping through the table

Posted on 2007-04-05
3
Medium Priority
?
206 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

801 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