Skip to main content
Question

Vertica function with select clause

  • June 11, 2019
  • 5 replies
  • 14 views

mpedroso
Forum|alt.badge.img+1

Hello guys!

I need help with setting up a function. My intention is to pass to this function a set of parameters that will compose a select and the function will return the result of the select. Is it possible to do this in Vertica?

Thank you.

5 replies

marcothesane
Forum|alt.badge.img+1
  • Participating Frequently
  • August 22, 2019

Not at this time ... you can, though, code it as SQL generating SQL, and then call it:

vsql -v -v1="'schema'" -v v2="'table'" -AtXf yourscript.sql | vsql

A bit rough, but I do that all the time ...


marcothesane
Forum|alt.badge.img+1
  • Participating Frequently
  • August 22, 2019

note that the string parameter is a double quote, a single quote, then the string, then a single quote, then a double quote....


Sudhakar_B
Forum|alt.badge.img+1
  • Participating Frequently
  • August 23, 2019

@mpedroso,
Can you please explain your use case a bit more? It seems like, you want to generate dynamic SELECT statement for performance improvement? I used to do that in traditional DB like Oracle, DB2, and SQL Servers all the time. However in Vertica it is NOT required. Though as suggested by marcothesane, this can be done from shell using scripting or python, this is really not required.
Please give us more on why you want to do this. May be we can help more.
Thx.


mpedroso
Forum|alt.badge.img+1
  • Author
  • Participating Frequently
  • August 26, 2019

@all thanks for the replies you guys.

I am wanting to use a function in a select that queries a given history table and returns the value found in select.

Example:
select
*,
rescue_hist_charge (employee_code, date) lastcharge
from employees

Function:
var v1 int;

select cod_charge into v1 from hist_charge where employee_code = @ {employee_code} and start_date = @ {date}

return v1;


Sudhakar_B
Forum|alt.badge.img+1
  • Participating Frequently
  • August 27, 2019

@mpedroso ,
From your code sample it seems, rescue_hist_charge function is a scalar function & returns a single value. Is that correct?
If so, you can just join employees and hist_charge table on employee_code and date columns.
For example:

select a.*, b.cod_charge
from employees a join cod_charge b 
on(a.employee_code = b.employee_code 
and a.date = b.start_date);

Hope this helps.