Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

 

SelectStatement

=

select Parameters

Parameters

 

[ [  FindOptions  ] [  FieldList  from ] ] TableBufferVariable [ IndexClause ] [  Options  ] [  WhereClause  ] [  JoinClause  ]

FindOptions

=

crossCompany | reverse | firstFast | [ firstOnly | firstOnly10 | firstOnly100 | firstOnly1000 ] | forUpdate | noFetch | [forcePlaceholders |forceLiterals] | forceselectorder | forceNestedLoop | repeatableRead | validTimeState

FieldList

=

Field  { ,  Field  } | *

Field

=

Aggregate  (  FieldIdentifier  ) |  FieldIdentifier

Aggregate

=

sum | avg | minof | maxof | count

Options

=

[ order by , group by ,  FieldIdentifier  [ asc | desc ] { ,  FieldIdentifier  [ asc | desc ] }] | [  IndexClause  ]

IndexClause

=

index  IndexName | index hint  IndexName

WhereClause

=

where  Expression

JoinClause

=

[exists | notexists | outer ] join  Parameters

Keywords Used in the Select Syntax

 
 

Keyword

Description

Example

asc

An option on the order by or group by clause. The sorting is ascending. (Sort is ascending by default.)

select * from custTable

order by Name asc;

avg

Returns the average of the fields.

CustTable custTable;

;

select avg(value) from custTable;

print custTable.value;

count

Returns the number of records.

CustTable xCT;

int64 iCountRows; ;

Select COUNT(RecID) from xCT;

iCountRows = xCT.RecID;

crossCompany

Returns data for all companies that the user is authorized to read from. (Acontainer can be added to reduce the number of companies involved.)

CustTable custTable;

container conCompanies = ['dat','dmo'];

;

select crossCompany :conCompanies

* from custTable;

desc

An option on the order by or group by clause. The sorting is descending.

select * from custTable

order by Name desc;

exists

A method that returns a Boolean value and a join clause.

while select AccountNum, Name from custTable

order by AccountNum

exists join * from ctr

where (ctr.AccountNum ==

custTable.AccountNum)

firstFast

A priority hint. The first row appears more quickly but the total return time for this option might be slower. The firstFast hint is automatically issued from all forms, but is rarely used directly from X++.

select firstFast custTable

order by AccountNum;

firstOnly

Speeds up the fetch. Instructs MorphX to fetch only the first record.

static InventTable find(

ItemId   itemId,

boolean  update = false)

{

InventTable inventTable;

;

inventTable.selectForUpdate

(update);

if (itemId)

{

select firstonly inventTable

index hint ItemIdx

where inventTable.itemId

== itemId;

}

return inventTable;

}

firstOnly10

Same as firstOnly, except returns 10 rows instead of one.

 

firstOnly100

Same as firstOnly, except returns 100 rows instead of one.

 

firstOnly1000

Same as firstOnly, except returns 1000 rows instead of one.

 

forceLiterals

Note
You are advised not to use the forceLiterals keyword in X++ select statements, because it could expose code to an SQL injection security threat.

forceLiterals instructs the kernel to reveal the actual values that are used in whereclauses to the Microsoft SQL Server database at the time of optimization.

forceLiterals and forcePlaceholders are mutually exclusive.

 

forceNestedLoop

Forces the Microsoft SQL Server database to use a nested-loop algorithm to process a particular SQL statement containing a join algorithm. This means that a record from the first table is fetched before any records from the second table are fetched. Typically, other join algorithms, such as hash-joins and merge-joins, would be considered. This keyword is often combined with the forceSelectOrder keyword.

while select forceSelectOrder

forceNestedLoop inventTransThis

index hint TransIdIdx

where inventTransThis.InventTransId

== inventTrans.InventTransId

&& inventTransThis.StatusIssue

<= StatusIssue::ReservOrdered

forcePlaceholders

Instructs the kernel not to reveal the actual values used in where clauses to the SQL Server database at the time of optimization. This is the default in all statements that are not join statements.

The advantage of using this keyword is that the kernel can reuse the access plan for other similar statements with other search values. The disadvantage is that the access plan is computed without taking into consideration that data distribution might not be even. The access plan is an on-average access plan.

forcePlaceholders and forceLiterals are mutually exclusive.

static void forcePlaceHoldersExample(Args _args)

{

SalesTable salesTable;

SalesLine salesLine;

;

while select forcePlaceholders salesLine

join salesTable

where salesTable.SalesId ==

salesLine.SalesId

&& salesTable.SalesId == '10'

{

//more code

}

}

forceSelectOrder

Forces the SQL Server database to access the tables in a join in the specified order. If two tables are joined, the first table in the statement is always accessed first. This keyword is often combined with the forceNestedLoop keyword.

display ForecastHasPurch hasForecastPurch()

{

ForecastPurch   forecastPurch;

InventDim       inventDim;

;

select firstOnly forcePlaceholders

forceSelectOrder recId

from forecastPurch

index hint ItemIdx

where forecastPurch.itemId == this.itemId

exists join inventDim

index hint DimIdIdx

where inventDim.inventDimId ==

forecastPurch.inventDimId

&& inventDim.configId == this.configId;

return forecastPurch.recId;

}

forUpdate

Selects records exclusively for update. Depending on the underlying database, the records may be locked for other users.

ttsBegin;

while select forUpdate ledgerJournalTrans

index hint NumVoucherIdx

where ledgerJournalTrans.journalNum ==

_journalNum &&

ledgerJournalTrans.voucher == _voucher

{

ledgerJournalTrans.doDelete();

counter++;

}

if (counter

&& ledgerJournalTable.journalType

!= LedgerJournalType::Periodic)

{

NumberSeq::release(

ledgerJournalTable.voucherSeries,

_voucher);

}

ttsCommit;

group by

Instructs the database to group selected records by fields.

CustTable custTable;

;

while select sum(CreditMax) from custTable

group by CustGroup

{

print custTable.CustGroup, " ",custTable.CreditMax;

}

index

Instructs the database to sort the selected records as defined by the index.

CustTable custTable;

;

while select AccountNum, Name from custTable

index AccountIdx

{

print custTable.AccountNum, " ", custTable.Name;

}

index hint

Gives the database a hint to use this index to sort the selected records as defined by the index. The database can ignore the hint.

Note
A wrong index hint can have a big performance impact. Index hints should only be applied to SQL statements that do not have dynamic where clauses or order byclauses, and where the effect of the hint can be verified.

while select forUpdate ledgerJournalTrans

index hint NumVoucherIdx

where ledgerJournalTrans.journalNum

== _journalNum

join

Used to join tables on a column that is common to both tables. The join criteria are specified in the where clause because there is no on keyword in X++ SQL.

Reduces the number of SQL statements that are needed if you want to loop through a table and update transactions in a related table.

For example, if you process 500 records in a table, and want to update related records in another table, and use a nested while select to do this, there will be 501 trips to the database. If you use a join, there will be a single trip to the database.

while select ledgerTable

join ledgerTrans

where ledgerTrans.accountNum ==

ledgerTable.accountNum

{

amountMST += ledgerTrans.amountMST;

}

maxof

Returns the maximum of the fields.

CustTable custTable;

;

select maxof(CreditMax) from custTable;

minof

Returns the minimum of the fields.

CustTable custTable;

;

select minof(CreditMax) from custTable;

noFetch

Indicates that no records are to be fetched at present. This is typically used when the result of the select is passed on to another application object, for example, a query that performs the actual fetch.

select noFetch custTable

order by AccountNum

notExists

Chosen only if there are no posts.

while select AccountNum, Name from custTable

order by AccountNum

notExists join * from ctr

where (ctr.AccountNum ==

custTable.AccountNum)

optimisticLock

Forces a statement to run with Optimistic Concurrency Control even if a different value is set on the table.

For more information, see Optimistic Concurrency Control.

select optimisticLock custTable

where custTable.AccountNum > '1000'

order by

Instructs the database to sort the selected records by fields in the order by list.

select * from custTable

order by accountNum desc

where custTable.AccountNum > "100";

outer

Returns all rows from the first-named table, including rows that have no match in the second-named table. This is a left outer join, although there is no left keyword. There is no right outer join in X++ SQL.

 
while select AccountNum
 from custTable
 order by AccountNum
 outer join * from custBankAccount
 where custBankAccount.AccountNum ==
    custTable.AccountNum
{
 print custTable.AccountNum,
    " , ", custBankAccount.DlvMode;
}
pause;

pessimisticLock

Forces a statement to run with Pessimistic Concurrency Control even if a different value is set on the table.

For more information, see Optimistic Concurrency Control.

select pessimisticLock custTable

where custTable.AccountNum > '1000';

repeatableRead

Specifies that no other transactions can modify data that has been read by logic inside the current transaction, until after the current transaction completes.

An explicit transaction completes at either ttsAbort or at the outermost ttsCommit.

For a stand-alone select statement, the transaction duration is the duration of theselect command. However, the database sometimes enforces the equivalent ofrepeatableRead in individual select statements even without this keyword appearing in your X++ code (depending on how the database decides to scan the tables).

For more information, see the documentation for the underlying relational database product.

reverse

Records are returned in reverse order.

select reverse custTable

order by AccountNum;

sum

Returns the sum of the fields. Can be used to sum all accounts, order lines, and so on.

CustTable custTable;

;

select sum(CreditMax) from custTable;

validTimeState

Filters rows from a table that has its ValidTimeStateFieldType property set to a value other than None. For more information, see Valid Time State Tables and Date Effective Data.

 
static void VtsJob1(Args _args)
{
// A VTS table in AX 2012.
CustPackingSlipTransHistory xrec1;
utcDateTime myDateFrom , myDateTo;
anytype myAnytype = -1; myDateFrom = DateTimeUtil::utcNow();
myDateTo = myDateFrom; SELECT
validTimeState(myDateFrom, myDateTo)
*
FROM xrec1; myAnytype = xrec1.getFieldValue("RecId");
info(myAnytype);
}

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. get your copy at the MS Press Store

Community Additions

ADD

 

'like' example

As of AX 2012 it is possible to use the 'like' keyword where a '*' means 0 to many, a '?' means exactly 1.

SELECT * from tCustTable order by tCustTable.AccountNum desc where (!(tCustTable.Name like '*i*i*') && tCustTable.Name like 'T?e *')

See: http://msdn.microsoft.com/en-us/library/dd261457.aspx

Select Statement Syntax [AX 2012]的更多相关文章

  1. Temporary TempDB Tables [AX 2012]

    Temporary TempDB Tables [AX 2012] 1 out of 4 rated this helpful - Rate this topic Updated: November ...

  2. Following a Select Statement Through Postgres Internals

    This is the third of a series of posts based on a presentation I did at the Barcelona Ruby Conferenc ...

  3. Overview of Form Control Types [AX 2012]

    Overview of Form Control Types [AX 2012] Other Versions 0 out of 1 rated this helpful - Rate this to ...

  4. Using Controls in a Form Design [AX 2012]

    Using Controls in a Form Design [AX 2012] This topic has not yet been rated - Rate this topic Update ...

  5. Hosting custom WPF calendar control in AX 2012

    原作者: https://community.dynamics.com/ax/b/axilicious/archive/2013/05/20/hosting-custom-wpf-calendar-c ...

  6. Temporary InMemory Tables [AX 2012]

    Temporary InMemory Tables [AX 2012] This topic has not yet been rated - Rate this topic Updated: Oct ...

  7. Table Properties [AX 2012]

    Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Ap ...

  8. SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)

    SQL Fundamentals || Oracle SQL语言 Capabilities of the SELECT Statement(SELECT语句的功能) Data retrieval fr ...

  9. How to: Debug X++ Code Running in .NET Business Connector [AX 2012]

    This topic has not yet been rated - Rate this topic  http://msdn.microsoft.com/EN-US/library/bb19006 ...

随机推荐

  1. CodeForces Round 198

    总体感觉这次出的题偏数学,数学若菜表示果断被虐.不过看起来由于大家都被虐我2题居然排到331,rating又升了74.Div2-AA. The Walltime limit per test1 sec ...

  2. BZOJ4289 : PA2012 Tax

    一个直观的想法是把每条边拆成两条有向边,同时每条有向边是新图中的一个点.对于两条边a->b与b->c,两点之间连有向边,费用为两条边费用的最大值.然后新建源点S与汇点T,由S向所有起点为1 ...

  3. storm源码之一个class解决nimbus单点问题【转】

    本文导读: storm nimbus 单节点问题概述 storm与解决nimbus单点相关的概念 nimbus目前无法做到多节点的原因 解决nimbus单点问题的关键 业界对nimbus单点问题的努力 ...

  4. storm环境搭建(前言)—— 翻译 Setting Up a Development Environment

    Setting Up a Development Environment 搭建storm开发环境所需步骤: Download a Storm release , unpack it, and put ...

  5. No FileSystem for scheme: 远程访问HDFS找不到shceme

    问题描述: hadoop版本:hadoop-2.0.0-cdh4.3.0 在本地环境下能够找到scheme,但是通过maven打包fatjar 后放到其他机器上就出现找不到scheme. 看了代码,发 ...

  6. version `GLIBC_2.17' not found

    @ 今天把一个linux程序布到线上服务器上时,运行不起来,下面是解决思路. @ 运行程序,报以下错误 root@iZ25uck2l28Z:/tmp/tmp# ./speed_test ./speed ...

  7. QQ空间分享功能(二)

    http://sns.z.qq.com/tools/share/demo_html.jsp  手机QQ空间分享功能接入指引: 1.请求地址: http://sns.z.qq.com/share 2.请 ...

  8. javamail发送邮件的简单实例

    今天学习了一下JavaMail,javamail发送邮件确实是一个比较麻烦的问题.为了以后使用方便,自己写了段代码,打成jar包,以方便以后使用.呵呵 以下三段代码是我的全部代码,朋友们如果想用,直接 ...

  9. li:hover在ie6下失效的解决方案

    li:hover在ie6下是无效的,它只在ie7以下版本有效.要解决这个问题有两个解决方法.一个是用js来解决,但是这种方法我不喜欢,因为它必需把js代码和css代码都放在html文件中.第二种是在每 ...

  10. Git补丁

    引子: 上班有问题没有解决,在家里搞定了,于是把改动打成一个补丁,明天应用到公司的工作电脑上.(以下内容转自别处) 1.创建补丁,比如把最新的两次提交纪录转化为补丁文件,可以用如下命令: git fo ...