Saturday, February 25, 2012

Can I use a computed column for this?

I have a table with two integers and I want another computed column like so
IIF ( colX >= colY, 1, 0 )
but enterprise manager keeps telling me there is an error in my Formula but
I do not see what it could be.
the computed column is defined to be an int
thanks,
J> but enterprise manager keeps telling me there is an error in my Formula
Egads, why are you using Enterprise Manager for this? Try Query Analyzer.
CREATE TABLE dbo.foo
(
x INT,
y INT,
z AS CONVERT(INT, CASE WHEN x >= y THEN 1 ELSE 0 END)
)
Also, not sure why you want to use INT. This could easily be BIT or TINYINT
if it is only ever going to contain two possible values.|||there is no IIF() in SQL - it's CASE
case when colx>=coly then 1 else 0 end
james wrote:

>I have a table with two integers and I want another computed column like so
>IIF ( colX >= colY, 1, 0 )
>but enterprise manager keeps telling me there is an error in my Formula but
>I do not see what it could be.
>the computed column is defined to be an int
>thanks,
>J
>
>|||James,
Use a "case" expression.
select colX, colY, case when colX >= colY then 1 else 0 end as colZ
from t1
AMB
"james" wrote:

> I have a table with two integers and I want another computed column like s
o
> IIF ( colX >= colY, 1, 0 )
> but enterprise manager keeps telling me there is an error in my Formula bu
t
> I do not see what it could be.
> the computed column is defined to be an int
> thanks,
> J
>
>|||CASE WHEN colx >= coly THEN 1 ELSE 0 END
Why would you put such a thing in a computed column? Put it in a view or
query rather than clutter your table with redundant information.
David Portas
SQL Server MVP
--|||Thanks Aaron, and all the other responses. All your comments are helpful
JIM
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:%23GfaOmjuFHA.2076@.TK2MSFTNGP14.phx.gbl...
> Egads, why are you using Enterprise Manager for this? Try Query Analyzer.
> CREATE TABLE dbo.foo
> (
> x INT,
> y INT,
> z AS CONVERT(INT, CASE WHEN x >= y THEN 1 ELSE 0 END)
> )
> Also, not sure why you want to use INT. This could easily be BIT or
> TINYINT if it is only ever going to contain two possible values.
>

No comments:

Post a Comment