【问题标题】:How to get NULL as values for invalid column如何获取 NULL 作为无效列的值
【发布时间】:2015-04-29 13:32:26
【问题描述】:

我使用的是 Sql Server 2012。考虑表架构,

create table A (col1 int, col2 int)

我正在尝试执行此查询,

select col1, col2, col3, col4 from A

因为col3col4 不在表中,所以我收到执行错误。

但是有什么办法,这 2 列可以显示在结果中,NULL 作为每一行的值,即使它在表中不可用?

【问题讨论】:

    标签: sql sql-server sql-server-2012 ssms-2012


    【解决方案1】:

    为这两列中的每一列使用一个别名:

    select col1, col2, null as col3, null as col4 from A
    

    【讨论】:

      【解决方案2】:

      试试下面的选择查询..

      select col1, col2, null as col3, null as col4 from A
      

      【讨论】:

        【解决方案3】:

        将 Null 转换为所需的数据类型:

        select col1, col2, cast(null as int) as col3, cast(null as int) as col4 from A

        【讨论】:

          猜你喜欢
          • 2014-03-11
          • 1970-01-01
          • 2023-01-11
          • 1970-01-01
          • 1970-01-01
          • 2021-11-09
          • 1970-01-01
          • 2015-12-21
          • 1970-01-01
          相关资源
          最近更新 更多