Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Question on using For loop within SELECT statement in Stored Procedure

tptan86Jun 26 2013 — edited Jun 27 2013

Hi guys,

I have a question here.

I have a GridView to display, the records will be retrieved via Stored Procedure.

My GridView supoports multi column sorting.

The way i came out with is the parse the sort parameters over to the SP.

For example, i will pass these over...

sortColumn = "ID,NAME";

sortType = "ASC,DESC";

My SP will handle and split the variables into respective arrays.

My idea now is to use the array to provide the parameters for the ORDER BY clause in my statement.

Here's a rough sketch of my statement.

SELECT * FROM table_name where ID='123' ORDER BY...

     //For loop here formulate the order by clause

     For i in 1..array_count

          loop

               //concatenate and formulate the output

          end loop;

So my final query will be ORDER BY ID ASC, NAME DESC;

Right now i am also using a CASE to determine ASC and DESC.

I am also using DECODE to identify which are the columns parsed in by the application.

Is a for loop feasible in this case? If not, any alternatives? Pretty new with PL/SQL stored procedures.

Any help is greatly appreciated.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 25 2013
Added on Jun 26 2013
3 comments
805 views