Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Comparing int columns in SQL

Author  Topic 

krugg
Starting Member

12 Posts

Posted - 2005-03-28 : 07:35:13
Hi,
I am looping through some records in T-SQL using a cursor and I'm wondering if I can do a comparison for two int values. I've written down what I want to achieve inside pseudocode comments below. Is this possible or is there a better way to do it?

DECLARE games_cursor CURSOR FOR
select home_team_id, away_team_id, home_team_score, away_team_score from games where week_id = 2

OPEN games_cursor

FETCH NEXT FROM games_cursor
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM games_cursor
'start of pseudocode
declare @winning_team_id int
if home_team_score > away_team_score
set @winning_team_id = home_team_id
else
set @winning_team_id = away_team_id
'end of pseudocode

CLOSE games_cursor
DEALLOCATE games_cursor


Thanks
Andrew

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-03-28 : 07:47:36

Select case home_team_score > away_team_score then home_team_id else away_team_id end from games where week_id=2


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

krugg
Starting Member

12 Posts

Posted - 2005-03-28 : 22:07:58
I have tried and several variants of this but I get the following error:

Incorrect syntax near '>'.

Any other ideas?

Andrew
Go to Top of Page

rrb
SQLTeam Poet Laureate

1479 Posts

Posted - 2005-03-28 : 22:22:34
you left out when
Select case when home_team_score > away_team_score then home_team_id else away_team_id end from games where week_id=2


--
I hope that when I die someone will say of me "That guy sure owed me a lot of money"
Go to Top of Page

krugg
Starting Member

12 Posts

Posted - 2005-03-28 : 22:44:50
Thanks everyone I got it now...
Go to Top of Page
   

- Advertisement -