Creating a Top One Link (Magic xpa 4.x)
How can we fetch only the top 1 record of a table using a Link Query?
Creating a Direct SQL task that fetches the top 1 record is easy, however, not always applicable.
For example: SELECT TOP 1 Employee_ID, Employee_Name FROM Employee ORDER BY Employee_ID DESC
The following steps are required:
-
Change the Change Tables in Studio database property to No.
Manipulations to the table definition on the Magic xpa side are required; these changes should not be reflected in the real database structure.
-
Get the definition of the table, for example an Employees table.
-
Duplicate the column you need to do the Top 1 statement on, for example the Employee_ID column.
-
Place one of the column entries as column #1 in the Magic xpa structure.
-
Change the column's DB Column name property from Employee_ID to: TOP 1 Employee_ID AS "MyID"
-
Change the field number on all indexes using the Employee_ID field. Instead of using the first column, use the second instance of the column (the copied one).
-
Use this table entry in a Link Query operation – it will fetch the first / last record (according to the index direction).
Note: This table entry can only be used to read data; no DMLs can be performed on it.