Link

Link is used to define joins between database tables to access, with single abbreviation, these tables with one Read or For instruction.

Syntax

 Link [MAINCLASS] With LINK_LIST As [LINKCLASS]Link [MAINCLASS] With LINK_LIST As [LINKCLASS] Where WHERE_CONDITIONSLink [MAINCLASS] With LINK_LIST As [LINKCLASS] Order By ORDER_EXPRESSIONSLink [MAINCLASS] With LINK_LIST As [LINKCLASS] Where WHERE_CONDITIONS Order By ORDER_EXPRESSIONS
 [CLASS]KEY_NAME = EXPRESSION_LIST[CLASS]KEY_NAME(INDEX_VALUE) = EXPRESSION_LIST[CLASS]KEY_NAME ~= EXPRESSION_LIST[CLASS]KEY_NAME(INDEX_VALUE) ~= EXPRESSION_LIST

With the syntax using =, the join done is a left outer join. With the syntax using ~=, the join done is an inner join (which is preferable, from a performance point of view, when possible):
* a left outer join means that if a line has no valid joined line, the main line will appear nevertheless
* an inner join will select only the lines that have a valid join.

Examples

 # First simple example# Access to X3 customer table and to information related to the creation user (that still exists)Local File BPCUSTOMER [BPC], AUTILIS [AUS]Link [BPC] With [AUS]CODUSR~=[BPC]CREUSR As [BPUSR]# Second exampleAccess to X3 customer, to creation user information and to modification user (when it exists)Local File BPCUSTOMER [BPC], AUTILIS [AUS], AUTILIS [AUS2]Link [BPC] With [AUS]CODUSR~=[BPC]CREUSR, [AUS2]CODUSR=[BPC]UPDUSR As [BPUSR2]# Third example (not based on standard Sage X3 tables) # Let's imagine we have a sales history table called HISTORY [HIS]# In this table, we store the key of the customer HISCUST, the key of the product HISPROD# and the key of a salesrep HISREP (not always filled)# The other tables we want to perform a join with are:# CUSTOMER [CUST] table: a main key CUSKEY (1 component CUSTCODE). It includes a COUNTRY code (COUNTRY)# PRODUCT [PROD] table: a main key PROKEY (1 component PROCODE)# SALESREP [SREP] table: a main key PROKEY (1 component PROCODE)# PRODDES [PRDE] table: product description per language. The key PRODES has 2 components (PRO,LANG)# The product descriptions are not necessarily available for all the languages# COUNTRY [COUN] table: a main key COUNKEY (1 component COUNCODE). Includes a language code (LANCODE).# CONDITION is a string that has been transmitted by a calling script. Every column of one of the joined# tables can possibly be present thereLocal File HISTORY [HIST], CUSTOMER [CUST], PRODUCT [PROD], SALESREP [SREP], COUNTRY [COUN], PRODDES [PRDE]# The link is here a unique instruction (this is why we have an '&' at the beginning of the next lines)# The order in which the join condition are given is important:# The join on [COUN] cannot be done before the join on [CUST] because it requires a column from [CUST]# The join on [PRDE] requires a column from [COUN] and can therefore not be done before [COUN] and [CUST]Link [HIS] With& [CUST]CUSKEY ~= [HIST]HISTCUST,& [PROD]PROKEY ~= [HIST]HISTPROD,& [SREP]REPKEY = [HIST]HISTREP,& [COUN]COUNKEY ~= [CUST]COUNTRY,& [PRDE]PRODES = [HIST]HISTPROD; [COUN]LANCODE& As [HISLNK]& Where evalue(CONDITION)& Order By Key KEYHIST=[HIST]HISTDATE;[CUST]CATEGORY;[HIST]CUSKEY;[PROD]PROKEY# Now we can use itFor [HISLNK]KEYHIS(1) Where [CUST]COUNTRY="USA" and [PROD]CATEG=1: # Combines with CONDITION# In this loop, we have [HIST], [CUST], [SREP], [COUN], [PRDE] on line# A loop is performed for every distinct HISTDATEFor [HISLNK]KEYHIS(2)# A loop is performed for a given HISTDATE, for every distinct customer CATEGORYFor [HISLNK]KEYHIS(3)# A loop is performed for a given HISTDATE and customer CATEGORY, for every distinct CUSKEYFor [HISLNK]KEYHIS(4)# A loop is performed for a given HISTDATE,CATEGORY,CUSKEY, for every distinct PROKEYFor [HISLNK]KEYHIS# A loop is performed for every record having the same HISDATE,CATEGORY,CUSKEY,PROKEY# [CUST]NAME, [PROD]NAME, [SREP]NAME are available here...Next...Next....Next...NextNext# Example 3# A customer can have up to 2 associated sales rep or not (the REP(0..1) columns can be empty)# But here, we want to select only the customers that have two sales representativesLocal File BCUSTOMER [BPC], SALESREP [SRE1], SALESREP [SRE2]# A not optimal link syntax would be the following:Link [BPC] whith [SRE1]REP0=[BPC]REP,[SRE2]REP0=[BPC]REP(1) As [LNK] Where [BPC]REP<>"" and [BPC]REP(1)<>""# The reason is that an external join is not optimal, and that an additional filtering condition is added# An optimal link syntax would be the following:Link [BPC] whith [SRE1]REP0~=[BPC]REP(0),[SRE2]REP0~=[BPC]REP(1) As [LNK]# Here, the join is faster, and the customers returned have mandatorily two existing sales representatives

Description

Link is used to define a set of joins between a main table and additional tables, specifying any selection and sort criteria. It is also meant to define an abbreviation (that will be called the link abbreviation) to access all these tables with a single Read or For instruction. The main table and the linked tables must first have been opened with a LocalFile instruction.

Comments

Associated errors

Error codeDescription
7A linked table is not opened.
20The main abbreviation is a link abbreviation.
21The key does not exist on a linked table.
28Abbreviation already used in a link condition.

See also

Read, File, Filter, Order By, Where, For, Write, Rewrite, Delete, Update, Columns.