如果您在SQL Server中使用"insert into“创建临时表,它将使用第一个insert来确定列是否接受空值。如果第一个insert具有空值,则该列变为可空,否则它将不可为空。
有没有办法使用"insert into“创建临时表来接受空值?
示例
这样做没有任何问题。
Select 'one' as a , null as b
into #temp
insert into #temp
Select 'two' as a , 500 as b
但是,这会抛出“无法将NULL值插入到列‘b’中”
Select 'one' as a , 500 as b
into #temp
insert into #temp
Select 'two' as a , null as b
我知道我可以做create Table
或alter column
语句,但我希望不用重写成百上千的现有查询。
发布于 2014-08-12 02:08:33
这个怎么样?
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
发布于 2015-08-13 18:59:58
我可以通过在第一次插入之前显式创建临时表来解决这个问题。
create table #temp (a varchar(10) not null, b int null)
发布于 2020-10-08 22:18:07
(Un)幸运的是,这个问题太流行了,也出现在Sybase ASE 15.7的顶部,所以只需在这里添加我对Sybase的答案。
对我来说,cast、convert或coalesce都不起作用,但case语句起到了作用(这就是coalesce,但是...)
select
a = case when 1 = 0 then null else 'one' end,
b = case when 1 = 0 null else 500 end
into #temp
https://stackoverflow.com/questions/15264645
复制相似问题