Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query IF..ELSE problem

Status
Not open for further replies.

damgog

Programmer
Jul 12, 2001
10
IE
I am new to SQL so please be gentle.
I have three tables A, B and c. I am selecting from a JOIN on these three tables. There are three cases:
if value1 in A > value2 in A
do something, ie. UPDATE tables A, B and C
else if value1 in A = value2 in A
do something else
else if value1 in A < value2 in A
do something else.

Is there any way of writing something like
IF value1 FROM A > value2 FROM A

The closest I can get to that is
IF(SELECT value1 FROM A) > (SELECT value2 FROM A)
This causes an error.

I hope I am making sense. Please dont hesitate to reply if you would like me to clarify the problem

Thank you.
 

It appears that you'll need to use a cursor. Here is some T-SQL to help you get started.

DECLARE @val1 int, @val2 int
DECLARE TableA_Cursor CURSOR FOR
SELECT value1, value2 FROM TableA

OPEN TabelA_Cursor

FETCH FIRST FROM TabelA_Cursor INTO @val1, @val2
WHILE @@FETCH_STATUS = 0
BEGIN

IF @val1 > @ val2
BEGIN
<Update tables A, B and C>
END
ELSE
IF @val1 = @val2
BEGIN
<do something>
END
ELSE
BEGIN
<Do something>
END

FETCH NEXT FROM TabelA_Cursor INTO @val1, @val2

END

CLOSE TabelA_Cursor
DEALLOCATE TabelA_Cursor

Terry Broadbent
Please review faq183-874.

&quot;The greatest obstacle to discovery is not ignorance -- it is the illusion of knowledge.&quot; - Daniel J Boorstin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top