Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow temporary tables to accept null values

If you create temp tables using "insert into" in SQL Server it uses the first insert to determine whether a column accepts null value or not. if the first insert has null value the column become nullable otherwise it will be non-nullable.

Is there a way to create temp tables using "insert into" to accept null values?

Example

This works without any problem

Select 'one' as a , null as b
into #temp

insert into #temp
Select 'two' as a , 500 as b

However this throws "Cannot insert the value NULL into column 'b'"

Select 'one' as a , 500 as b
into #temp

insert into #temp
Select 'two' as a , null as b

I know I could do create Table or alter column statement but I want to do it without rewriting hundreds of the existing queries.

like image 228
Smith Avatar asked Mar 07 '13 06:03

Smith


1 Answers

How about this?

Select CONVERT(varchar(100), 'one') as a , CONVERT(int, 500) as b
into #temp

insert into #temp
Select 'two' as a , null as b

select * from #temp order by 1
like image 191
Robin Avatar answered Sep 20 '22 07:09

Robin