【问题标题】:Datepart function error in Microsoft Dynamics SQLMicrosoft Dynamics SQL 中的 Datepart 函数错误
【发布时间】:2015-06-13 04:23:22
【问题描述】:

我目前正在尝试在 Microsoft Dynamics AX2012 中执行 SQL 查询,以使用 DATEPART 函数输出交货日期后的一年。

我在 AOT 中创建了一个类“CustRerportDemo”,并在尝试执行查询时,即仅从表“SalesTable”中的“deliverydate”字段中查询出年份。遇到错误提示:

变量Datepart尚未声明

我知道 datepart 是 SQL 中的函数调用,不需要声明。因此,我想知道为什么以及如何纠正这个问题?我只是想显示从交货日期算起的年份。

因此,如果日期是 2016 年 6 月 13 日,则查询结果将只是 2016 年或 16 年。 我附上了以下代码。请帮忙。

public void processReport()
{
CustTable custTable;
SalesTable salesTable;

//select all customers
while select * from custTable
{
    //clear the temporary table
    custReportRDPTmp.clear();
    //assign customer account and name
    custReportRDPTmp.CustAccount = custTable.AccountNum;
    custReportRDPTmp.Name = custTable.name();
    //select count of invoiced sales order of customer
    select count(RecId) from salesTable
    where salesTable.CustAccount == custTable.AccountNum
    && salesTable.SalesStatus == SalesStatus::Invoiced;
    custReportRDPTmp.SalesOrderInvoiceCount = int642int(salesTable.RecId);
    //New Column to display PaymentMode
    select PaymMode
    from salesTable
    where salesTable.PaymMode == custTable.PaymMode;
    custReportRDPTmp.Payment = SalesTable.PaymMode;
    //New Column to display SalesAmountTotal by drawing from a different table using a JOIN statement
    select smmSalesAmountTotal
    from salesTable;
    custReportRDPTmp.SalesAmt = salesTable.smmSalesAmountTotal;

    //New Column to display month from delivery date
    select DATEPART("yyyy", DeliveryDate) as year
    // To extract  a single value for year and month from DeliveryDate in SalesTable
    from salesTable

    /* where payment in (select count(payment) from salesTable
     where salesTable.CustAccount == custTable.AccountNum
    &&*/

    //insert in temporary table buffer
    custReportRDPTmp.insert();
}
}

编辑代码:

select firstOnly DeliveryDate
from salesTable
where salesTable.CustAccount == custTable.AccountNum;
//Get Year from date
custReportRDPTmp.DateTimeStamp = year(salesTable.DeliveryDate);

结果如图:

【问题讨论】:

    标签: sql axapta x++ dynamics-ax-2012 datepart


    【解决方案1】:

    X++ select 语句是 not SQL,因此您不应该假设任何或所有 SQL 函数在 X++ 中都可用;他们不是!

    个日期函数,你搜索的那个叫做year

    虽然它不属于选择:

    select firstonly DeliveryDate from salesTable;
    y = year(salesTable.DeliveryDate);
    

    【讨论】:

    • 是否需要通过变量“y”创建一个表来存储该字段的年份?感谢您的帮助
    • 另外,firstonly 不是表示只选择列中的第一个数据吗?
    • 您需要一个变量(y 或其他)来保存该值。如果您需要对值进行分组,则仅需要一个表字段。您甚至可以将报告中的日期字段格式化为仅显示年份。 firstonly 只选择一条记录,你应该做你需要的选择。
    • 是的,'firstonly' 只选择一条记录,但是,我理解它只会从顶部堆栈返回一条查询记录。与不同 i.ds 匹配的其他记录怎么样?
    • 使用while select salesTable where ... 选择匹配的订单。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多