check-element-exists-in-collection

When you need to find out if a specific element exists in a collection or not, how do you go about it? Do you loop through the list till you find the value?

If that’s what you do, this post is for you.

Continue Reading …

{ 0 comments }

why-no-ora00904

Here’s a puzzle for you to solve. You’re given these two tables – empl and dept – in which column emp_dept_id of table empl references dept_id of table dept.

SQL> desc dept
 Name              Null?    Type
 ----------------- -------- ------------
 DEPT_ID                    NUMBER(2)
 DEPT_NAME                  VARCHAR2(6)

SQL> desc empl
 Name              Null?    Type
 ----------------- -------- ------------
 EMP_ID                     NUMBER(2)
 EMP_NAME                   VARCHAR2(6)
 EMP_DEPT_ID                NUMBER(2)

Note that the foreign key column names in the two tables is not identical. It’s called dept_id in table dept, emp_dept_id in table empl.

Continue Reading …

{ 3 comments }

Thumbnail image for Flatten Hierarchical Data using a Single SQL

Flatten Hierarchical Data using a Single SQL

Oracle has some neat ways of handling hierarchical data. A while back I wrote about how Oracle SQL can find the entire tree of data above or below a node in a hierarchy using the CONNECT BY clause. What if the requirement is to flatten hierarchical data? For example, given a table containing the employee [...]

Read the Full Article →
Thumbnail image for LISTAGG for Converting Rows to Comma-Separated String

LISTAGG for Converting Rows to Comma-Separated String

In an earlier post, we saw how Oracle SQL can be used to transpose data from rows to columns. It is a similar, though simpler, exercise to transpose data from rows to a comma-seperated list.

Read the Full Article →
Thumbnail image for Referencing User Defined Types over DBLink: Problem and Alternatives

Referencing User Defined Types over DBLink: Problem and Alternatives

Consider a PL/SQL call to a function which returns a user-defined type (UDT) – say, a SQL Type object. This works well as long as the called function resides in the same database as the calling PL/SQL, but if the called function is in a remote database, the PL/SQL call via DBLink end ups with [...]

Read the Full Article →
Thumbnail image for How to Cast PLSQL Function Output as View

How to Cast PLSQL Function Output as View

An Oracle view is typically like a window to the output of a SQL statement, but sometimes, a single SQL isn’t sufficient to get you the result you want. You may need to do some procedural data manipulation before you get the output you want. Does this mean, you cannot use an Oracle view if [...]

Read the Full Article →
Thumbnail image for Stop Overlapping Ranges in a Table

Stop Overlapping Ranges in a Table

Range-based definition of values is a common business scenario, as in the case of mortgage pre-payment penalty charges depending on "pre-payment amount" slabs, or incentives for a dealer depending on "business generated" slabs. A necessary validation is such cases is that the slabs should have no overlaps: each discrete value should match no more than [...]

Read the Full Article →
Thumbnail image for Using LEAD/LAG to Place a Value within Extents

Using LEAD/LAG to Place a Value within Extents

Take a table with the structure {<Category>, <Extent>}– say, a GRADE_MASTER table with two columns: {GRADE_CODE, SCORE_UPTO}. For examination scores in the range 0-100, GRADE_MASTER specifies the extent of score up to which a certain grade applies. To find the grade for a given the examination score, the SQL needs to compare the EXTENT values [...]

Read the Full Article →
Thumbnail image for Find Number of Rows in Each Table in a Schema

Find Number of Rows in Each Table in a Schema

How will you find out the number of rows in each table in a schema in Oracle? Here are two approaches with their pros and cons.

Read the Full Article →
Thumbnail image for What is the Signature of a PLSQL Program?

What is the Signature of a PLSQL Program?

In interviews for PLSQL developers, I usually ask a question of the form: If you write a procedure to do <specific operation>, what will be the signature of such a procedure? In response, the developer often veers off into describing the logic/body of the procedure, which is not what was asked. If you’re among those [...]

Read the Full Article →