1

I've got this procedure working in TOAD/PLSQL but, would like the alias for the first column to be set to the field_name argument passed to the procedure. I think I need to build the query as a string like,

query := 'Select 1 as ' || field_name || ' From Dual';

But am not getting it right. Is what I have in mind possible?

Thanks and the working code I'm trying to modify is below.

Create or Replace Procedure Delete_Me(field_name NVarChar2)
as
result_set sys_refcursor;

BEGIN

open result_set for 

    Select
         Elapsed_Time((Select Start_Time From Temp_Time1)) as field_name
        ,To_Char(SysDate, 'HH12:MI:SS AM') as Time_Completed
        ,Elapsed_Time((Select Start_Time From Temp_Time0)) as Process_So_Far
    From
         Dual;

    DBMS_SQL.RETURN_RESULT(result_set);     

End;

After comment:

I pass the procedure a string and its valued is placed in, "field_name." I would like the alias of the first column to adopt the value of field_name. So if I call the procedure thusly:

BEGIN
DeleteMe('Random_Column_Name');
END;

The first column name would be called, "Random_Column Name." If I called the procedure this way:

BEGIN
DeleteMe('Different_Column_Name');
END;

The first column would be names, "Different_Column_Name."

After Dmitry's second comment: It doesn't mean anything. It's an example of what I've tried and failed to get to work.

3
  • Could you please edit your question and add an example of what you have now and what you want to get? Currently, it is hard to understand.
    – Dmitriy
    Commented Mar 12, 2019 at 13:37
  • The SQL for your cursor should be constructed by concatenating the alias variable with other parts.The variable for the alias should be outside quotes. Commented Mar 12, 2019 at 14:26
  • The string query := 'Select 1 as ' || field_name || ' From Dual'; - what does it mean? Where did you use it?
    – Dmitriy
    Commented Mar 12, 2019 at 22:47

2 Answers 2

1

I understand that you need to make a dynamic query, the way to do that is sometihing like this:

DECLARE
    TYPE ty_refcur IS REF CURSOR;

    c_mycur  ty_refcur;
    whatever varchar2(200) := 'whatever';
    my_query varchar2(500);

BEGIN
    my_query := 'Select ''hello''as '||whatever||' from dual';
    OPEN c_mycur FOR my_query;
      --whatever you want to do
    CLOSE   c_mycur;
END;
2
  • Excellent! This is the right idea but, I need it to be a procedure and return the results. And I'm sorry but, I'm not following the "Whatever I want to do." I want it to return the results of the my_query string you built. Thanks!
    – anonimitie
    Commented Mar 13, 2019 at 15:29
  • Hi, with --whatever you want to do i mean, you can copy and paste your own beheavor, the same you have in your question code. DBMS_SQL.RETURN_RESULT(result_set); about the procedure, the code that i paste is the same like the body of a procedure, only get out the DECLARE and it's a procedure body. Commented Mar 14, 2019 at 9:49
0

Here's what I finally came up with. Stanimir helped me understand how variables are used. Thanks for that!

Create or Replace Procedure Report_Elapsed_Time(field_name0 NVarChar2,
                                                field_name1 NVarChar2)
    as
    result_set sys_refcursor;
        query VarChar2(30000);
    BEGIN

        query := 'Select
                         Elapsed_Time((Select Start_Time From Temp_Time1)) as ' || Replace(field_name0, ' ', '_') || '
                        ,To_Char(SysDate, ''HH12:MI:SS AM'') as Time_Completed
                        ,Elapsed_Time((Select Start_Time From Temp_Time0)) as ' || Replace(field_name1, ' ', '_') || '
                  From
                         Dual';

        open result_set for query;

        DBMS_SQL.RETURN_RESULT(result_set);     

    End;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.