Microsoft T-SQL to Oracle SQL translation - sql

I've worked with T-SQL for years but I've just moved to an organisation that is going to require writing some Oracle stuff, probably just simple CRUD operations at least until I find my feet. I'm not going to be migrating databases from one to the other simply interacting with existing Oracle databases from an Application Development perspective. Is there are tool or utility available to easily translate T-SQL into Oracle SQL, a keyword mapper is the sort of thing I'm looking for.
P.S. I'm too lazy to RTFM, besides it's not going to be a big part of my role so I just want something to get me up to speed a little faster.

The language difference listed so far are trivial compared to the logical differences. Anyone can lookup NVL. What's hard to lookup is
DDL
In SQL server you manipulate your schema, anywhere, anytime, with little or no fuss.
In Oracle, we don't like DDL in stored procedures so you have jump through hoops. You need to use EXECUTE IMMEDIATE to perform a DDL function.
Temp Tables
IN SQL Server when the logic becomes a bit tough, the common thing is to shortcut the sql and have it resolved to a temp table and then the next step is done using that temp table.
MSSS makes it very easy to do this.
In Oracle we don't like that. By forcing an intermediate result you completely prevent the Optimizer from finding a shortcut for you. BUT If you must stop halfway and persist the intermediate results Oracle wants you to make the temp table in advance, not on the fly.
Locks
In MSSS you worry about locking, you have nolock hints to apply to DML, you have lock escalation to reduce the count of locks.
In Oracle we don't worry about these in that way.
Read Commited
Until recently MSSS didn't fully handle Read Committed isolation so you worried about dirty reads.
Oracle has been that way for decades.
etc
MSSS has no concept of Bitmap indexes, IOT, Table Clusters, Single Table hash clusters, non unique indexes enforcing unique constraints....

I get the impression most answers focus on migrating an entire database or just point to some differences between T-SQL and PL/SQL. I recently had the same problem. The Oracle database exists, but I need to convert a whole load of T-SQL scripts to PL/SQL.
I installed Oracle SQL Developer and ran the Translation Scratch Editor (Tools > Migration > Scratch Editor).
Then, just enter your T-SQL, choose the correct translation in the dropdown-list (it should default to 'T-SQL to PL/SQL'), and convert it.

I have to things to mention.
1) When I worked on Oracle 8, you could not do "Select #Result", you had to instead use the dummy table as follows "Select #Result from dual". Not sure if that ridiculousness still exists.
2) In the Oracle world they seem to love cursors and you better read up on them, they use them all the time AFAICS.
Good luck and enjoy,
it is not that different to MS SQL. Thankfully, I do not have to work with it anymore and I am back in the warm comfort of MS tools.

If you replace your ISNULL and NVL nonsense with COALESCE, it'll work in T-SQL and PL/SQL!

It's not trivial to map them back and forth, so I doubt there's a tool that does it automatically. But this link might help you out: http://vyaskn.tripod.com/oracle_sql_server_differences_equivalents.htm

The most important differences for plain T-SQL are:
NVL replaces ISNULL
SYSDATE replaces GETDATE()
CONVERT is not supported
Identity columns must be replaced with sequences <-- not technically T- or PL/ but just SQL
Note. I assume you do not use the deprecated SQL Server *= syntax for joins
#jodonell: The table you link to is a bit outdated, oracle has become somewhat more standards compliant after 9i supporting things like CASE and ANSI outer joins

I have done a few SQL server to oracle migrations. There is no way to migrate without rewriting the backend code. Too many differences between the 2 databases and more importantly differences between the 2 mind sets of the programmers. Many managers think that the 2 are interchangeable, I have had managers ask me to copy the stored procedures from SQL server and compile them in oracle, not a clue! Toad is by far the best tool on the market for supporting an oracle application. SQL developer is ok but was disappointing compared to toad. I hope that oracle will catch their product up to toad one day but it is not there yet. Have a good day :) chances are if you are migrating to oracle it is for a reason and in order to meet that requirement you will need to rewrite the back end code or you will have many issues.

In Oracle SQL Developer, there is a tool called Translation Scratch Editor. You can find it from Tools > Migration.
The Oracle SQL Developer is a free download from Oracle and it is an easy install.

If you're doing a one-off conversion, rather than trying to support two versions, you must look at Oracle Migration Workbench. This tool works with Oracle's SQLDeveloper (which you really should have if you are working with Oracle). This does a conversion of the schema, data, and some of the T-SQL to PL/SQL. Knowing both well, I found it did about an 80% job. Good enough to make it worth while to convert the bulk of procedures, and hand convert the remainder "tougher" unknown parts.

Not cheap ($995) but this tool works great: http://www.swissql.com/products/sql-translator/sql-converter.html

A few people have mentioned here converting back and forward. I do not know of a tool to convert from MSSQL to Oracle, but I used the free MS tool to convert a Oracle db to MSSQL and it worked for me and converted a large db with no problems I can call. It is similar to the Access to MSSQL tool that MS also provide for free. Enjoy

jOOQ has a publicly available, free translator, which can be accessed from the website here: https://www.jooq.org/translate
It supports DML, DDL, and a few procedural syntax elements. If you want to run the translation locally via command line, a license can be purchased and the command line works as follows:
$ java -cp jooq-3.11.9.jar org.jooq.ParserCLI -t ORACLE -s "SELECT substring('abcde', 2, 3)"
select substr('abcde', 2, 3) from dual;
See: https://www.jooq.org/doc/latest/manual/sql-building/sql-parser/sql-parser-cli
(Disclaimer, I work for the vendor)

Related

Convert sql server script syntax to oracle script syntax

I have an application that is supposed to support two types of databases SQL SERVER and ORACLE. So we've been working forever on SQL Server and now we are making this support.
My idea was to create a tool to generate the scripts of creating the database using CMO then convert those scripts to PL/SQL Oracle scripts and run them on Oracle.
My questions are:
Is this syntax conversion possible in code?
I need a guideline to make this kind of syntax conversion.
Do you have a better suggestion to maintain two types of databases (i mean when making a change of one of them, we dont have to make it to the other. we need a tool to make that change.)?
If you write using standard sql it should be mostly portable. Eg use fetch instead of top, SET #a=.. instead of Select #a=.. Use Merge for updates instead of join updates, CURRENT_TIMESTAMP instead of getdate() etc.

Moving from Oracle SQL to ANSI SQL pros and cons

I work in a project where the UI has direct access to the database through SQL code. The company has a framework where we create UI pages in xml and after that it is parsed and creates FLEX pages. If we want some data from the DB (Oracle) we add a sql query in the xml (instead of databinding with a datacontext object like we could do with WPF). If we want to add some logic, there is no code behind, we call store procedures. After we have the data we need the parser does the job.
The new requirements are to use the framework and create a new product that will be compatible with SQL Server and the thoughts are to start transforming the (Oracle)SQL queries to ANSI SQL.
Can somebody tell me the benefits and mainly the problems that we are going to face doing that?
Do you think there is a better way?
Note: The framework is really big and there are a lot of products built on that so managers are not keen to just throw it away(I tried but.. :))
Each dialect of SQL is different. You could use ANSI SQL but a) not all of ANSI SQL is implemented by most DBMS and b) most DBMS's have implementation-specific optimisations which will perform better for some cases.
So I'd say, don't go for ANSI SQL. It won't always work and sometimes it will work slower than taking advantage of a vendor's non-standard implementations.
Specifically, Oracle requires a StoredProcedure to return a REF_CURSOR from a stored procedure to fill a DataSet. SQL Server doesnt; the SP returns what the sp SELECTed. You're going to have to change your SP's to get rid of the returned REF_CURSOR.
Date handling is quite different: Oracle needs a to_date to turn a string into a date in where clauses etc; SQL Server just takes the string and converts it for you. And so on and so on. (I'm not at all sure what the ANSI Standard is, or even if it covers this!) To avoid changing your SQL you could add create SQL Server function called to_date, but this is now going to slow up your SQL.
If you have much PL/SQL in stored procedures, you have a big job converting it to T-SQL. They are quite different.
Good luck!

Generate SQL queries with Ruby

I want an easy way to generate SQL queries in Ruby. I know all about ActiveRecord, Sequel and DataMapper. I'm not looking for an ORM but just an easier way to generate SQL statement strings.
I'm using RBHive to run Hive queries and would like an easy way to generate the Hive query statements.
Surely this exists (AR, etc) and I've looked at Arel, which seems promising. But can't figure out how to strip the SQL statement generation pieces off of the popular ORM libraries. Everything requires a connection to a database server.
Currently I just use raw SQL strings, but I want to get away from that as the queries are becoming more and more complex and error-prone.
Any ideas?
You said
Everything requires a connection to a database server.
I think, at least with Sequel you can use a dummy Database:
require 'sequel'
DB = Sequel::Database.new() #-> <Sequel::Database: >
puts DB[:test].sql #-> SELECT * FROM "TEST"
You're right, you're looking for Arel, which is used to build an AST for SQL statements and does not require a database connection.
The syntax is pretty different from what you're used to seeing, though. So be aware.
Read the README here to see if it makes sense to you:
https://github.com/rails/arel

will the sql queries i run in ms-access also work on mysql without any changes?

will the sql queries i run in ms-access also work on mysql without any changes ?
It's possible, but it depends on what the queries use. Date and string functions are the most likely to cause problems when porting queries.
The DATEDIFF keyword is supported on both Access & MySQL, but the function takes different parameters:
Access: DATEDIFF
MySQL: DATEDIFF
Well, if the coder wrote the queries with portability in the forefront of their mind then there's a good chance that you will need to make only minimal changes. However, you could only expect the most simple queries to work with no changes, regardless of which SQL product were involved.
In an ideal world, all SQL products would comply with ISO/ANSI Standard SQL with vendor extensions. In reality, while mySQL generally has a good track record in Standard SQL compliance, the Access Database Engine's record is rather poor -- it still doesn't even conform to entry level SQL-92, which was a fairly fundamental requirement even a decade ago (and seemingly none too difficult to achieve either).
[Your question is in all lower case. I've assumed by 'queries' you mean SQL DML SELECT. If you use 'queries' to mean INSERT/UPDATE/DELETE SQL DML plus SQL DDL and SQL DCL then this changes the answer. You should note the the Access Database Engine's UPDATE SQL DML is proprietary and non-deterministic; further, it does not support SQL-92's scalar subquery syntax. This is of major significance when porting to a SQL product.]
Thanks for your question. It just goes to show that it's worth considering portability from day one.
I would like to add one more point to OMG Ponies answer
Transform that is use for cross tab queries in MS ACCESS cannot be used in MySQL
e.g.
TRANSFORM Sum([M_Sales].[Amount]) AS SumOfAmount
SELECT [M_Sales].[Department]
FROM M_Sales
GROUP BY [M_Sales].[Department]
PIVOT Format([M_Sales].[Sale_date],"mmm") In ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
in MSACCESS ( taken from )
could be something in MySql Common MySQL Queries . Just visit the Pivot table section
Given some of your previous questions, you could save some time with MySQL, compared to Access: 12.1.10. CREATE TABLE Syntax

Introduce me to Oracle PL/SQL extensions

I'm an accomplished user of SQL; I'm confident creating schema and optimizing queries in ANSI-SQL, for Sybase, MS SQL Server, MySQL, or postgresql. I understand joins, subqueries, indexes, etc., so I don't need to recapitulate any of that that isn't different under Oracle.
I'll be taking a job that'll require using Oracle. Point me to online resources designed not as an "Intro to SQL for Oracle" but ones that explain "PL/SQL for people who already understand SQL".
I'm especially interested in the following: a concise guide to PL/SQL extensions, and optimizing Oracle queries.
Oracle® Database PL/SQL User's Guide and Reference (10g) Really, what more could you want?
If you are new to Oracle, I'd also suggest you spend a bit of time learning its transaction model, as it is subtly different to SQL Server, which could bite you. Here is a good article on it. The other poster who suggested reading Kyte is spot on.
You should be aware that PL/SQL is a more-or-less complete programming language. Crazy people like me create applications where most of the work is done in PL/SQL packages on the server.
asktom.oracle.com would be useful to browse.
I suggest you investigate the following powerful features:-
analytic functions
materialized views
pipelined PL/SQL functions
Read for the concepts: http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/toc.htm
Read articles and books by Steve Feuerstein, he is one of the main evangelists of PL/SQL.
Focus on their PL/SQL data structures like cursors, associative arrays, tables(PL/SQL not SQL), etc.
One thing to keep in mind with googling Oracle code, you will run into old code a lot more than you will new code. Try to learn new rather than old.
For example the old way of looping over a query is to create a cursor and then perform a while loop over it until it returns nothing. Now days you can implicitly create a loop like this:
FOR Test IN (Select Ct from TableCT) LOOP
Sum := Sum + Test.Ct;
END LOOP;
For optimizing queries, get SQL Developer and run the explain plan. It may take a while to understand, but it's the best way that I found. Also, if you haven't run into it yet, the WITH clause works wonders for optimizing queries.