Reputation: 4324
I want to test two TaskChangeLogIDs where ID '28' has a NewRollback of 0, and ID '31' has a NewRollback of 1.
I want make sure that depending which ID is called, it selects the correct NewRollback number. I get an error applying the code below in a new window so just need help tweaking this and learn on how to test things like this in the future. I'm getting an error in the IF statement line only.
SELECT isnull(NewRollback, 0) FROM Core.TaskChangeLog
WHERE TaskChangeLogID = 28
SELECT isnull(NewRollback, 0) FROM Core.TaskChangeLog
WHERE TaskChangeLogID = 31
IF (NewRollback = 1)
Select * from Core.TaskChangeLog where NewRollback = 1
ELSE
BEGIN
Select * from Core.TaskChangeLog where NewRollback = 0
END
Upvotes: 2
Views: 64
Reputation: 93694
You don't need IF
condition to do this just AND/OR
logic will do the job
Try this way
Select * from Core.TaskChangeLog
where (NewRollback = 1 and askChangeLogID = 28)
OR (NewRollback = 0 and askChangeLogID = 31)
To do some action based on select
result try this
IF EXISTS (Select * from Core.TaskChangeLog
where NewRollback = 1 and askChangeLogID = 28) -- Replace with your ID
BEGIN
--some action
END
IF EXISTS (Select * from Core.TaskChangeLog
where NewRollback = 0 and askChangeLogID = 28)
BEGIN
--some action
END
Upvotes: 2
Reputation: 1269553
How about putting all this in one query? This is my best guess as to the logic you want:
Select *
from Core.TaskChangeLog
where NewRollback = (SELECT COALESCE(NewRollback, 0)
FROM Core.TaskChangeLog
WHERE TaskChangeLogID = XX
);
Your code fails because the NewRollback
in the IF
is undefined. You could use a variable, but that would just complicate the code. And, the code should be simplified rather than made more complicated.
Upvotes: 2