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

后端 未结 3 696
挽巷
挽巷 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:36

    Simply add the column to insert list and fixed value to select list.

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

提交回复
热议问题