It is possible to simulate left join using inner join while creating indexed view in SQL Server? I found a solution using ISNULL(id,0)
but it does not work for me.
I have Table1 (ID PK, ID_U FK null)
and I want to left join Table2 (ID_U PK, VALUE)
. I want to see all rows from Table1
and associated rows from Table2
if exist ID_U.
In created view script I have
select t1.ID, t1.ID_U, t2.VALUE
from Table1 t1
join Table2 t2 on t1.ID_U = t2.ID_U
or (ISNULL(t1.ID_U, 0) = 0 and ISNULL(t2.ID_U, 0) = 0)
where t1.ID_U is null -- only for test
/* create clustered index */
As a result I should see all rows from Table1
where ID_U is null with nulls in right table, but I have 0 rows.
What am I doing wrong?
I do not recommend you do this.
You really should not do this.
Not even sure if SQL will allow you to create an indexed view doing this - hope not.
But, if you absolutely have to make an INNER act like a LEFT JOIN, then here's one way...