how to call table-valued function in CASE expression - sql

I want call function in case when but when i call function I get an error .
My code is :
s = case
when v.IsAverage=1 then
isnull(avg([Value]),0)
when v.IsCumulative=1 then
isnull(sum([Value]),0)
when v.IsCumulative=0 then
GetLastValueTest('93/.1/01','93/12/29')
I get this error :
'GetLastValueTest' is not a recognized built-in function name.
but when I call this way it works :
select * from GetLastValueTest('93/.1/01','93/12/29')

select * from GetLastValueTest('93/.1/01','93/12/29')
GetLastValueTest looks like a table-valued function
then correct way to use it in case statement is:
case
when v.IsAverage=1 then isnull(avg([Value]),0)
when v.IsCumulative=1 then isnull(sum([Value]),0)
when v.IsCumulative=0 then (select top 1 [ColumnName] from GetLastValueTest('93/.1/01','93/12/29'))
end

Assuming your GetLastValueTest is scalar function - you need to specify schema explicitly, i.e. dbo.GetLastValueTest('93/.1/01','93/12/29') (or whatever you schema is if it different from dbo)
If your GetLastValueTest is table-valued function then you can't mix select from it with scalar values in other branches of case. In this case you need something like
when v.IsCumulative=0
then (select top 1 some_value from dbo.GetLastValueTest('93/.1/01','93/12/29'))

Related

What do parameters like the following one in a SELECT query do?

I've a query like this:
SELECT personas.IDDNI iddni,
PERSONAS.NOMBRE NOMBRE,
PERSONAS.APELLIDO1 APELLIDO1,
PERSONAS.APELLIDO2 APELLIDO2,
PERSONAS.ANTIGUEDAD ANTIGUEDAD,
VACACIONES(personas.IDDNI, to_char(add_months(sysdate, 0), 'YYYY')) VACACIONES
FROM personas personas,
trieniosobservaciones t
WHERE personas.iddni = t.iddni
AND ( personas.iddni = '47656567' )
I'd like to know what VACACIONES(personas.IDDNI,to_char(add_months(sysdate,0),'YYYY')) VACACIONES does in the query, as depending on the personas.iddni value it can return one row or give the following error:
The number specified in exact fetch is less than the rows returned.
VACACIONES is a user-defined function that takes two arguments, an IDDNI value for a person and a year. Beyond that, we cannot tell you what it does because it is a user-defined function and we do not have access to your database or the source code of the function.
You can find the source-code of the function using:
SELECT *
FROM all_source
WHERE name = 'VACACIONES'
AND type = 'FUNCTION'
ORDER BY owner, line;
and then you can work out what it does.

Error : Cannot perform an aggregate function on an expression containing an aggregate or a subquery

I tried to write this in one of my SQL stored procedure. Just a simple logic to just check if the #kPortalOrigin exists in the table [cli].[tbl_S_PortalOrigin]:
IF (COUNT ((SELECT kPortalOrigin
FROM [cli].[tbl_S_PortalOrigin]
WHERE kPortalOrigin = #kPortalOrigin)) = 0)
RAISERROR('Portal Origin is invalid!', 16, 1)
However, I get this error:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery
Any help? Thanks. And I would not mind an explanation why this error occurs.
if EXISTs ((select kPortalOrigin from [cli].[tbl_S_PortalOrigin] where kPortalOrigin = #kPortalOrigin)) RAISERROR('Portal Origin is invalid!',16,1)
this will works.

Error message when try to run a function in function

I create two functions that worked well as a stand alone functions.
But when I try to run one of them inside the second one I got an error:
'SQL compilation error: Unsupported subquery type cannot be evaluated'
Can you advise?
Adding code bellow:
CASE
WHEN (regexp_substr(ARGUMENTS_JSON,'drives_removed":\\[]') = 'drives_removed":[]') THEN **priceperitem(1998, returnitem_add_item(ARGUMENTS_JSON))**
WHEN (regexp_substr(ARGUMENTS_JSON,'drives_added":\\[\\]') = 'drives_added":[]') THEN 1
WHEN (regexp_substr(ARGUMENTS_JSON,'from_flavor') = 'from_flavor') THEN 1
WHEN (regexp_substr(ARGUMENTS_JSON,'{}') = '{}') THEN 1
ELSE 'Other'
END as Price_List,
The problem happened when with function 'priceperitem'
If I replace returnitem_add_item with a string then it will work fine.
Function 1 : priceperitem get customer number and a item return pricing per the item from customer pricing list
Function 2 : returnitem_add_item parsing string and return a string
As an alternative, you may try processing udf within a udf in the following way :
create function x()
returns integer
as
$$
select 1+2
$$
;
set q=x();
create function pi_udf(q integer)
returns integer
as
$$
select q*3
$$
;
select pi_udf($q);
If this does not work out as well, the issue might be data specific. Try executing the function with one record, and then add more records to see the difference in the behavior. There are certain limitations on using SQL UDF's in Snowflake and hence the 'Unsupported Subquery' Error.

converting varchar to md5

I have the following sql code in oracle client 11g. I would like to convert the "ssno" to md5 hash. I've read other posts but none of them specifically say where to put the code. thanks!
SELECT FS_HIRES."rsa",
FS_HIRES."ssno",
FS_HIRES."lname",
FS_HIRES."series",
FS_HIRES."grade",
FS_HIRES."pos_title",
FS_HIRES."ethnicity",
FS_HIRES."disability",
FS_HIRES."type_appt",
FS_HIRES."Perm_Temp",
FS_HIRES."gender",
FS_HIRES."age",
FS_HIRES."age_categories",
FS_HIRES."los",
FS_HIRES."date_apnt",
FS_HIRES."mm_apnt",
FS_HIRES."yy_apnt",
FS_HIRES."apnt_noa",
FS_HIRES."apnt_auth",
FS_HIRES.L2_DESC,
FS_HIRES.L3_DESC,
FS_HIRES.L4_DESC,
FS_HIRES.L5_DESC,
FS_HIRES."fy"
FROM FS_HIRES
The HASH_MD5 constant can't be referred to directly from SQL, so a statement like:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'),
DBMS_CRYPTO.HASH_MD5),
...
will get an error like "ORA-06553: PLS-221: 'HASH_MD5' is not a procedure or is undefined". You can either use the internal value for that constant, which is 2:
SELECT FS_HIRES."rsa",
DBMS_CRYPTO.HASH(UTL_I18N.STRING_TO_RAW(FS_HIRES."ssno", 'AL32UTF8'), 2),
...
Or if you don't want to rely on a constant that could potentially change in a future release, define your own function:
create or replace function my_md5(p_str varchar2) return raw is
begin
return dbms_crypto.hash(utl_i18n.string_to_raw(p_str, 'AL32UTF8'),
dbms_crypto.hash_md5);
end my_md5;
/
... and then call that:
SELECT FS_HIRES."rsa",
MY_MD5(FS_HIRES."ssno", 'AL32UTF8') AS "ssno",
...
If your database character isn't AL32UTF8, you may need to do more conversion as mentioned in the documentation, and it'll be easier to hide that in the function too.
Try this:
select
'123456789' as ssno,
rawtohex(
DBMS_CRYPTO.Hash (
UTL_I18N.STRING_TO_RAW ('123456789', 'AL32UTF8'),
2)
) as ssno_md5
from dual;
Output:
SSNO SSNO_MD5
123456789 25F9E794323B453885F5181F1B624D0B

How can I call a function in DB2

I have implement a function fn_getDate()
I want to use it as the following
select fn_getDate() as 'currenct_date'
so how shall I call it
select fn_getDate() as 'currenct_date' FROM SYSIBM.SYSDUMMY1;