Why my Teradata stored procedure is not running - stored-procedures

REPLACE PROCEDURE Dynamic_Select()
BEGIN
DECLARE stmt VARCHAR(500);
DECLARE result1 VARCHAR(50);
set stmt = 'SELECT count(*) into FROM table_name';
EXECUTE immediate stmt into result1;
END;
Error Message:
SPL1027:E(L8), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like ';' between the word 'stmt' and the 'into' keyword.'.
Please help me on this. I am not getting how to take the count(*) of my table into the result1 variable.
Please help me on this. I am not getting how to take the count(*) of my table into the result1 variable.

Related

Set a variable value in stored procedure and use in following query

How can I set a variable in stored procedure and use it in following query to be executed.
create or replace procedure sp1()
returns table (dealer_id varchar, dealershipgroup_name varchar)
language sql
as
$$
declare
create_query varchar;
res resultset;
MISSING_DEALER NUMBER(38,0) default 0;
begin
MISSING_DEALER := 100;
select_query := 'WITH CTE AS(
SELECT dealer_id,
CASE WHEN dealer_id=:MISSING_DEALER then \'Abc\'
WHEN dealershipgroup IS NULL then \'\'
ELSE dealershipgroup end as dealershipgroup FROM TBL )
select * from CTE';
res:= (execute immediate : select_query);
return table(res);
end;
$$;
call sp1();
Could someone please suggest how can I use MISSING_DEALER in the query. I am currently getting the following error
Uncaught exception of type 'STATEMENT_ERROR' on line 28 at position 9 : SQL compilation error: error line 8 at position 26 Bind variable :MISSING_DEALER not set
You need to concatenate the string parts of your SQL statement with the variable. This is covered in the documentation if you look at the end of the section here

How to write multi line query in snowflake scripting in stored procedure

create or replace procedure create_src_table()
returns table (name varchar, age number(10,0),dob date)
language sql as
$$
declare
create_query varchar;
res resultset;
begin
create_query := `CREATE TEMPORARY TABLE SRC_TEMP_TBL AS SELECT * FROM
(WITH CTE_1 AS (SELECT * FROM "DB"."DW"."USER_TBL" WHERE name='rahul'),
CTE_2 AS (SELECT * FROM CTE_1 WHERE CAST(DOB AS DATE)<2000-05-01)
SELECT name,age,dob FROM CTE_2 limit 10)`;
res := (execute immediate : create_query);
return table(res);
end;
$$;
call create_src_table();
Could someone please help in how to write multiline sql query. I found few answers that indicate using backtick in javascript but not sure how to achieve it in sql.
Multi-statement SQL should be wrapped inside BEGIN ... END block:
EXECUTE IMMEDIATE 'BEGIN
statement1;
statement2;
...
END;';
Sample:

Anyone knows why LPAD function returns an error when it is called during UNLOAD command inside Redshift stored procedure?

I'm trying to call the LPAD function during the UNLOAD command inside a Redshift stored procedure and it keeps returning an error: Amazon Invalid operation: syntax error at or near "9" . Can anyone help me address this error?
The column that LPAD is applied to is VARCHAR data type. Basically, I'm adding leading "0" to records that are less than 9 character long.
If it matters, I'm using Aginity Pro as the query editor while connecting to Redshift. Here is an example stored procedure:
CREATE OR REPLACE PROCEDURE schemaname.sp_example ()
LANGUAGE plpgsql
AS
$$
DECLARE _CurrentDate VARCHAR(10);
_UnloadQuery VARCHAR(65000);
BEGIN
WITH CTE_CurrentDate AS (SELECT TO_CHAR(CAST(GETDATE() AS DATE), 'YYYYMMDD') AS CurrentDateParm)
SELECT CurrentDateParm INTO _CurrentDate FROM CTE_CurrentDate;
_UnloadQuery := '
UNLOAD ('''||
'
SELECT B.column_name
FROM tbl_a a
INNER JOIN tbl_b B
on LPAD(a.column_name,'''||$9||''',''0'') = B.column_name
GROUP BY B.column_name
'
||'''
)
to '''||'s3://address_'||_CurrentDate||'.CSV'||'''
credentials '''||'aws_access_key_id=xxx'||';'||'aws_secret_access_key=xxx'||'''
Header
ALLOWOVERWRITE
delimiter '''||','||'''
addquotes
PARALLEL OFF
';
EXECUTE _UnloadQuery;
END;
$$;

Count giving wrong results in stored procedure

I am getting incorrect count from a query ran inside a stored procedure.
When we run the same query ( after hard-coding the values of table name and schema name ) it gives the correct result.
Preliminary analysis , hints the fact that the query run inside the stored procedure is for some reason ignoring the second filter ( i.e where ... and ... , the second part is being ignored ).
CREATE OR REPLACE PROCEDURE dev.gp_count (tablename VARCHAR(256))
AS
$$ DECLARE schema_name VARCHAR(64);
table_name VARCHAR(128);
check_count_1 INT;
check_count_2 INT;
BEGIN
schema_name:= SPLIT_PART(tablename,'.',1);
table_name:= SPLIT_PART(tablename,'.',2);
check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' + table_name +'%');
raise info 'check_count_1 - %',check_count_1;
end;
$$
language plpgsql;
And calling the above procedure as-
call dev.gp_count ('dev.gp_test1');
The result obtained from stored procedure is -
Warnings:
check_count_1 - 925
If we run the same query after substituting the values for table name and schema then -
select count(*) from information_schema.tables where table_schema = 'dev' and table_name like '%gp_test1%';
RESULT -
count
3
Now investigating the issue further -
The count obtained from stored procedure is same as the count obtained from this query -
select count(*) from information_schema.tables where table_schema = 'dev';
RESULT-
count
925
My Guess -
So this hints that , maybe inside stored procedure the second filter condition is being ignored.
Besides helping me with other alternatives , please do help me find the reason behind this anomaly too .
Thanks in Advance.
I think your problem lies in 1) your string concatenation and 2) use of table_name as a variable:
check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' + table_name +'%');
PostgreSQL string concatenation uses ||, so it should look like this:
check_count_1 := (select count(*) from information_schema.tables where table_schema = schema_name and table_name like '%' || table_name || '%');
Try changing it to look like this:
CREATE OR REPLACE PROCEDURE gp_count (tablename VARCHAR(256))
AS
$$ DECLARE
schema_name VARCHAR(64);
table_name1 VARCHAR(128);
check_count_1 INT;
check_count_2 INT;
BEGIN
schema_name:= SPLIT_PART(tablename,'.',1);
table_name1:= SPLIT_PART(tablename,'.',2);
check_count_1 := (select count(*) from information_schema.tables f where table_schema = schema_name and f.table_name like '%' || table_name1 || '%');
raise info 'check_count_1 - %',check_count_1;
end;
$$
language plpgsql;
Disclosure: I work for EnterpriseDB (EDB)

Dynamic query in Cursor for %rowtype

I need to create a record type to contain the result set of the above query of
V_SQL VARCHAR2(200) := 'SELECT T1.'||record_id||',T1.'||card_no||',T2.TEST_CARD_NO, T1.'||type_cc||' FROM '|| table_name||' T1
INNER JOIN TEST T2 ON
T2.ID = T1.'||record_id;
So that I can use t1 C_BRNGB_EXTRACT%ROWTYPE (plus another column for t2.col4%TYPE inside l_BRNGB_EXTRACT) inside loop execute statement
CREATE OR REPLACE PROCEDURE "GCCPMAINT"."FUNCTION_CURSOR"(table_name VARCHAR2,record_id VARCHAR2,card_no VARCHAR2,type_cc VARCHAR2)
AS
limit_in NUMBER:=100;
V_SQL VARCHAR2(200) := 'SELECT T1.'||record_id||',T1.'||card_no||',T2.TEST_CARD_NO, T1.'||type_cc||' FROM '|| table_name||' T1
INNER JOIN TEST T2 ON
T2.ID = T1.'||record_id;
TYPE BRNGB_EXTRACT IS REF CURSOR;
C_BRNGB_EXTRACT BRNGB_EXTRACT;
TYPE BRNGB_EXTRACT1 IS TABLE OF C_BRNGB_EXTRACT%ROWTYPE;
l_BRNGB_EXTRACT BRNGB_EXTRACT1;
BEGIN
OPEN C_BRNGB_EXTRACT FOR V_SQL;
LOOP
FETCH C_BRNGB_EXTRACT BULK COLLECT INTO l_BRNGB_EXTRACT LIMIT limit_in;
EXIT WHEN l_BRNGB_EXTRACT.COUNT = 0;
FORALL indx IN 1 .. l_BRNGB_EXTRACT.COUNT
EXECUTE IMMEDIATE 'UPDATE table_name SET CARD_NO=:1 WHERE CARD_NO=:2 AND RECORD_ID=:3' USING l_BRNGB_EXTRACT(indx).test_card_no,l_BRNGB_EXTRACT(indx).CARD_NO,l_BRNGB_EXTRACT(indx).RECORD_ID;
END LOOP;
CLOSE C_BRNGB_EXTRACT;
COMMIT;
END;
For a above stored procedure I'm getting the following error
PLS-00320: the declaration of the type of this expression is
incomplete or malformed PL/SQL: Item ignored PLS-00597: expression
'L_BRNGB_EXTRACT' in the INTO list is of wrong type PL/SQL: SQL
Statement ignored PLS-00487: Invalid reference to variable
'C_BRNGB_EXTRACT%ROWTYPE'
Please help me to solve this.
i got the solution
TYPE BRNGB_EXTRACT IS REF CURSOR;
C_BRNGB_EXTRACT BRNGB_EXTRACT;
-- TYPE BRNGB_EXTRACT1 IS TABLE OF C_BRNGB_EXTRACT%ROWTYPE;
TYPE BRNGB_EXTRACT1 IS RECORD (
record_id varchar(30),
card_no varchar(30),
TEST_CARD_NO varchar(30)
);
type test_rec_arr is table of BRNGB_EXTRACT1 index by pls_integer;
l_BRNGB_EXTRACT test_rec_arr;

Resources