Pergunta

I want to write some queries which can work in almost all the databases without any SQLExceptions. So, where can I get the ANSI standards to write the queries ?

Foi útil?

Solução

Not sure that'll help you.

Vendors are touch and go as far as standards implementation and often the standards themselves are imprecise enough such that you could never write a query that would work with all implementors.

For example, SQL 92 defines the concatenation operator as || but neither MySQL nor MSSQL use this (Oracle does). Vendor independent string concatenation is impossible.

Similarly, a standard escape character is not specified so how you handled that might not work in all vendors.

Having said that:

Outras dicas

From wikipedia:

The SQL standard is not freely available. The whole standard may be purchased from the ISO as ISO/IEC 9075(1-4,9-11,13,14):2008.

Nevertheless I would not advise you to follow this strategy because no database engine follows any SQL standard (SQL 99, 2003, etc.) to the letter. All of them take liberties in the way they handle instructions or define variables (for example, when comparing two strings different engines handle case sensitivity differently). A method that is very efficient with one engine can be terrible inefficient for another.

A suggestion would be to develop a standard group of queries and develop different classes that contain the specific implementation of that query for a certain target RDBMS.

Hope this helped

Check out the BNF of the core SQL grammars available at http://savage.net.au/SQL/

This is part of the answer - the rest, as pointed out by Kiranu and MattMitchell, is that different vendors implement the standard differently. No DBMS adheres perfectly to even SQL-92, though most are pretty close.

One observation: the SQL standard says nothing about indexes - so there is no standard syntax for creating an index. It also says nothing about how to create a database; each vendor has their own mechanisms for doing that.

The Sql-92 standard is probably the one you want to target. I believe it's supported most of the major RDBMSs.

Here is a less terse link. Sample content:

  • PostgreSQL Has views. Breaks standard by not allowing updates to views...
  • DB2 Conforms to at least SQL-92.
  • MSSQL Conforms to at least SQL-92.
  • MySQL Conforms to at least SQL-92.
  • Oracle Conforms to at least SQL-92.
  • Informix Conforms to at least SQL-92.

Something else you might consider, if you're using .NET, is to use the factory pattern in System.Data.Common which does a good job of abstracting provider specifics for a number of RDBMSs.

If you are trying to make a product that will work against multiple databases I think trying to only use standard sql is not the way to go, as other answers have indicated, due to the different 'interpretations' of the standard. Instead you should if possible have some kind of data access layer in your application which has different implementations specific for each database. Depending on what you are trying to do, there are tools such as Hibernate which will so a lot of the heavy lifting in regards to this for you.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top