August 5, 2009 at 9:28 am
Hi everyone,
I am trying to create a view with a left outer join. The following code works fine with creting the join, but I would like to know if it's possible for me to set default values for the columns from tableB? I've tried setting defaults in the underlying tableB value, but it doesn't seem to propagate into the view when it is created.
create view myView
as
select tableA.col1, tableA.col2, tableB.col1, tableB.col2
from tableA left outer join tableB
on tableA.col1 and tableB.col1
Is there anyway to set defaults for the rows that has no matches?
Thank in advance for any help!
Tom
August 5, 2009 at 9:40 am
sure...the ISNULL or COALESCE function can do what you want:
create view myView
as
select
tableA.col1 as cola1,
tableA.col2 as cola2,
ISNULL(tableB.col1,'no selection') as colb1, --assuming a varchar or something
ISNULL(tableB.col2,0) as colb2 --assuming an integer or something
from tableA
left outer join tableB
on tableA.col1 = tableB.col1
Lowell
August 5, 2009 at 10:56 am
Worked perfectly now with the defaults! Thanks a lot!
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply