Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Performing an insert with Null

Author  Topic 

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 09:18:39
I have a basic insert function that uses two fields, one from each of two tables.

However, the table being inserted into doesn't allow nulls in the 2 insert fields (and i want to keep that) but the data being inserted might have Nulls due to the left outer join.

here's the code:

insert into tbl_cohort (cohort_key, family_key)
select a.cohort_key, cu.family_key
from tbl_cohort a
left outer join
tbl_usage cu on a.family_key = cu.family_key
where a.import_control_key = ‘10’


So basically, if there is a value in tbl_cohort that is not found in table "tbl_usage" then the family key field is NULL and the code errors out (I'm calling this code from VS2008, though I don't think that matters). What i really want to happen is that any record where the left join causes a NULL in the family key column to make family key "OTHER" instead of NULL.

I understand the ISNULL syntax when the null value is in the data before runnign the ISNULL, but not sure how to make it happen in an Insert statement where the left join causes the NULL.

Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-05-08 : 09:46:41
I will only answer your question on how to use ISNULL() in this case.
I will not ask why you want to do that
quote:
Originally posted by rdw72777

I have a basic insert function that uses two fields, one from each of two tables.

However, the table being inserted into doesn't allow nulls in the 2 insert fields (and i want to keep that) but the data being inserted might have Nulls due to the left outer join.

here's the code:

insert into tbl_cohort (cohort_key, family_key)
select a.cohort_key, ISNULL(cu.family_key,'')
from tbl_cohort a
left outer join
tbl_usage cu on a.family_key = cu.family_key
where a.import_control_key = ‘10’


So basically, if there is a value in tbl_cohort that is not found in table "tbl_usage" then the family key field is NULL and the code errors out (I'm calling this code from VS2008, though I don't think that matters). What i really want to happen is that any record where the left join causes a NULL in the family key column to make family key "OTHER" instead of NULL.

I understand the ISNULL syntax when the null value is in the data before runnign the ISNULL, but not sure how to make it happen in an Insert statement where the left join causes the NULL.

Thanks




No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 10:43:16
Is there a better way than ISNULL. I'm relatively new and this is what I know, so if there's a better way I'm all ears.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 11:08:53
quote:
Originally posted by rdw72777

Is there a better way than ISNULL. I'm relatively new and this is what I know, so if there's a better way I'm all ears.


hmm..whats the issue with ISNULL function? you also have COALESCE() function which does the same thing. But didnt understand why you dont want to use ISNULL
ISNULL/COALESCE works even when NULL values comes as a result of join. There's no necessity that it should exist in tables even before join

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 11:54:03
Ahh I'd never heard of that. it does look like something I'll use in the future though.

So I'm trying to expand my initial code into a larger block of code and keep getting an error. The larger block of code is below (I'm guiessing the simplified code I listed initally didn't prepare me for potential issues in the lagrer block). It seems like there are foreign key constraints on the value of the cohort_family_code on the table tbl_cohort. I know its set to nto allow NULL, which is what I was hoping the COALESCE would fix but I'm not sure what this error means. Any thoughts?

The error I get is:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_cohort_curve_family_code".
The conflict occurred in database "Amort_D", table "dbo.tbl_curve_family", column 'curve_family_code'. The statement has been terminated.



{insert into tbl_cohort
(import_control_key, date_key, currency_key, business_unit_code, management_code, product_code, deferral_type, curve_family_code, node_key, amount, most_recent_interval)
(select 34, a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'ABC' then 'DEF' else a.deferral_type end as deferral_type, coalesce(a.curve_family, 'OTHER'), n.node_key, Sum(a.[Sum Amount]) as amount, 0
from zitbl_sol_cost a
left outer join [Dim Currency] b on a.[Base Curr] = b.[Currency Abbreviation]
left outer join tbl_node n on a.node = n.node_code
where a.deferral_type <> 'NDC'
group by a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'NDC' then 'DSC' else a.deferral_type end, a.curve_family, n.node_key
having sum(a.[Sum Amount]) <> 0 )
}
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 12:01:08
This means the column 'curve_family_code' is linked via FK constraint to one of other table columns in database. So it will allow only those values which exists as a valid value in master table. As such you cant use COALESCE logic to put a default value into it. The default value should prexist in master table if you're planning to do insert like this.
Ideally we handle these type of scenarios by inserting the default value already in master table so that referential integrity is taken care of. And in select we dont directly pass it like above. But we will use a variable to get value from master table and prestore it like

DECLARE @default_family_code varchar(20)

SELECT @default_family_code = family_code
from yourmastertable
where <default condition here>

then use it in select as follows

insert into tbl_cohort
(import_control_key, date_key, currency_key, business_unit_code, management_code, product_code, deferral_type, curve_family_code, node_key, amount, most_recent_interval)
(select 34, a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'ABC' then 'DEF' else a.deferral_type end as deferral_type, coalesce(a.curve_family, @default_family_code ), n.node_key, Sum(a.[Sum Amount]) as amount, 0
from zitbl_sol_cost a
left outer join [Dim Currency] b on a.[Base Curr] = b.[Currency Abbreviation]
left outer join tbl_node n on a.node = n.node_code
where a.deferral_type <> 'NDC'
group by a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'NDC' then 'DSC' else a.deferral_type end, a.curve_family, n.node_key
having sum(a.[Sum Amount]) <> 0 )


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 13:16:30
Thanks, I see how that works. Just a couple of followups:

1. In my initial COALESCE, when i put in the literal text 'OTHER', that didn;t work. But 'OTHER' is one of the values in the curve_family_code in the master table (in this instance tbl_curve_family). So i guess SQL doesn't bother to check if my manually entered value of 'OTHER' is int eh master table before having an issue with it; is my understanding correct.

2. Since I know i want the coalesce to always be 'OTHER', do I have to set it through a select statement from that table or can I literally say @default_family_code = 'OTHER' rather than doing the select statement. I see the value in the select statement of course (if I hardcode a value in that isn't part of the mastertable i'll still get the error and need to go back in and hardcode another value) I was just curious.
Go to Top of Page

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 13:36:54
Never mind, I see the issue. "Other" is not a value in the "curve_family_code" field in the table "tbl_curve_family". What I wanted to do was have any time a value not in this table shows up to automatically code it to "Other". I think I know what I need to do
Go to Top of Page

rdw72777
Starting Member

14 Posts

Posted - 2012-05-08 : 13:59:40
Well it definitely doesn't like finding something in the data that isn't already in the tbl_curve_famiyl tab. I added a record for curve_family_code in this table for "OTHER" and ran the following code and still got the foreign key error.

Feels like an issue better left for tomorrow lol :-(

{DECLARE @default_family_code varchar(50)
SELECT @default_family_code = 'OTHER'
insert into tbl_cohort
(import_control_key, date_key, currency_key, business_unit_code, management_code, product_code, deferral_type, curve_family_code, node_key, amount, most_recent_interval)
(select 41, a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'NDC' then 'DSC' else a.deferral_type end as deferral_type, coalesce(a.curve_family, @default_family_code), n.node_key, Sum(a.[Sum Amount]) as amount, 0
from zitbl_sol_cost a
left outer join [Dim Currency] b on a.[Base Curr] = b.[Currency Abbreviation]
left outer join tbl_node n on a.node = n.node_code
where a.deferral_type <> 'NDC'
group by a.date_key, b.[Currency Key], a.Unit, a.[Mgmt Cd], a.Prod, case a.deferral_type when 'NDC' then 'DSC' else a.deferral_type end, a.curve_family, n.node_key
having sum(a.[Sum Amount]) <> 0 )
}

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-08 : 16:10:07
do you mean you populated a record in tbl_curve_family with value 'OTHER' for column curve_family_code?
Unless you do that you will get fk error

see what below returns

SELECT COUNT(*) FROM tbl_curve_family where curve_family_code='OTHER'


it should return 1 if insertion was done succesfully

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -