选择null作为testdate进入#temp_table

Rod*_*Rod 9 sql t-sql sql-server null temp-tables

有没有办法将datetime value(getdate())插入/更新到如下所示的临时表中:

select id, null as testdate
into #temp_table
Run Code Online (Sandbox Code Playgroud)

然后声明:

update #temp_table 
set testdate=getdate()
Run Code Online (Sandbox Code Playgroud)

我收到错误:

无法将datetime转换为int ...

谢谢.

小智 16

在select into中插入列

select id, cast(null as datetime) as testdate
into #temp_table
Run Code Online (Sandbox Code Playgroud)

  • +1:没错.SQL Server中默认为NULL是INT; 你需要将NULL CAST/CONVERT到适当的数据类型,以便OP工作. (4认同)