Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which query works in both SQL Server and Oracle?

I am searching for a SELECT query that works in both SQL Server and Oracle. It does not matter what it selects or what the result is, but it cannot depend on the database contents.

This sounds like a simple thing to do, but I cannot come up with an example.

First I thought of SELECT 0, but Oracle needs FROM dual which SQL Server does not support. Then I tried SELECT * FROM INFORMATION_SCHEMA.TABLES, but Oracle does not have those...

Edit: this question arose trying to check whether some SQL Server linked servers are reachable. But I think it is very interesting by itself.

like image 634
Michel de Ruiter Avatar asked Jun 01 '18 12:06

Michel de Ruiter


People also ask

Are SQL and Oracle queries the same?

Oracle, meanwhile, uses PL/SQL, or Procedural Language/SQL. Both are different “flavors” or dialects of SQL and both languages have different syntax and capabilities. The main difference between the two languages is how they handle variables, stored procedures, and built-in functions.

Can you use SQL Server with Oracle?

You get access to data stored in Oracle right from the SQL server without the need for coding. You can perform different actions on data stored in Oracle from SQL Server, such as distributed queries. You can address data stored in Oracle the same way you would SQL Server.

What is the difference between Oracle SQL and SQL Server?

Oracle is owned by Oracle Corporation and can run on a wide variety of platforms such as Windows, Linux, Solaris, HP-UX, and OS-X. Oracle supports PL/SQL and SQL language to write queries to access data from its database. SQL Server is owned by Microsoft and can only be used on the Windows platform.

Are queries in MySQL and Oracle same?

MySQL and Oracle SQL are both RDBMSs (relational database management systems) owned by Oracle Corporation. MySQL is built to be primarily free and open-source, while Oracle is primarily built to be commercial and paid. MySQL is also more customizable than Oracle which is because Oracle is a finished product.


1 Answers

On rextester, the following works for both SQL Server and Oracle:

select count(*)
from sys.all_objects;

There may be a handful of other sys tables that this would work for.

EDIT:

I'm going to echo Jeroen's comment. If you want the same result as well, then add a clause that filters out all rows:

select count(*) as always_zero
from sys.all_objects
where 1 = 0;
like image 71
Gordon Linoff Avatar answered Sep 28 '22 08:09

Gordon Linoff