0

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

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by bahed121
1

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

Edited by abelLazm: n/a

Votes + Comments
Helpful
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.