Hey all,


Can any when tell me how to loop and put if-conditions in sql query? as I am new in SQL i am a bit confused about this please help me i have very short time.


Thanks in Advance

Recommended Answers

All 2 Replies

Loops in sql qork with cursors check these links 1.for detailed help coding example is as follows

DECLARE @A varchar(50),@B varchar(50);
 DECLARE @c bigint;
set @c=0
DECLARE contact_cursor CURSOR FOR
SELECT A, B  FROM TABLE_NAME

OPEN contact_cursor;
FETCH NEXT FROM contact_cursor
INTO @A,@B;
WHILE @@FETCH_STATUS = 0
BEGIN
set @c=(select count(b) from table_name where A=@A)
PRINT @A+' | '+ @B+' | '+ @C +' | text. |'
   -- This is executed as long as the previous fetch succeeds.
   FETCH NEXT FROM contact_cursor
  INTO @A,@B;
END
CLOSE contact_cursor;
DEALLOCATE contact_cursor;

for if condition check this link.... example is given bellow

IF condition
BEGIN 
--code
END
ELSE
BEGIN 
--code
END
commented: Helpful +2

Thanks abellazm I will give it a try

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.