Oracle

Oracle


Drop/Remove Column in Oracle Table

8.11.2010 | 0 Comments

Here’s a quick post on how to drop a column/field in a table.

Dropping One Column
alter table
table_name
drop column col_name1;

Dropping Multiple Columns

alter table
table_name
drop (col_name1, col_name2);

Simple, eh?


Convert Rows to One Column

8.07.2010 | 0 Comments

Converting rows to column is a breeze if you are using Microsoft Access or reporting tools like Oracle Reports Builder. However, there are times that you’ll also need it for something else. I mean like displaying rows to one column in a SQL*Plus report. So how leh? (imagine that saying with a Singaporean accent)
Read more…


Oracle IN : Maximum Number of Values

7.10.2010 | 0 Comments

In case you wonder what it the maximum number of values you can put inside the IN condition, it is 1000 as of Oracle 9i. It is documented in the link below.
http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96536/ch44.htm#288033.

Example:

SELECT * FROM emp
WHERE emp_no IN(1,2,3,4,5......1000);

I needed this because I was given a long list of values (around 500 values) and I need to fetch records in a table using that data. It would be easy if I can insert it first into a temp table and then join. Unfortunately, I only have read rights on the database so I need to use the IN condition. But I got concerned because in Oracle 7 and 8i releases, the maximum number of values you can put inside IN is a maximum of 255 (one byte).

Note:

Take note that the IN(SUBQUERY) does not have limitations because that’s essentially a join.

Example:
SELECT * FROM emp
WHERE emp_no IN(SELECT emp_no FROM dept_emp);

However as part of my best practices, I make sure that the results of the IN subquery is either one or none for optimal performance (not applicable in some instances).


Enable/Disable Bind Variable in SQL*Plus

1.18.2010 | 0 Comments

As state in the title, this post is about turning off the bind variable in SQL*Plus. Actually, this is also applicable in PL/SQL Developer and Toad but I’ll be discussing more on SQL*Plus. You see, I have a problem wherein whenever I load my backup data (saved as insert statements) and there are ampersand (&) or colon(:) somewhere in the script, SQL*Plus will ask me for an input. Of course, I don’t need that because as a backup, I want to load the data as it is. And when migrating 60MB or more, then it isn’t funny to update the query one by one. The good thing is, I don’t actually need to update the query because there’s an answer to my problem. And it’s pretty damn simple.

Syntax


set define off

Oh yes, that’s just one line that will solve my misery. Well, it’s all about my ignorance. To turn back on, then do the opposite as below.

Syntax


set define on


Oracle WITH Clause (Subquery Factoring)

12.17.2009 | 0 Comments

WITH clause was introduced in Oracle 9i to provide powerful new syntax for enhancing query performance. It optimizes query speed by eliminating redundant processing in complex queries.

WITH Clause Syntax

WITH
 alias_name         -- alias for the aggregate_query
AS
 (aggregate_query_here)
SELECT...            -- Beginning of the query main body

It should be noted that multiple aggregate queries can be defined in the WITH clause as below.

WITH
 alias_name1
AS
 (agg_query1) ,
 alias_name2
AS
 (agg_query2)
SELECT...

When using subquery factoring, think of the aggregate_query as an in-line view. Actually, it is a view but a temporary one. Instead of creating a permanent view accessible in the database, you are creating a temporary one exclusively used by your main query.

Usage

I primarily use this in creating adhoc queries, reports and extractions. Of course, you can just create a view to accomplish this task. However, there are times wherein you are not allowed to create additional objects in the database for one reason or another. In that case, this subquery optimization comes very handy.

Caveat

You can’t use this inside Oracle Forms. It also doesn’t work inside stored procedures or anything with PL/SQL in it. At least I’m talking about until 11g.