0

Firstly, I have read through other posts on this page regarding similar issues but I cannot figure out where I am going wrong. I want to populate a column of [table1] with values from another column in [table2].

When I run,

insert into Staged.dbo.factSales(Date)
    select SaleDate 
    from Staged.dbo.SaleDates

I get the following error

Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'SaleValue', table 'Staged.dbo.factSales'; column does not allow nulls. INSERT fails.

SaleValue is the column in Staged.dbo.factSales and design-wise, comes after 'Date' - SaleID, OrderID, ProductID, BranchID, EmployeeID, Date, SaleValue.

Does anyone have any idea where I am going wrong? Thanks in advance!

Marcin

3
  • Use brackets to enclose your ANOTHER sql statement Commented Nov 11, 2016 at 16:55
  • The answer is the error message Commented Nov 11, 2016 at 16:56
  • Someone thought it wise to put a not null restriction on the factSales table for the column SaleValue. Change your insert to provide the appropriate saleVale! Askyourself why would that restriction be there, and why are you trying to insert a record without one. Are you doing something wrong by omiting the value or is the restriction no longer valid? Both of these are business questions that only you can really answer. Commented Nov 11, 2016 at 17:04

2 Answers 2

1

SalesValue does not allow nulls, so supply a zero.

Insert into Staged.dbo.factSales (Date,SaleValue)
Select SaleDate,0 from Staged.dbo.SaleDates
Sign up to request clarification or add additional context in comments.

5 Comments

Or supply '0' if is a string, You also can change your table to allow nulls for that field.
@JuanCarlosOropeza Correct. I made an assumption.
Why do I have to include SaleValue if I want to only want to affect a specific column? Also, SaleValue already contains valuable data so I cant just override it with a meaningless 0
@MPol because your fact table does not accept nulls in SalesValue. Without it, you are trying to add records which would have a null value in SalesValue.
Okay, so my sql command replaces all data, not just insert it?
0

Fixed using

update Staged.dbo.factSales set Staged.dbo.factSales.Date = Staged.dbo.SaleDates.SaleDate from Staged.dbo.factSales inner join Staged.dbo.SaleDates on Staged.dbo.factSales.SaleID = Staged.dbo.SaleDates.SaleID

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.