Groups | Blog | Home
all groups > sql server programming > march 2004 >

sql server programming : PLease help with case expression...complex.


Magy
3/22/2004 10:50:57 PM
I need some help with a case expression. I need to interpert the following
statements into a case expression. The statements are from Access and I'm
trying to do the same in a SQL stored proc.
@cboagTypeGroup And @cboagType will be the parameters passed to my
procedure. Normally they would represent controls on a form, but I replaced
them with the variable names to be more clear.


IIf(@cboagTypeGroup & ""="*" And cboagType & ""="*",1,

IIf(@cboagType] & ""<>"*",

IIf([pkAgreementTypeID] & ""=@cboagType & "",1,0),

IIf([fkAgreementGroupTypeID] & ""=cboagTypeGroup & "",1,0)))


This what I came up with but it does not work like I would like it to:

Field2 = case when @cboagTypeGroup = '*' AND @cboagType = '*' then 1 else
case when @cboagType <> '*' then
case pkAgreementTypeID when @cboagType then 1 else
case fkAgreementGroupTypeID when @cboagTypeGroup then 1 else 0
end end end end

I really need help to accomplish this.

Thank you sincerly,
Magy

Anith Sen
3/22/2004 11:28:52 PM
Instead of simply doing code conversion, it would be better if you
understand the logic & re-write it in t-SQL. Based on a glance though the
code, you can have :

CASE WHEN @cboagTypeGroup = '*' AND cboagType = '*'
THEN 1
ELSE CASE WHEN @cboagType <> '*'
THEN CASE WHEN pkAgreementTypeID = @cboagType
THEN 1
ELSE 0
END
ELSE CASE WHEN fkAgreementGroupTypeID = cboagTypeGroup
THEN 1
ELSE 0
END
END

--
Anith

Greg Obleshchuk
3/23/2004 4:27:02 PM
Hi

Try this

Case
when @cboagTypeGroup = '*' AND @cboagType = '*' then 1
when @cboagType <> '*' then case when pkAgreementTypeID = @cboagType
then 1 else 0 end
else case when fkAgreementGroupTypeID = @cboagTypeGroup then 1 else 0
end
end


that looks about right
Greg


[quoted text, click to view]

AddThis Social Bookmark Button