How to return from a SQL function that includes a recursive common table expression? - sql-function

I have a function like this (just reduced it in order to make it readable ):
CREATE FUNCTION myfunction(somearg text) RETURNS integer AS
$$
WITH recursive mycte(index, myi) AS (
select 0,null
--from
--where
UNION ALL
SELECT mycte.index+1, case when[condition] then mycte.myi=mycte.index end
FROM mytable AS m, mycte
WHERE index<50
)
SELECT myi
FROM mycte;
--how to return myi;
$$
LANGUAGE SQL ;
SELECT myfunction('abc');
How can I return myi?
Once [condition] is true, myi should be returned. ([condition] can be true only once in my task and efficiency is no problem)

You don't need to "return" anything. The result of a SQL function is automatically the last query in the body. In your case it's the "main" select of the CTE.
If the function is declared as a scalar function (as yours is), this also means only the "first" row of the result will be returned.
The way you call it will work just fine.
But given your query, I think your function should be declared as returns table (myi integer)

Related

bigquery sql table function with string interpolation

I am trying to write a BigQuery SQL function / stored procedure / table function that accepts as input:
a INT64 filter for the WHERE clause,
a table name (STRING type) as fully qualified name e.g. project_id.dataset_name.table_name
The idea is to dynamically figure out the table name and provide a filter to slice the data to return as a table.
However if try to write a Table Function (TVF) and I use SET to start dynamically writing the SQL to execute, then I see this error:
Syntax error: Expected "(" or keyword SELECT or keyword WITH but got keyword SET at [4:5]
If I try to write a stored procedure, then it expects BEGIN and END and throws this error:
Syntax error: Expected keyword BEGIN or keyword LANGUAGE but got keyword AS at [3:1]
If I try to add those, then I get various validation errors basically because I need to remove the WITH using CTEs (Common Table Expression), and semicolons ; etc.
But what I am really trying to do is using a table function:
to combine some CTEs dynamically with those inputs above (e.g. the input table name),
to PIVOT that data,
to then eventually return a table as a result of a SELECT.
A bit like producing a View that could be used in other SQL queries, but without creating the view (because the slice of data can be decided dynamically with the other INT64 input filter).
Once I dynamically build the SQL string I would like to EXECUTE IMMEDIATE that SQL and provide a SELECT as a final step of the table function to return the "dynamic table".
The thing is that:
I don't know before runtime the name of this table.
But I have all these tables with the same structure, so the SQL should apply to all of them.
Is this possible at all?
This is the not-so-working SQL I am trying to work around. See what I am trying to inject with %s and num_days:
CREATE OR REPLACE TABLE FUNCTION `my_dataset.my_table_func_name`(num_days INT64, fqn_org_table STRING)
AS (
-- this SET breaks !!!
SET f_query = """
WITH report_cst_t AS (
SELECT
DATE(start) as day,
entity_id,
conn_sub_type,
FROM `%s` AS oa
CROSS JOIN UNNEST(oa.connection_sub_type) AS conn_sub_type
WHERE
DATE(start) > DATE_SUB(CURRENT_DATE(), INTERVAL num_days DAY)
AND oa.entity_id IN ('my-very-long-id')
ORDER BY 1, 2 ASC
),
cst AS (
SELECT * FROM
(SELECT day, entity_id, report_cst_t FROM report_cst_t)
PIVOT (COUNT(*) AS connection_sub_type FOR report_cst_t.conn_sub_type IN ('cat1', 'cat2','cat3' ))
)
""";
-- here I would like to EXECUTE IMMEDIATE !!!
SELECT
cst.day,
cst.entity_id,
cst.connection_sub_type_cat1 AS cst_cat1,
cst.connection_sub_type_cat2 AS cst_cat2,
cst.connection_sub_type_cat3 AS cst_cat3,
FROM cst
ORDER BY 1, 2 ASC
);
This might not be satisfying but since Procedural language or DDL are not allowed inside Table functions currently, one possible way around would be simply using PROCEDURE like below.
CREATE OR REPLACE PROCEDURE my_dataset.temp_procedure(filter_value INT64, table_name STRING)
BEGIN
EXECUTE IMMEDIATE FORMAT(CONCAT(
"SELECT year, COUNT(1) as record_count, ",
"FROM %s ",
"WHERE year = %d ",
"GROUP BY year ",
"; "
), table_name, filter_value);
END;
CALL my_dataset.temp_procedure(2002, 'bigquery-public-data.usa_names.usa_1910_current');

Is it possible to pass in a variable amount of parameters to a stored procedure in redshift?

I am trying to write a stored procedure in AWS Redshift SQL and one of my parameters needs the possibility to have an integer list (will be using 'IN(0,100,200,...)' inside there WHERE clause). How would I write the input parameter in the header of the procedure so that this is possible (if at all?)
I've tried passing them in as a VARCHAR "integer list" type thing but wasn't sure then how to parse that back into ints.
Update: I found a way to parse the string and loop through it using the SPLIT_PART function and store all of those into a table. Then just use a SELECT * FROM table with the IN() call
What I ended up doing was as follows. I took in the integers that I was expecting as a comma-separated string. I then ran the following on it.
CREATE OR REPLACE PROCEDURE test_string_to_int(VARCHAR)
AS $$
DECLARE
split_me ALIAS FOR $1;
loop_var INT;
BEGIN
DROP TABLE IF EXISTS int_list;
CREATE TEMPORARY TABLE int_list (
integer_to_store INT
);
FOR loop_var IN 1..(REGEXP_COUNT(split_me,',') + 1) LOOP
INSERT INTO int_list VALUES (CAST(SPLIT_PART(split_me,',',loop_var) AS INT));
END LOOP;
END;
$$ LANGUAGE plpgsql;
So I would call the procedure with something like:
CALL test_string_to_int('1,2,3');
and could do a select statement on it to see all the values stored into the table. Then in my queries the need this parameter I ran:
.........................
WHERE num_items IN(SELECT integer_to_store FROM int_list);

stored procedure functions not working properly?

i am writing a procedure function in phpmyadmin for attendance purpose.But i am getting wrong information from function if condition.
below is the sample code for procedure and functions without if.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `USP_GetEmployeeAttendanceReport`(IN selectedIndex int,IN searchText nvarchar(20),IN selectedDate datetime)
BEGIN
select FN_CheckEmpAttendanceStatus(selectedIndex,selectedDate);
END
Function FN_CheckEmpAttendanceStatus
DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `FN_CheckEmpAttendanceStatus`(cardid varchar(150),selectedDate datetime) RETURNS int(11)
BEGIN
DECLARE result INT;
set result=(select count(*) from iotrans where CARDID=cardid and dt=selectedDate);
return result;
END
but from function i am getting garbage values (i.e 80,0,81,82....).thanks in advance
The problem is most likely caused by the fact that you use the same name cardid for a function parameter as for a column in iotrans, thus MySQL can't tell them apart and condition WHERE CARDID=cardid always evaluates as TRUE.
Always give distinct names to routine parameters. I'd suggest to come up with some naming scheme, e.g. putting a underscore in front of the name of a parameter, so that you do that consistently across all your code and can easily tell whether it's a parameter or a column name.
One more thing usage of result variable is unnecessary overhead in your case as is BEGIN...END block.
That being said your function might've looked like this
CREATE DEFINER=`root`#`localhost`
FUNCTION FN_CheckEmpAttendanceStatus
(
_cardid varchar(150),
_selectedDate datetime
) RETURNS INT(11)
RETURN
(
SELECT COUNT(*)
FROM iotrans
WHERE cardid = _cardid
AND dt = _selectedDate
);

Firebird 2.1 stored procedure to concatenate text on multiple rows

I am trying to write a stored procedure to concatenate multiple rows of text together to return it as a single string. For example:
CREATE TABLE TEST (
ID INTEGER,
SEQ INTEGER,
TEXT VARCHAR(255));
COMMIT;
INSERT INTO TEST (ID, SEQ, TEXT) VALUES (1, 1, "LINE 1");
INSERT INTO TEST (ID, SEQ, TEXT) VALUES (1, 2, "LINE 2");
INSERT INTO TEST (ID, SEQ, TEXT) VALUES (1, 3, "LINE 3");
COMMIT;
SET TERM !!;
CREATE PROCEDURE concat_names (iID INTEGER)
RETURNS (CONCAT VARCHAR(2000))
AS
DECLARE VARIABLE name VARCHAR(255);
BEGIN
CONCAT = '';
FOR SELECT TEXT FROM TEST where id=:iID INTO :name
DO BEGIN
CONCAT = CONCAT || name;
END
END!!
SET TERM ;!!
commit;
However when I run:
select concat from concat_names(1);
It always returns zero rows.
Any ideas?
You forget for SUSPEND. Your proc should look like this:
SET TERM !!;
CREATE PROCEDURE concat_names (iID INTEGER)
RETURNS (CONCAT VARCHAR(2000))
AS
DECLARE VARIABLE name VARCHAR(255);
BEGIN
CONCAT = '';
FOR SELECT TEXT FROM TEST where id=:iID INTO :name
DO BEGIN
CONCAT = CONCAT || name;
END
SUSPEND;
END!!
SET TERM ;!!
You can achieve the same result without stored proc. Use LIST aggregate function:
SELECT LIST(text, '') FROM TEST where id=:iID
Second parameter of LIST is a delimiter. If you call LIST with only field name, then comma ',' will be used to separate values.
In the case the field TEST can ben null and you don't want to set to null the whole result it is useful to use:
CONCAT = CONCAT || coalesce(name,'');
instead of
CONCAT = CONCAT || name;
Without utilizing a Stored Proc and using version Firebird 2.5, the LIST aggregation function will return "Comma-separated string concatenation of non-NULL values in the column"*. Using the aforementioned TEST table, the SQL
SELECT LIST(TEXT)
FROM TEST
returns
LINE 1,LINE 2,LINE 3
This may be of some interest.
*Taken from the Firebird reference page here

Use Recursive CTE in DB2 stored proc

I have a need to run a recursive CTE within a stored proc, but I can't get it past this:
SQL0104N An unexpected token "with" was found following "SET count=count+1;
". Expected tokens may include: "". LINE NUMBER=26.
My google-fu showed a couple of similar topics, but none with resolution.
The query functions as expected outside of the stored proc, so I'm hoping that there's some syntactic sugar I'm missing that'll let this work. Similarly, the proc compiles and works without the query.
Here's a contrived example:
--setup
create table tree (id integer, name varchar(50), parent_id integer);
insert into tree values (1, 'Alice', null);
insert into tree values (2, 'Bob', 1);
insert into tree values (3, 'Charlie', 2);
-
- the proc
create or replace procedure testme() RESULT SETS 1 LANGUAGE SQL
BEGIN
DECLARE SQLSTATE CHAR(5);
DECLARE SQLCODE integer default 0;
DECLARE count INTEGER;
DECLARE sum INTEGER;
DECLARE total INTEGER;
DECLARE id INTEGER;
DECLARE curs CURSOR WITH RETURN FOR
select count,sum from sysibm.sysdummy1;
DECLARE hiercurs CURSOR FOR
select id from tree order by id;
SET bomQuery='';
PREPARE stmt FROM bomQuery;
SET count = 0;
SET sum = 0;
set total = 0;
OPEN hiercurs;
FETCH hiercurs INTO id;
WHILE (SQLCODE <> 100) DO
SET count=count+1;
with org (level,id,name,parent_id) as
(select 1 as level,root.id,root.name,root.parent_id from tree root where root.id=id
union all
select level+1,employee.id,employee.name,employee.parent_ id from org boss, tree employee
where level < 5 and employee.parent_id=boss.id)
select count(1) into sum from org;
SET total=total+sum;
FETCH hiercurs INTO id;
END WHILE;
CLOSE hiercurs;
OPEN curs;
END
the cte in db2 doesn't seem to recognize the scalar result of the query, and so it won't let the select into work (not a problem on Oracle or SQLServer)...solution is to open a cursor and FETCH INTO (instead of SELECT INTO) instead.
In addition to rjb's suggestion of enclosing the CTE query inside a cursor, you can also stuff the CTE into a user-defined function or a view, and then code a straight select against that object into your stored procedure.

Resources