MPol
MPol

Reputation: 3

SQL Server - Insert into table, values from another table

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

Upvotes: 0

Views: 78

Answers (2)

MPol
MPol

Reputation: 3

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

Upvotes: 0

John Cappelletti
John Cappelletti

Reputation: 81930

SalesValue does not allow nulls, so supply a zero.

Insert into Staged.dbo.factSales (Date,SaleValue)
Select SaleDate,0 from Staged.dbo.SaleDates

Upvotes: 1

Related Questions