views:

43

answers:

3

Hai guys,

I have a table with a column named Is_Deleted and my query is

select Is_Deleted from Stock where Stock.Mat_Id=1

alt text

Now i have to write a condition to check whether all values are 1 else i have to terminate my loop.. How it can be done? any suggestions...

+1  A: 
select exists(select 1 from Stock where Mat_Id = 1 and
  (is_deleted is null or is_deleted <> 1))
jspcal
@jspcal it shows error... An expression of non-boolean type specified in a context where a condition is expected, near ')'
Pandiya Chendur
@jspcal your edited query gives me Incorrect syntax near 'limit'.
Pandiya Chendur
@jspcal ya i have checked but still Incorrect syntax near 'limit'.
Pandiya Chendur
@jspascal no result
Pandiya Chendur
+1  A: 

This should do what you're after.

IF EXISTS(SELECT 1 FROM (select DISTINCT Is_Deleted from Stock where Stock.Mat_Id=1) a WHERE Is_Deleted <> 1)
BEGIN
 -- Terminate the loop
END
ELSE
BEGIN
 -- Perform action
END
Dave Barker
@Dave perfect that worked.... see my other question and try to give an answer for that also http://stackoverflow.com/questions/2093363/update-statement-in-sql-server-2005
Pandiya Chendur
@Pandiya: what's wrong with my answer to the other question?
gbn
@Pandiya: It looks like gbn has provided an accepted answer.
Dave Barker
@gbn your answer worked for me... It was fault on my part to ask dave to answer for that... Keep in touch..
Pandiya Chendur
+1  A: 

A simple exists is all you need

IF EXISTS (SELECT * FROM Stock.Is_Deleted = 0 AND Stock.Mat_Id = 1)
...

If you are looping through different Mat_Ids...

...
--Get first @Mat_Id
WHILE @Mat_Id IS NOT NULL
BEGIN
    IF EXISTS (SELECT * FROM Stock.Is_Deleted = 0 AND Stock.Mat_Id = @Mat_Id)
    BEGIN
       ...
    END
    SET @Mat_Id = NULL
    --Get next @Mat_Id
END
...
gbn
@gbn ur latter answer worked for me..
Pandiya Chendur
@gbn can you give ur mail id if you wish, so that i can contact you through mail...
Pandiya Chendur