A good reference for Oracle PL/SQL - sql

What are the best references for Oracle PL/SQL? I have an excellent knowlege of SQL-92 and of MS-SQL extensions, but now I'm working with Oracle and I'm struggling to find good references for the PL/SQL language.
I am looking for references for the following:
Variable
Loops
Cursor
Packages
Trigger
Stored Procedures
Temporary Tables
Thanks so much.

As Klaus says the online documentation is pretty good. Start with the 2-Day Application Developer's Guide. If you're using 11gR2, you'll want to read the most recent version of the PL/SQL Language Reference. Newer Oracle releases have updated versions of the documents.
But if you want to buy a book, then Steven Feuerstein's Oracle PL/SQL Programming is the one.

One additional observation. You include temporary tables in a list of PL/SQL features. This is because you are used to the T-SQL way of doing things. In Oracle things are handled differently.
In Oracle we have global temporary tables. These are permanent database objects, like regular tables, but they are defined so that the data is held temporarily - either for the duration of a transaction or a session. The data in a global temporary table is only queryable by the session which populates it.
The point being that temporary tables are discussed in the SQL Reference, not the PL/SQL manual.

This reference is really good.

In addition to PL/SQL documentation that others have mentioned, the Oracle Database Concepts Guide is a good reference for finding out how Oracle works, including an overview of package, triggers, tables etc. I have posted a link to the 11.2 version, but you can find the docs for older versions here.

I've always found Tech on the Net to be good quick reference

Java2s has a good reference tutorial.
Here is the Oracle section: http://www.java2s.com/Book/Oracle/CatalogOracle.htm
Here is the Oracle PL/SQL reference catalog, easy to navigate: http://www.java2s.com/Code/Oracle/CatalogOracle.htm
Here is the Oracle PL/SQL tutorial section: http://www.java2s.com/Tutorial/Oracle/CatalogOracle.htm
I have the OReilly Book but won't post where I got that link along with Oracle Student workbook which is on Scribd.
And along with the Oracle references and tahiti oracle, this site was a good tutorial site for beginners. http://www.tutorialspoint.com/plsql/index.htm

Adding points to accepted answer:
1)Concepts for Database Developers
2) Temporary tables in stored procedure from AskTom
Sample code snippet from AskTom
open ref_cursor for
select *
from ( query you used to put into temp_1 ),
( query you used to put into temp_2 )
where join_conditions
in your procedure. You'll find that Oracle is much better at complex queries involving dozens (yes more then 16) tables -- without any issues whatsoever.

Related

How come SAS, Proc SQL, doesn't throw an error when I CREATE already existing tables

SAS Beginner here.
INTRO:
In SQL Server Management Studio an error is thrown when you try to CREATE an object/table that already exists. So the solution is to first DROP the table and then CREATE it.
So how come in SAS I can write something like this... (pretending that the Example table exists)
PROC SQL;
CREATE TABLE Example AS
SELECT *
FROM Work.Test;
QUIT;
QUESTION:
Even though the Example object/table already exists, no error is thrown. Does anyone know why SAS or SQL Server Management Studio are different in this regard?
SAS has a default OPTION REPLACE, which tells SAS that you would like to allow this behavior.
If you wish to disable this feature, set OPTION NOREPLACE. As the documentation above states, this would not prevent the above from executing, however, because it is in the work library (which is a temporary library).
As to the why (why things are different), it is undoubtedly due to historical differences in how the languages are used. SAS isn't really a database language (though it shares heavily with them, including the built-in PROC SQL); it's also substantially older than SQL Server (origins in the 1960s and even before in PL/1 and PL/2). SQL Server gives you lots of tools for updating a table in place; SAS has many of those tools, but for various reasons SAS programmers tend to replace tables rather than updating them in place.

How to move a table from system schema to scott schema in Oracle?

In some days ago...i was converting some Large MySQL Database to Oracle 10g R2 by using Oracle SQL Developer database migration tools.But unfortunately it was migrated on system schema.but i need to it on scott schema.
After Googling i found this two links OraFAQ Forum and ASK TOM Q&A.But I cannot find any appropriate answer. Any one Can help me to do , How it is possible.
Thanks In Advance.
IIRC the MySQL backup tool spits out plain SQL. As it would be in the form of fairly vanilla SQL -- just create and insert, I guess -- it ought to be able to be run against your Oracle schema with the minimum of alteration.
Having said that, in the SQL Developer migration wizard, the second step allows you to select the target schema. If you have a connection setup to scott, why doesn't that work for you?
If the table isn't too large (dependent upon your system resources and server horsepower etc.) then you could simply rebuild the table in the desired schema with the following.
N.B. You need to be logged in as either the target schema's user (with select permission on the table in the SYSTEM tablespace) or as system:
CREATE TABLE <newschema>.<tablename>
AS
SELECT *
FROM system.<tablename>;
Then remove the original table once the new table has been created.
If the table is large then you could use DATAPUMP to export and import it into the desired schema.
Here is an article on using Data Pump for this purpose:
http://oraclehack.blogspot.com/2010/06/data-pump-moving-tables-to-new-schema.html
Hope this helps

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.

Microsoft T-SQL to Oracle SQL translation

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)