Insert Statement Check Is NULL SQL Server 2008 -


my small sql query importing data old database new one. new database not allow projectno, projectname or leaderid null value, code needs check if value null , if is, add in default values projectname , leaderid (projectno primary key of old table, cannot null).

as far can tell, have written case statement correctly keep getting following error:

cannot insert value null column 'projectname', table 'erp.dbo.project'; column not allow nulls. insert fails.

i using sql server 2008 well

insert [erp].[dbo].[project] ([projectid], [projectname], [leaderid])    select         projectno,         case            when projectname null 'unknown'         end,         case            when projectleaderid null 1         end             multitech.dbo.projects go 

using isnull

 insert [erp].[dbo].[project]        ([projectid]        ,[projectname]        ,[leaderid])  select projectno,        isnull(projectname, 'unknown'),        isnull(projectleaderid, 1)  multitech.dbo.projects 

add missing else , code work too. case documentation

else else_result_expression

is expression returned if no comparison operation evaluates true. if argument omitted , no comparison operation evaluates true, case returns null. else_result_expression valid expression.

 insert [erp].[dbo].[project]        ([projectid]        ,[projectname]        ,[leaderid])  select projectno,         case when projectname null 'unknown' else projectname end,         case when projectleaderid null 1 else projectleaderid end  multitech.dbo.projects 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -