I am getting following error when I call my stored proc. I made changes to the datatype but not helping much. The goal is to create a big long SQL on the fly joining 19 tables.
I am getting following error
Here is the stored Proc
createprocedure sapecd.dynamic_tables() language sqlscript as
ctr integer;
vl_cnt integer;
sql_query_union nvarchar(5000);
v2 nvarchar(500);
begin
ctr:=1;
sql_query_union:='';
selectcount(*) into vl_cnt FROM TABLES WHERE schema_name like'SAPEC%'AND table_name like'/1FC/0ZSSM47A%';
WHILE ctr < :vl_cnt DO
select sqlquery into v2 from (
SELECT table_name,schema_name, '(select * from ' || schema_name || '."' || table_name || '")'as sqlquery, row_number() over (orderby table_name) as row_num FROM TABLES
WHERE schema_name like'SAPEC%'AND table_name like'/1FC/0ZSSM47A%') where row_num=:ctr;
sql_query_union := sql_query_union || v2 || ' union all ';
ctr:=ctr+1;
ENDWHILE;
select sqlquery into v2 from (
SELECT table_name,schema_name, 'select * from ' || schema_name || '."' || table_name || '"'as sqlquery, row_number() over (orderby table_name) as row_num FROM TABLES
WHERE schema_name like'SAPEC%'AND table_name like'/1FC/0ZSSM47A%') where row_num=:vl_cnt;
-- sql_query_union := sql_query_union || v2;
sql_query_union := v2;
EXEC sql_query_union;
end