【问题标题】:select null as testdate into #temp_table选择 null 作为 testdate 进入#temp_table
【发布时间】:2011-04-12 07:35:00
【问题描述】:

有没有办法将日期时间值 (getdate()) 插入/更新到创建的临时表中,如下所示:

select id, null as testdate
into #temp_table

然后再声明:

update #temp_table 
set testdate=getdate()

我得到错误:

无法将 datetime 转换为 int...

谢谢。

【问题讨论】:

    标签: sql sql-server tsql null temp-tables


    【解决方案1】:

    将选择中的列投射到

    select id, cast(null as datetime) as testdate
    into #temp_table
    

    【讨论】:

    • +1:没错。 SQL Server 中的 NULL 默认是一个 INT;您需要将 NULL 转换/转换为适当的数据类型,以使 OP 的东西正常工作。
    • +1:我总是更喜欢显式创建临时表的另一个例子。
    猜你喜欢
    • 2018-04-16
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多