James
James

Reputation: 1

how to copy data from one SQL column table to another SQL column table

Need assistance on the following. How can I copy data from one SQL column table to another sql column table?

I have the following tables

dbo.Thecat62 and dbo.thecase6

inside dbo.Thecat62 , I need to copy the column Work_Order_Job_Start_Date values to dbo.thecase6 column Job_Start_Date. Currently there are null value in the Job_Start_Date column in dbo.thecase6.

I have tried using the following command

INSERT INTO dbo.thecase6 (Job_Start_Date) SELECT Work_Order_Job_Start_Date FROM dbo.thecat62

but received the error Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6'; column does not allow nulls. INSERT fails. The statement has been terminated.

Any help will be great!

Thanks!

Upvotes: 0

Views: 391

Answers (2)

Dgan
Dgan

Reputation: 10285

Because on this table Therefore.dbo.TheCase6 for CaseNo you have specify Not NULL Constraints something like this

CaseNo int NOT NULL

But you did not select the CaseNo column from the dbo.thecat62 table, so you are explicitly trying to insert nulls into a non-nullable column.

You just need to select the CaseNo column, as well, presuming it does not contain any nulls in teh source table.

INSERT INTO dbo.thecase6 (Job_Start_Date,CaseNo) 
SELECT Work_Order_Job_Start_Date,CaseNo FROM dbo.thecat62

Upvotes: 2

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

The error says it has a column CaseNo which doesn't allow NULL.

Cannot insert the value NULL into column 'CaseNo', table 'Therefore.dbo.TheCase6';

You are inserting rows in the new table which will have just 1 column filled and rest of the columns will be empty

Either Alter the table in which you are inserting the data and allow the column to allow null values. Or if you don't want to allow null values, update the null values to some default values.

Upvotes: 1

Related Questions