【问题标题】:Why does this SQL code not work in Microsoft Access but works in SQL Server Management Studio?为什么此 SQL 代码在 Microsoft Access 中不起作用,但在 SQL Server Management Studio 中起作用?
【发布时间】:2017-03-25 11:11:24
【问题描述】:

此代码在 Microsoft Access 中不起作用,但在 Microsoft SQL Server Management Studio 中起作用。我该怎么做才能在 Access 中完成这项工作?我一直收到一个

“查询表达式'CAST(od.UnitPrice * od.Quantity * (1 + od.Discount) as decimal(10,2))'Order Total'中的语法错误(缺少运算符)。

Select TOP 5 
    c.CompanyName as 'Company Name', 
    CAST(od.UnitPrice * od.Quantity * (1 + od.Discount) as decimal(10, 2)) 'Order Total'
From 
    Customers as c
Join 
    Orders as o On c.CustomerID = o.CustomerID
Join 
    OrderDetails as od On o.OrderID = od.OrderID
Where 
    od.UnitPrice * od.Quantity * (1 + od.Discount) > 5000 
    AND c.Country IN ('Austria', 'Denmark', 'Germany', 'Ireland', 'Sweden')
Order By 
    o.OrderDate desc

【问题讨论】:

  • 到底是什么错误?
  • @gurV cast 出错。我不知道为什么。

标签: sql-server ms-access


【解决方案1】:

我认为 CAST 在 Access 中无法正常工作(如果存在)。我会取出 CAST 并将其更改为 CDec() 函数之类的东西。您通常必须使用以下其中一种:https://support.office.com/en-ie/article/Type-Conversion-Functions-8ebb0e94-2d43-4975-bb13-87ac8d1a2202

【讨论】:

    【解决方案2】:

    你不能在 Access 中使用 CAST...试试这个选项:

    Cint -- 转换为整数

    Clng -- 长

    Cdbl -- 双倍

    Csng - 单人

    Cstr - 字符串

    Cbool - 布尔值

    CDec - 十进制

    示例:SELECT clng(fieldName) FROM tableName

    在你的情况下:

    Select TOP 5 
        c.CompanyName as 'Company Name', 
        CDec(od.UnitPrice * od.Quantity * (1 + od.Discount)) 'Order Total'
    From 
        Customers as c
    Join 
        Orders as o On c.CustomerID = o.CustomerID
    Join 
        OrderDetails as od On o.OrderID = od.OrderID
    Where 
        od.UnitPrice * od.Quantity * (1 + od.Discount) > 5000 
        AND c.Country IN ('Austria', 'Denmark', 'Germany', 'Ireland', 'Sweden')
    Order By 
        o.OrderDate desc
    

    希望对您有所帮助!

    【讨论】:

    • ...并且不要忘记将 CDec 转换为十进制,这是 OP 正在寻找的数据类型
    【解决方案3】:

    CAST 和 CONVERT 列函数似乎在 MS Access 中不起作用。

    尝试使用 CInt 或 CLng 函数,具体取决于您需要整数(16 位)还是长整数(32 位)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-08
      • 2023-03-25
      • 1970-01-01
      • 2012-08-24
      • 2010-09-14
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多