2015-06-13 45 views
1

我當前正試圖在Microsoft Dynamics AX2012中執行SQL查詢,以通過使用DATEPART函數從傳遞日期輸出一年。Microsoft Dynamics SQL中的Datepart函數錯誤

我在AOT中創建了一個類「CustRerportDemo」,並試圖執行查詢,該查詢僅查詢表「SalesTable」中的字段「deliverydate」中的年份。我遇到提示出錯:

可變日期部分尚未聲明

我瞭解,日期部分是在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); 

結果:

As Seen under the DATETIMESTAMP: REturn value of 0

回答

2

X ++ SELECT語句 SQL,因此你不應該承擔任何或所有的SQL函數在X ++中可用;他們不是!

There are date function,您所搜索的那個函數名爲year

它不會在選擇雖然屬於:

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

我將需要的「Y」的變量存儲領域一年來創建一個表?此外,感謝您的幫助 – developer

+0

此外,「firstonly」是否意味着只選擇列中的第一個數據? – developer

+0

您將需要一個變量('y'或其他)來保存該值。如果需要對值進行分組,則只需要一個表字段。您甚至可以格式化報告中的日期字段以僅顯示年份。 'firstonly'只選擇一條記錄,你應該做你需要的選擇。 –