While I'm assuming this is a homework problem, and I normally don't help with those, this was interesting enough that I couldn't stop myself. 🙂
You have a number of flow-of-logic errors. For example, if you look at where you have your increment on @a, you'll notice that you'll never reach that if @i is not evenly divisible by @a. So, the moment you try to divide 3 by 2, it never increments @a to 3, but just loops. Even if you fix that, check what'll happen to your increment for @i when it hits 4. Since 4 is the first non-prime number, it has a problem with where the increment is in relation to the check for @count.
Try this version:
DECLARE
@i INT,
@a INT,
@count INT
SET @i = 1
WHILE (@i <= 500)
BEGIN
SET @count = 0
SET @a = 1
WHILE (@a <= @i)
BEGIN
IF (@i % @a = 0)
SET @count = @count + 1
END
IF (@count = 2)
PRINT @i
SET @i = @i + 1
END
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon