Solved

looping through the table

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

758 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