SAP DBTech JDBC: [328]: invalid name of function or procedure: RECORD_COUNT. I don't understand why an error occurs when using RECORD_COUNT () - hana

I don't understand why an error occurs when using RECORD_COUNT ().
Do you have a way to assign a record number value to an INT scalar variable?
Here is my code:
CREATE table tab (COL_A int);
INSERT INTO tab VALUES (1);
INSERT INTO tab VALUES (2);
DO (IN inTab TABLE(col_a int) => TAB, OUT v INT => ?)
BEGIN
DECLARE i int;
v = 0;
FOR i IN 1 .. RECORD_COUNT(:inTab)
DO
v = :v + :inTab.col_a[:i];
END FOR;
END
ERROR:...SAP DBTech JDBC: [328]: invalid name of function or procedure: RECORD_COUNT...
I am trainning about sap hana, thank you very much for my help and interest in my question.

The RECORD_COUNT SQLScript function is available in SAP HANA 2 and higher. If your HANA system is a HANA 1 system, then this function is not available.

Related

SQLExceptionHelper Invalid Input syntax for integer (PL/pgSQL)

I want to get an integer from a database query:
SELECT CAST(concat('id_',substr_of_jointable) AS INTEGER) into integervalue
FROM books_authors where id_books = booksvalue
ORDER BY id_books DESC
LIMIT 1;
subsr_of_jointable is a TEXT with value authors. However I always get an error:
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: invalid input syntax for integer: "id_authors"
Wobei: SQL statement "SELECT CAST(concat('id_',substr_of_jointable)AS INTEGER) FROM books_authors where id_books = books_value ORDER BY id_books DESC LIMIT 1"
PL/pgSQL function books_ins_trig_proc() line 125 at SQL statement
Does anyone have an idea why? The column id_books id_authors is an integer value in the database.
Assuming you're trying to build a dynamic query inside a PL/pgSQL function, you might want to take a look at this approach.
Data sample
CREATE TABLE t (id_authors INT);
INSERT INTO t VALUES (1);
Function
CREATE OR REPLACE FUNCTION myfunction(TEXT) RETURNS INT
LANGUAGE 'plpgsql' AS $BODY$
DECLARE i INT;
BEGIN
EXECUTE 'SELECT id_'|| $1 ||' FROM t LIMIT 1;' INTO i;
RETURN i;
END;
$BODY$;
This example is only to shows how you can concatenate your strings to create a column name inside your dynamic query.
Calling the function
SELECT * FROM myfunction('authors');
myfunction
------------
1
(1 Zeile)
Further reading:
Execute Dynamic Commands
PL/pgSQL Function Parameters

Syntax error when declaring a postgres variable [duplicate]

This question already has answers here:
How to declare a variable in a PostgreSQL query
(15 answers)
Closed 3 years ago.
I'm getting a SQL error when trying to declare a variable in Postgresql. Check out my code below:
declare number int8;
begin
select ID into number from public.People p where p."Name" = 'Jeff';
if found then
insert into public.Friends ("PeopleID", "Name", "PhoneNumber")
values (number, 'Jeff', '205-123-4567')
end if;
end;
When I try and run this, it returns this exception:
SQL Error [42601]: ERROR: syntax error at or near "int8"
I'm expecting there to be only one entry with the name "Jeff", so I won't have to worry about having more than one possible value for "number".
You cannot declare a variable in pure SQL in Postgres, unlike other RDBMS such as SQL Server or MySQL.
Your code compiles properly in this Postgres 12 db fiddle when I put it within plgpsql procedure (still, you should consider another name that number for a variable).
create procedure test_proc() language plpgsql AS '
declare number int8;
begin
select ID into number from people p where p."Name" = ''Jeff'';
if found then
insert into friends ("PeopleID", "Name", "PhoneNumber")
values (number, ''Jeff'', ''205-123-4567'');
end if;
end;
';
Or in Postgres < 11 with a void function:
create function test_proc() returns void language plpgsql AS '
declare number int8;
begin
select ID into number from people p where p."Name" = ''Jeff'';
if found then
insert into friends ("PeopleID", "Name", "PhoneNumber")
values (number, ''Jeff'', ''205-123-4567'');
end if;
end;
';
You might also want consider this insert ... select query, that achieves the same purpose in a single database call:
insert into friends("PeopleID", "Name", "PhoneNumber")
select ID, "Name", '205-123-4567' from people where p."Name" = 'Jeff'

Need help in converting and validating XMLType in Oracle SQL Table

So the scenario here is data is coming from an external system to oracle database.
I need to convert CLOB datatype to XMLTYPE and validate the XMLTYPE field in a table.
If the validation or conversion fails, i need to push this record in another table. How can i achieve this.
I tried using XMLTYPE function and XMLISVALID function but i keep getting errors.
You could use WITH FUNCTION:
CREATE TABLE t(id INT, x CLOB);
INSERT INTO t(id, x) VALUES(1, '<root/>');
INSERT INTO t(id, x) VALUES(2, '<root></root>');
INSERT INTO t(id, x) VALUES(3, '<root><a></root>'); -- incorrect XML
Checking if content of column is valid XML:
WITH FUNCTION func(col CLOB) RETURN NUMBER
AS
x XMLTYPE;
BEGIN
x := XMLTYPE(col);
RETURN 1;
EXCEPTION WHEN OTHERS THEN
RETURN 0;
END;
SELECT *
FROM t
WHERE func(x) = 1; -- get only valid XML
-- WHERE func(x) = 0; -- get invalid XML
db<>fiddle demo

Informix: Inserting values into tables using input parameters with EXECUTE USING

We're having a situation where we need to insert values into multiple tables using a stored procedure similar to this:
DROP TABLE informix.myTable;
GO
CREATE TABLE informix.myTable (id SERIAL NOT NULL, my_field DECIMAL(7,0))
GO
DROP PROCEDURE informix.Insert_Into_Table;
GO
CREATE PROCEDURE "informix".Insert_Into_Table (pTable_name CHAR(30), pValue DECIMAL(7,0))
RETURNING INTEGER;
-- EXECUTE informix.Insert_Into_Table('myTable', 12345)
DEFINE cust_qry VARCHAR(250);
DEFINE new_id INTEGER;
LET cust_qry = 'insert into informix.' || pTable_name || ' ( my_field ) values ( ? )';
PREPARE stmt_id FROM cust_qry;
EXECUTE stmt_id USING pValue;
SELECT DBINFO('sqlca.sqlerrd1')
INTO new_id
FROM systables WHERE owner = 'informix' AND tabname = pTable_name;
FREE stmt_id;
RETURN new_id;
END PROCEDURE;
We're getting a syntax error in the EXECUTE USING line (it compiles commenting that line).
We need to retrieve the SERIAL id from the procedure, and it's desirable to use the USING clause since there are >100 values (like pValue) in the real world situation.
IBM online docs show EXECUTE USING host variables but we couldn't find any examples using stored procedure parameters.
Informix Version: IBM Informix Dynamic Server Version 12.10.FC10WE
Any pointers on how to solve that error?
Thanks in advance

how to apply an aggregation function over a measure dynamically passed to a stored procedure?

I am trying to do spit by dimension on an analytic view within a stored procedure, and I want to pass the measure on which I will apply the aggregation function dynamically. So I did the following:
create procedure procHO (in currentMeasure varchar(60))
language sqlscript as
begin
data_tab = select MONTH_NAME as ID, sum(:currentMeasure) from
_SYS_BIC."schema/analyticView" GROUP BY MONTH_NAME;
end;
then I call the procedure this way:
call procHO("MARGIN");
but I am getting an error saying :
inconsistent datatype: only numeric type is available for aggregation function: line 5 col 38 (at pos 124) Could not execute 'call procHO("MARGIN")'
I also tried to do this using CE_ functions, here is what I did:
create procedure procHO1(in currentMeasure varchar(60))
language sqlscript as
begin
out1 = CE_OLAP_VIEW("schema/analyticView", ["MONTH_NAME",
SUM(:currentMeasure)]);
end;
and I call the procedure this way:
call procHO1("MARGIN");
but still, I am getting an error saying:
feature not supported: line 5 col 70 (at pos 157)
Could not execute 'call procHO1("MARGIN")'
by the way, as a workaround, it is possible to create a dynamic SQL query that would resolve the issue, here is an example:
create procedure procHO2(in currentMeasure varchar(60))
language sqlscript as
begin
exec 'select MONTH_NAME AS ID, sum('||:currentMeasure||') as SUM_MEASURE from
_SYS_BIC."schema/analyticView" GROUP BY MONTH_NAME';
end;
I call it this way
call procHO2('MARGIN');
but I don't want to create the SQL query dynamically since it's not recommended by SAP.
So what to do to pass an aggregated measure dynamically?
this is what the sample code from the documentation:
CREATE PROCEDURE my_proc_caller (IN in_client INT, IN in_currency INT, OUT outtab mytab_t) LANGUAGE SQLSCRIPT
BEGIN
outtab = SELECT * FROM CALC_VIEW (PLACEHOLDER."$$client$$" => :in_client , PLACEHOLDER."$$currency$$" => :in_currency );
END;
of course this works only on the latest release. Which revision are you running on?