Arthur Mastropietro Arthur Mastropietro - 6 months ago 32
SQL Question

Alias field generated from subquery inside select

I have a select query like this:

SELECT
emp_des_company as 'Company',
(SELECT pav_des_valor FROM slt_pergunta_avaliacao pa, slt_avaliacao a WHERE pa.pav_idn_pergunta = 393 AND c.cas_idn_avaliacao = a.avl_idn_avaliacao)
FROM slt.case
...


How can i alias this subquery like this:

SELECT
emp_des_company as 'Company',
(SELECT pav_des_valor FROM slt_pergunta_avaliacao pa, slt_avaliacao a WHERE pa.pav_idn_pergunta = 393 AND c.cas_idn_avaliacao = a.avl_idn_avaliacao) as (SELECT per_des_descricao WHERE per_idn_pergunta = 393)
FROM slt.case
...


Thanks in advance.

Answer

You cannot simply use dynamic values for column aliases in static SQL. You will have to use a fixed value instead: AS column_name

One alternative that would make it possible is to create your query dynamically, look into prepared statements.