create or replace procedure Practise.sp_test()
OPTIONS(strict_mode=false)
BEGIN
DECLARE v_product_code int64;
DECLARE v_price int64;
set v_product_code = (SELECT product_code FROM `virendra101987.Practise.product` where v_product_code =01);
set v_price = (SELECT price FROM `virendra101987.Practise.product` where v_product_code =01);
EXECUTE IMMEDIATE v_product_code;
EXECUTE IMMEDIATE v_price;
insert into `Practise.sales1`(order_date,product_code,quantity_order,sales_price)
values (current_date(),v_product_code,1,v_price);
END
CALL `virendra101987.Practise.sp_test`();
Why am I getting this error?
ERROR Query error: Cannot coerce expression v_product_code to type STRING at [virendra101987.Practise.sp_test:7:23]
The error message arises from the EXECUTE IMMEDIATE expecting a string. However in looking at what you're trying to achieve I don't believe it is necessary.
The SET operator will execute the SQL statements for you and does not require any further action to evaluate. So you should be able to pass them into your insert statement without issue.
For example:
create or replace procedure Practise.sp_test()
OPTIONS(strict_mode=false)
BEGIN
DECLARE v_product_code int64;
DECLARE v_price int64;
set v_product_code = (SELECT product_code FROM `virendra101987.Practise.product` where v_product_code =01);
set v_price = (SELECT price FROM `virendra101987.Practise.product` where v_product_code =01);
insert into `Practise.sales1`(order_date,product_code,quantity_order,sales_price)
values (current_date(),v_product_code,1,v_price);
END;
CALL `virendra101987.Practise.sp_test`();
Related
I have a procedure that returns a list when requested - test_1.
I want to create a procedure that sends each value from a table type_cd_list to the procedure test_1 and returns a list, but I get an error:
An existing result set Cursor is opened from stored procedure
Code:
--calling procedures
CALL Pb_tmd.test_1('280');
--create table
CREATE VOLATILE TABLE type_cd_list
(
num_chn VARCHAR(10),
info_system_type_cd VARCHAR(10)
) ON COMMIT PRESERVE ROWS;
INSERT INTO type_cd_list ('12','280');
--procedure
REPLACE PROCEDURE Pb_tmd.test_2()
DYNAMIC RESULT SETS 1
BEGIN
DECLARE hc1 VARCHAR(10);
FOR
RepordPointer AS c_sptable CURSOR FOR
SELECT info_system_type_cd AS c_c1 FROM type_cd_list
DO
SET hc1 = RepordPointer.c_c1;
CALL Pb_tmd.test_1(:hc1);
END FOR;
ROLLBACK;
END;
Or is there another option to combine these 2 procedures? Below is the nested procedure code
REPLACE PROCEDURE Pb_tmd.test_1 (IN db_lst VARCHAR(100))
DYNAMIC RESULT SETS 1
BEGIN
DECLARE NumberInst VARCHAR(100);
DECLARE SQL_TEXT VARCHAR(3000);
DECLARE cur1 CURSOR WITH RETURN ONLY FOR S1;
SET SQL_TEXT = 'SELECT * FROM Pb_'||db_lst||'_tmd.LOAD_LOG';
PREPARE S1 FROM SQL_TEXT;
OPEN cur1;
END;
I am trying to create a stored procedure, but I get this error
Expected tokens may include: "". LINE NUMBER=17. SQLSTATE=42601
My code:
CREATE OR REPLACE PROCEDURE FETCH_EMP_SP(IN V_EMP_NAME VARCHAR(100),IN V_EMP_DEPT VARCHAR(100))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE p_query_string VARCHAR(100);
IF ((V_EMP_NAME IS NOT NULL) AND (V_EMP_DEPT IS NOT NULL)) THEN
SET p_query_string = 'emp_name ='||V_EMP_NAME||' AND emp_dept='||V_EMP_DEPT||' WITH UR';
ELSEIF(V_EMP_DEPT IS NOT NULL) THEN
SET p_query_string = ' AND emp_dept='||V_EMP_DEPT||' WITH UR';
ELSE
SET p_query_string = ' WITH UR';
END IF;
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR SELECT emp_name,emp_no,emp_dept,emp_location from employee where status=1 p_query_string;
OPEN C1;
END#
should be executed successfully
Declarations and statements can’t follow in arbitrary order in a Compound SQL (compiled) statement
Cursor declarations must follow the variables declarations and must be followed by the SQL procedure statements.
So, place the cursor declaration after the variable declaration.
Moreover, there is a number of other errors in your code. Should be something like this:
CREATE OR REPLACE PROCEDURE FETCH_EMP_SP(IN V_EMP_NAME VARCHAR(100),IN V_EMP_DEPT VARCHAR(100))
DYNAMIC RESULT SETS 1
LANGUAGE SQL
BEGIN
DECLARE p_query_string VARCHAR(256);
DECLARE C1 CURSOR WITH RETURN TO CLIENT FOR S1;
IF ((V_EMP_NAME IS NOT NULL) AND (V_EMP_DEPT IS NOT NULL)) THEN
SET p_query_string = ' AND emp_name ='''||V_EMP_NAME||''' AND emp_dept='''||V_EMP_DEPT||''' WITH UR';
ELSEIF(V_EMP_DEPT IS NOT NULL) THEN
SET p_query_string = ' AND emp_dept='''||V_EMP_DEPT||''' WITH UR';
ELSE
SET p_query_string = ' WITH UR';
END IF;
SET p_query_string='SELECT emp_name,emp_no,emp_dept,emp_location from employee where status=1 '||p_query_string;
PREPARE S1 FROM p_query_string;
OPEN C1;
END#
String constants in the query text must be wrapped in single quotes. Don't do this if emp_dept is a numeric column.
I have this here:
create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (test,'''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;
end#
The problem is that it seems to run (no error message) but there is nothing in the table....
Any ideas?
Thanks
I think you need to change test to '||test||' in set v_query like below:
create procedure test_ins(IN tab varchar(128))
Language sql
begin
DECLARE stmt_ins STATEMENT;
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values ( '''||test||''','''')';
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;
end#
Seems you are not using a debugger...try the one in IBM Data Studio (free download).
create or replace procedure test_ins(IN tab varchar(128))
Language sql
specific test_ins
begin
DECLARE v_query varchar(2048);
DECLARE test varchar(20);
DECLARE test_tab varchar(20);
DECLARE stmt_ins STATEMENT;
set test = 'HERE IT IS';
set test_tab = tab;
set v_query= 'INSERT INTO '||test_tab||'(test, free) values (''test'','''')';
call dbms_output.put_line(v_query);
PREPARE stmt_ins from v_query;
EXECUTE stmt_ins;
end#
I have a SAP HANA procedures to update a table. I'm trying to create a dynamic sql query, this is my example:
declare _field varchar(100) := 'NAME';
declare _name varchar(100) := 'david';
declare _id integer := 1;
DECLARE SQL_STR VARCHAR(3000);
SQL_STR := 'UPDATE "_SYS_BIC"."TEST_TABLE" SET "'||_field||'" = '||_name||' WHERE "ID" = '||:_id;
EXECUTE IMMEDIATE (:SQL_STR);
But in the console I have this error:
Service exception: [260] invalid column name
How can I fix it?
I found the correct syntax to fix it
declare _id integer := 1;
declare _name varchar(100) := 'david';
declare _field varchar(100) := 'NAME';
DECLARE SQL_STR VARCHAR(3000);
SQL_STR := 'UPDATE "_SYS_BIC"."TEST_TABLE" SET "'||_field||'" = '''||_name||''' WHERE "ID" = '||_id;
EXECUTE IMMEDIATE (:SQL_STR);
If I have to set a column or table name the right syntax is
"'||_field||'"
To set a string value the syntax is '''||_name||''' the first two apostrophes are used for the escape so we can say to the query that the value is a string, the last one apostrophe is used to concatenate the value to the rest of the query string. The three apostrophes must be written without spaces among them.
I can't get this stored procedure to compile, I simply want to declare and set one variable that will be used in the proc. NOTE: shipNo is the only incoming parameter, and is VARCHAR(32).
P1: BEGIN
DECLARE #likeShipNo VARCHAR(32);
SET likeShipNo = '%' || shipNo || '%';
DECLARE cursor1 CURSOR WITH RETURN FOR
SELECT ADV_SHIP_NOTICE_NO,
VNDR_ID,
LOC,
CARR_CD,
CARR_PRO_NO,
BL_NO,
SHIP_EQUIP_ID,
TS_LOAD
FROM TRITS
WHERE ADV_SHIP_NOTICE_NO LIKE(#likeShipNo);
OPEN cursor1;
END P1
The variables in DB2 doesn't need the # like in MSSQL. Also, when you return a cursor, you need to add the sentence RESULT SET 1 before begin and add the WITH RETURN TO CLIENT to the cursor that you want to return. The procedure should look like this:
CREATE BEGIN MYSCHEMA.MYPROC (
IN shipNo VARCHAR(30) )
BEGIN
DECLARE likeShipNo VARCHAR(32);
SET likeShipNo = '%' || shipNo || '%';
DECLARE cursor1 CURSOR WITH RETURN TO CLIENT FOR
SELECT
ADV_SHIP_NOTICE_NO,
VNDR_ID,
LOC,
CARR_CD,
CARR_PRO_NO,
BL_NO,
SHIP_EQUIP_ID,
TS_LOAD
FROM
TRITS
WHERE
ADV_SHIP_NOTICE_NO LIKE likeShipNo;
OPEN cursor1;
END;