Cannot insert the value NULL into column where using Sql Select statement

后端 未结 3 687
挽巷
挽巷 2021-01-26 01:58

Can you tell me how to avoid below mentioned exception ?

 INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode)
     SELECT DISTINCT 
         a.City, a.ZipCode          


        
3条回答
  •  时光取名叫无心
    2021-01-26 02:19

    What the error message says is that the IsDeleted column is declared NOT NULL and it does not have a default value. In this case, you are probably inserting non-deleted records by default, so you might want to change column:

    alter table cities alter column IsDeleted int not null default 0;
    

    Alternatively, you can write the query to include the column:

    INSERT INTO [Migrated].[dbo].[Cities] (Name, ZipCode, IsDeleted)
        select  DISTINCT zl.City, zl.ZipCode, 
        from [Legacy].[dbo].[Ziplist] zl
        where a.City is not null and a.ZipCode is not null;
    

    These answers assume that IsDeleted is an integer with "0" for false. If the values are stored differently, then the code needs to be modified appropriately.

提交回复
热议问题