【问题标题】:Conditional Computed Columns in SQL Server ViewsSQL Server 视图中的条件计算列
【发布时间】:2013-06-29 00:00:04
【问题描述】:

我正在尝试使用以下查询从现有表 Students 创建 SQL Server 2008 视图:

SELECT Std_ID, 
       Name, 
       Sub1, 
       Sub2, 
       IIf ((Sub1 + Sub2)>160, 'A',
       IIf ((Sub1 + Sub2)>100, 'B','C')) AS Legend 
FROM  dbo.Student

它给了我下面提到的错误。

函数参数列表中的错误:“>”无法识别。
无法解析查询文本。

我希望视图具有计算列Legend。查询在查询设计器中完美执行,但无法创建视图。请指教。

【问题讨论】:

    标签: sql sql-server-2008 view


    【解决方案1】:

    IIF 是 SQL Server 2012 中的一项新功能 (http://www.mssqltips.com/sqlservertip/2570/new-logical-functions-in-sql-server-2012-iif-and-choose/)。你不能在 2012 年设计并期望它在 2008 年发挥作用。

    【讨论】:

      【解决方案2】:

      试试这个?

         SELECT 
           Std_ID, 
           Name, 
           Sub1, 
           Sub2, 
           Legend = 
                 case 
                   when Sub1 + Sub2 > 160 then 'A'
                   when Sub1 + Sub2 > 100 then 'B'
                   else 'C'
                 end
          FROM  dbo.Student
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-10-19
        • 1970-01-01
        • 2016-08-09
        • 2011-05-04
        相关资源
        最近更新 更多