Many people encounter problems when using the Cross-Connection Editor or Cross-Connection Query Builder to write a query. Sometimes they ask questions such as: What kind of SQL syntax should I use? Can I use the target database’s function in the query? These are all good questions. This article provides information that will help you understand how to use the correct syntax when writing a query in the Cross-Connection Editor or Query Builder.
1. Syntax for identifiers and strings in a cross-connection query
Identifiers (e.g., schema name, connection name, table name, owner name, alias name, etc.) | Use the backtick (backquote) character or nothing: `xxx` or xxx
(If xxx has a space in it, you must use the backtick character: `xxx`) |
For example: select col from `some_connection`.`some_schema`.`table1` |
---|---|---|
String | Enclose the string in single quotes: ‘xxx’
(Note: “xxx” is NOT supported) |
For example: select `col` from `some_connection`.`some_schema`.`table1` where `col` = ‘abc’ |
Note: Do not use double quotes (“xxx”) in a cross-connection query.
2. What syntax should I use?
In most cases, you can use MySQL syntax. But in some cases, it is recommended or even required to use the target database’s native syntax. It depends on how you write the query, what kind of target data source you are using, as well as some other factors.
To make things simple, this article provides several rules for you to follow. Now let’s start from the simplest query: a single-connection query.
2.1 Single-connection query
You may write a query in which the tables are all from a single data source
Rule 1: (Single-Connection) As long as the target data source supports SQL, an attempt is made to pushdown the whole query to the remote data source and execute the query there. So the target database’s SQL syntax is always recommended.
That is to say, if the target data source is Oracle, feel free to use Oracle functions in your query,
Rule 2: (Single-Connection) If the target data source is SQL Server, Oracle or Salesforce, you may even be allowed to use limited MySQL syntax in your query.
This limited MySQL syntax includes some commonly-used MySQL functions, “limit xxx,” and “order by xxx,” all of which are listed in Limited MySQL Syntax at the end of this article. Feel free to use them(no matter if they are marked by or not) for the specified data sources. If you use a function that is NOT on this list, the function is not translated. It is treated as the target database’s syntax and is pushed down.
Rule 3: (Single-Connection) For data sources that don’t support SQL, use MySQL syntax.
Tip: In order to improve query performance, please specify the column names that you really need to use and add some filters.
2.2. Cross-connection query
Users more often open the Cross-Connection Query Builder or Editor to do a query that uses multiple connections rather than a single connection.
It’s a little more complex, but some good examples can help you understand the rules.
Rule 4: (Multiple-Connection) The outer query of the “join query” should use MySQL syntax.
Tip: If you really need to use the target database’s syntax, reconstruct the query such that you use the target database’s syntax in a subquery. See Question 1 of 3.Troubleshooting.
Rule 5: (Multiple-Connection) In the subquery which can be pushed down to the remote database, try to use the target database’s syntax.
Tips: 1) If the subquery is a single-connection query, use syntax according to the rules in Single connection query. If not, use MySQL syntax.
2) How do you check whether or not a subquery can be pushed down?
Answer: All tables in the query must be from a single connection and the target data source must support SQL.
Rule 6: (Multiple-Connection) Check queries in an embedded query to see if each can be pushed down on its current level, without worrying about its subqueries or outer queries. If the query can be pushed down, try to use the target databases’s syntax.
Tip: If the current-level query is a single-connection query, use syntax according to the rules in Single connection query. If not, use MySQL syntax.
3. Trouble shooting
4. Useful Tips
1) In order to improve query performance, please specify the column names that you really need to use and add some filters. Otherwise, an attempt is made to fetch all data from the remote table.
2) If you find it difficult to use the Cross-Connection SQL Editor to write a query, you can use the Cross-Connection Query Builder. The Query Builder allows you to build a query without writing a SQL statement.
Additional Information
Limited MySQL syntax list used by target data source: SQL Server, Oracle, Salesforce
MySQL functions:
string functions | time functions | date functions | miscellaneous functions | aggregate functions | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
function name | translation has been done for: | function name | translation has been done for: | function name | translation has been done for: | function name | translation has been done for: | function name | translation has been done for: | ||||||||||
Oracle | SQL Server | Salesforce | Oracle | SQL Server | Salesforce | Oracle | SQL Server | Salesforce | Oracle | SQL Server | Salesforce | Oracle | SQL Server | Salesforce | |||||
SUBSTRING | NOW | YEAR | ISNULL | COUNT | |||||||||||||||
SUBSTR | CURRENT_TIMESTAMP | QUARTER | IFNULL | ||||||||||||||||
LENGTH | CURRENT_DATE | MONTH | TRIM | ||||||||||||||||
LOCATE | CURRENT_TIME | DAY | CONVERT_TZ | ||||||||||||||||
CONCAT | CURDATE | DAYOFMONTH | |||||||||||||||||
LEFT | CURTIME | HOUR | |||||||||||||||||
RIGHT | LOCALTIME | MINUTE | |||||||||||||||||
LOCALTIMESTAMP | SECOND | ||||||||||||||||||
DAYOFYEAR | |||||||||||||||||||
YEARWEEK | |||||||||||||||||||
WEEKOFYEAR | |||||||||||||||||||
WEEK | |||||||||||||||||||
WEEKDAY | |||||||||||||||||||
DAYOFWEEK | |||||||||||||||||||
MICROSECOND | |||||||||||||||||||
MONTHNAME | |||||||||||||||||||
DAYNAME | |||||||||||||||||||
TO_DAYS | |||||||||||||||||||
TO_SECONDS | |||||||||||||||||||
DATE_FORMAT | |||||||||||||||||||
ADDDATE | |||||||||||||||||||
ADDTIME | |||||||||||||||||||
DATE_ADD | |||||||||||||||||||
DATE_SUB | |||||||||||||||||||
DATEDIFF | |||||||||||||||||||
SUBDATE | |||||||||||||||||||
SUBTIME | |||||||||||||||||||
TIMESTAMPADD | |||||||||||||||||||
TIMESTAMPDIFF | |||||||||||||||||||
DATE | |||||||||||||||||||
TIME | |||||||||||||||||||
MAKEDATE | |||||||||||||||||||
MAKETIME | |||||||||||||||||||
TIMESTAMP |
limit xxx: translation has been done for SQL Server, Oracle
order by xxx: translation has been done for SQL Server
Start the discussion at forums.toadworld.com