|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 2:15 PM
Points: 1,176,
Visits: 674
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Wednesday, April 11, 2012 10:17 AM
Points: 213,
Visits: 3
|
|
First, if you created a foreign key on father_id you wouldn't be able to insert the null son record.
Second, since -255 is your default value for 'no father' in the Father table why not remove the isnull() from the view and use -255 as the value for 'no father' in the son table? Then everything works fine with an inner join and foreign key?
Edit: Oh yeah, the table name says [Family] on the create index statements.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 9:43 AM
Points: 1,137,
Visits: 670
|
|
Peter,
You can insert a NULL row even if there's a foreign key:
use tempdb go
create table a (aid int not null primary key) insert a values (1) go
create table b (aid int references a (aid)) insert b values (null) go
select * from b go
drop table b drop table a go
-- Adam Machanic SQL Server MVP SQLblog.com: THE SQL Server Blog Spot on the Web
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 2:15 PM
Points: 1,176,
Visits: 674
|
|
Hello guys thanks for the reply! Pete, this chunk of code works fine ... INSERT INTO Father (Father_id, Father_name) values(-255,'No father') ... on isnull(s.father_id, -255)=f.father_id ... but ... INSERT INTO Father (Father_id, Father_name) values(null,'No father') ... on s.father_id = f.father_id ... don´t work at all, no childs without father is show. At the other hand i canot change the row INSERT INTO Son values(null,'Child 0X of no father') to INSERT INTO Son values(-255,'Child 0X of no father') because at my realworld problem that table contains millions of rows with null values and dont want change my old data. Adam, At my problem Father_id is a IDENTITY column and cannot be nullable. Guys, It´s just a example from real problem. Of course u can find yourself in a bit different dilema. I´m happy to share my solution and to learn from yours replys. Jean
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 4:14 PM
Points: 1,
Visits: 35
|
|
| You don't have to use null you can insert a record with the id of zero, a negative number, etc.. still quite the rig, but might get you out of a jam.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 4:56 AM
Points: 84,
Visits: 174
|
|
Your example doesn't work.
In the left and right joins, Father 3 is displayed.
With the isnull(....), there is no child record in the son table to apply the isnull too, therefore, the father record never appears.
|
|
|
|