0

Im currently trying to move data from one table to another. In a previous question I asked today I was advised to use an insert statement. While trying to run the statement i get an error from SQL Server as follows:

Cannot insert the value NULL into column 'Parent', table '*****.dbo.Product'; column does not allow nulls. INSERT fails.

This is the statement I have created,

BEGIN TRANSACTION
INSERT INTO [*****].[dbo].[Product] 
([PDate]
  ,[SDate]
  ,[CreatedBy]
  ,[CreatedDate]
  ,[UpdatedBy]
  ,[UpdatedDate])
SELECT d.[PDate]
  ,d.[SDate]
  ,d.[CreatedBy]
  ,d.[CreatedDate]
  ,d.[UpdatedBy]
  ,d.[UpdatedDate]
FROM [*****].[dbo].[ProductData] AS d
JOIN [*****].[dbo].[Product] AS t
ON d.ProductDataID = t.ProductDataID
ROLLBACK TRANSACTION

I need to ensure that the data matches up correctly

Edit: Sorry, there was an error when i was copying the script. UseDRM is in both sections of my original script. What I want to do is to copy the data in the ProductData table to the Product table.

4

4 回答 4

3

The Parent column is not nullable and has not default value, so you need to add it to your query.

于 2012-06-22T11:31:23.130 回答
2

Try using ISNULL in you select query to check if the value is null & replace it with default value.

Eg.:

   BEGIN TRANSACTION
    INSERT INTO [*****].[dbo].[Product] 
    ([PDate]
      ,[SDate]
      ,[UseDRM]
      ,[CreatedBy]
      ,[CreatedDate]
      ,[UpdatedBy]
      ,[UpdatedDate])
    SELECT Isnull(d.[PDate], '1/1/1900')
      ,Isnull(d.[SDate], '1/1/1900')
      ,Isnull(d.[UseDRM], "")
      ,Isnull(d.[CreatedBy], "")
      ,Isnull(d.[CreatedDate], '1/1/1900')
      ,Isnull(d.[UpdatedBy], "")
      ,Isnull(d.[UpdatedDate], '1/1/1900')
    FROM [*****].[dbo].[ProductData] AS d
    JOIN [*****].[dbo].[Product] AS t
    ON d.ProductDataID = t.ProductDataID
    ROLLBACK TRANSACTION
于 2012-06-22T11:43:31.980 回答
2
BEGIN TRANSACTION
INSERT INTO [*****].[dbo].[Product] 
([PDate]
  ,[SDate]//Check Columns no here it is  6 total
  ,[CreatedBy]
  ,[CreatedDate]
  ,[UpdatedBy]
  ,[UpdatedDate])
SELECT d.[PDate] //Check columns numbers here it is 7 here 
  ,d.[SDate]
  ,d.[UseDRM]
  ,d.[CreatedBy] 
  ,d.[CreatedDate]
  ,d.[UpdatedBy]
  ,d.[UpdatedDate]
FROM [*****].[dbo].[ProductData] AS d
JOIN [*****].[dbo].[Product] AS t
ON d.ProductDataID = t.ProductDataID
ROLLBACK TRANSACTION

correct it then it will be ok d.[UseDRM] is not in insert

于 2012-06-22T12:01:39.700 回答
0

I finally figured out what i was doing wrong, instead of using an INSERT INTO statement I should have been using an UPDATE statement

UPDATE [********].[dbo].[Product] 
SET [PDate] = [********].[dbo].[ProductData].[PDate]
  ,[SDate] = [********].[dbo].[ProductData].[SDate]
  ,[CreatedBy] = [********].[dbo].[ProductData].[CreatedBy]
  ,[CreatedDate] = [********].[dbo].[ProductData].[CreatedDate]
  ,[UpdatedBy] = [********].[dbo].[ProductData].[UpdatedBy]
  ,[UpdatedDate] = [********].[dbo].[ProductData].[UpdatedDate]
FROM [********].[dbo].[ProductData], [********].[dbo].[Product]
WHERE [********].[dbo].[Product].[ProductDataID] = [********].[dbo].[ProductData].[ProductDataID]

I apologise for my orginial question which is poorly written

于 2012-06-22T13:35:02.520 回答