0

I try to use variable in the alias.

Is it possible not to change to dynamic SQL???

For example

get_this_year --> this is the function call this year, so 2018.

CREATE OR REPLACE PROCEDURE do_activity
v_cur_year    VARCHAR2(11);

BEGIN
v_cur_year := get_this_year;


select t.1 AS v_cur_year -- I wanna use in here
FROM table1

END do_activity

how can I use Variable as alias.

Thanks

3
  • It's not possible Commented Aug 2, 2018 at 21:57
  • why you want to use it as an alias ? what is the purpose? Commented Aug 2, 2018 at 22:06
  • It seems you are trying to do two impossible things here (see my answer). Could you explain what you need to achieve? Specify more context. There may be a different solution, for your problem, than just an answer to the specific question you asked. (Remember to add that information in the original question, not just in comments.) Commented Aug 2, 2018 at 22:18

1 Answer 1

1

What you are asking for is not possible.

The column list, names, structure, etc. has to be known, when the query is parsed. Queries within PL/SQL are parsed, when PL/SQL code is parsed/compiled, so in your case, on procedure creation. (This obviously excludes dynamic queries, which are constructed in run-time, hence can't be parsed on PL/SQL compilation.)

You would have to use the dynamic SQL, to get the column name defined by the function result, but you already stated, that you do not want dynamic SQL.

There's a second issue with your code, although it may be a result of you simplifying the code. You are not capturing the query result in your procedure, which is obligatory in Oracle PL/SQL. You can't just run a query, and expect its result to be returned by running the procedure - it's not Transact-SQL. To return a data set from PL/SQL, you would have to write a tabular function (still, this would require stable data set structure, so no dynamic column naming) or you would have to use an OUT parameter of ref-cursor type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.