error 1178: syntax error in a stored procedure in mariadb columnstore - sql

I try to produce a stored procedure that allows me to update many tables in my database but when I try to execute it with a columnstore engine, i get an error of procedure syntax not supported. I have looked for it in the web but do not manage to find where is the issue. If you have an idea, I let you the procedure here.
DELIMITER $$
CREATE PROCEDURE update_sp_aggregated()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE tableName varchar(255) DEFAULT "";
DECLARE cursor_update
CURSOR FOR
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name like 'sp_aggr%' ;
OPEN cursor_update;
updateAggregated: LOOP
FETCH cursor_update into tableName;
IF finished = 1 THEN
LEAVE updateAggregated;
END IF;
SET #sql = CONCAT('ALTER TABLE ', tableName, ' ADD COLUMN col2 varchar(5)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #sql = CONCAT('UPDATE ', tableName, ' SET col2= LEFT(col1, 1)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP updateAggregated;
CLOSE cursor_update;
END $$
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
DELIMITER ;
CALL update_sp_aggregated();

Finally found, so as tell in my comment, the issue was the update command was not allowed.
to solve it, you need to change the variable infinidb_vtable_mode by setting it to 0.
All is described here
and here

Related

Create a query as a procedure in BigQuery

I am basically trying to do this MySQL script in BigQuery:
CREATE PROCEDURE `test` (IN tab_name CHAR(40) )
BEGIN
SET #query =
CONCAT(
'SELECT *
FROM ', tab_name );
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
In theory, this should let me run this script:
CALL test(`my_dataset.my_table`)
and it will perform my "SELECT * FROM" script on that table.
So far, this isn't working for me because BQ doesn't want to accept the quotation marks breaking across lines, so it says there is an "Unclosed string literal".
Any idea how I can accomplish this in BQ?
Here is my attempt:
CREATE OR REPLACE PROCEDURE `mydataset.test`(tableName STRING)
BEGIN
DECLARE queryString STRING;
SET queryString = " SELECT * FROM mydataset."||tableName||"";
EXECUTE IMMEDIATE queryString;
-- SELECT queryString;
END;
Execute the procedure:
CALL `mydataset.test`('mytable');

Two cursors sequence in procedure

I have the following problem with a procedure:
CREATE PROCEDURE TEST()
LANGUAGE SQL
BEGIN
DECLARE V_TEST VARCHAR(100);
DECLARE V_TEST_EXT VARCHAR(100);
DECLARE SQLCODE INTEGER DEFAULT -1;
DECLARE RET_CODE INTEGER DEFAULT -2;
DECLARE LIST_CMD VARCHAR(512);
DECLARE LIST_CMD_EXT VARCHAR(512);
DECLARE CUR_TEST CURSOR WITH RETURN FOR LIST_STMT;
DECLARE CUR_TEST_EXT CURSOR WITH RETURN FOR LIST_EXT_STMT;
DECLARE CONTINUE HANDLER FOR SQLEXECPTION SET RET_CODE = SQLCODE;
SET LIST_CMD = 'SELECT TEST FROM TESTTAB';
PREPARE LIST_STMT FROM LIST_CMD;
OPEN CUR_TEST;
FETCH CUR_TEST INTO V_TEST;
WHILE (RET_CODE <> 100) DO
FETCH CUR_TEST INTO V_TEST;
END WHILE;
CLOSE CUR_TEST;
SET LIST_CMD_EXT = 'SELECT TEST FROM TESTTAB';
PREPARE LIST_STMT_EXT FROM LIST_CMD_EXT;
OPEN CUR_TEST_EXT;
FETCH CUR_TEST_EXT INTO V_TEST_EXT;
WHILE (RET_CODE <> 100) DO
FETCH CUR_TEST_EXT INTO V_TEST_EXT;
END WHILE;
CLOSE CUR_TEST_EXT;
END;
The problem I am having is that the procedure does the first cursor loop but ignores the second one. I tried adding BEGIN and END to each loop but that didn't help as well.
I am running DB2 v10.5 Windows. I need this to work as a procedure. Is there any way to make the procedure run both loops?
Thank you for your help.-
You don't reset the RET_CODE variable before processing the 2-nd cursor.
Use something like this:
SET RET_CODE = 0;
OPEN CUR_TEST_EXT;
...
BTW, don't declare a cursor using 'WITH RETURN' clause if you process this cursor inside the procedure.

DB2 SQL Error: SQLCODE=-952, > SQLSTATE=57014, from Tibco BW

I am calling a DB2 stored proc from BW call procedure pallet.
when i test this proc in database ,it is getting executed properly.
But when i call through BW it throws exception
JDBC error reported: (SQLState = 57014) -
com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-952,
SQLSTATE=57014, SQLERRMC=null,
My stored proc's code is as below
CREATE OR REPLACE PROCEDURE TABLE1_PURGE_PROC_V1 (IN v_REF_VERSION VARCHAR(3), OUT o_DELTETE_STATUS VARCHAR(7))
P1: BEGIN
--DECLARE v_TABLE_NAME VARCHAR(30);
DECLARE v_WHERE_CONDITION VARCHAR(1024);
DECLARE V_COUNT_QUERY VARCHAR(1024);
DECLARE v_COMMIT_COUNT INTEGER;
SET v_WHERE_CONDITION='REF_VERSION ='||v_REF_VERSION;
SET v_COMMIT_COUNT=10000;
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE1',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE2',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE3',v_COMMIT_COUNT,v_WHERE_CONDITION);
CALL SCHEMA.DELETE_WITH_COMMIT_COUNT('SCHEMA.TABLE4',v_COMMIT_COUNT,v_WHERE_CONDITION);
INSERT INTO SCHEMA.DEBUG_LOG(PROC_NAME,LOG_TIME,MESSAGE) VALUES('TABLE1_PURGE_PROC_V1',CURRENT_TIMESTAMP,'ALL TABLE1 RELATED TABLES INVALID DATA DELETED FOR VERSION-'||v_REF_VERSION);
SET o_DELTETE_STATUS ='SUCCESS';
END P1
####################################### PROC 2 ###########################################################
CREATE OR REPLACE PROCEDURE DELETE_WITH_COMMIT_COUNT(IN v_TABLE_NAME VARCHAR(24), IN v_COMMIT_COUNT INTEGER, IN v_WHERE_CONDITION VARCHAR(1024))
NOT DETERMINISTIC
LANGUAGE SQL
P1: BEGIN
-- DECLARE Statements
DECLARE SQLCODE INTEGER;
DECLARE v_DELETE_QUERY VARCHAR(1024);
DECLARE v_DELETE_STATEMENT STATEMENT;
SET v_DELETE_QUERY = 'DELETE FROM (SELECT 1 FROM ' || v_TABLE_NAME || ' WHERE ' || v_WHERE_CONDITION
|| ' FETCH FIRST ' || RTRIM(CHAR(v_COMMIT_COUNT)) || ' ROWS ONLY) AS DELETE_TABLE';
PREPARE v_DELETE_STATEMENT FROM v_DELETE_QUERY;
DEL_LOOP:
LOOP
EXECUTE v_DELETE_STATEMENT;
IF SQLCODE = 100 THEN
INSERT INTO TEP.DEBUG_LOG(PROC_NAME,LOG_TIME,MESSAGE) VALUES('DELETE_WITH_COMMIT_COUNT',CURRENT_TIMESTAMP,'ALL DATA DELETED FROM'||v_TABLE_NAME||'QUERY USED IS'||v_DELETE_QUERY);
LEAVE DEL_LOOP;
END IF;
COMMIT;
END LOOP;
COMMIT;
END P1
As I researched ,it is being said that it is the interruption code in DB2.
How to handle this in DB2
I added Tibco timeout to 6 minutes its working now.
deleting 2 million data from 12 tables
Thanks guys for helping

DB2 dynamic table name

I want to count the rows of a number of tables. But the table name should be used dynamically. I want to do that within one SQL statement.
I tried it with
BEGIN ATOMIC
FOR tmp AS (
SELECT tabschema || '.' || tabname tbl
FROM syscat.tables WHERE tabname LIKE '%CD') DO
(SELECT COUNT(*) FROM tmp.tbl);
END FOR;
END
but I receive the error
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL0204N "TMP.TBL" is an undefined name. LINE NUMBER=1. SQLSTATE=42704
and found no other working solution...
Is there a solution for that?
Thanks in advance.
I assume that your SELECT COUNT(*) FROM tmp.tbl should translate in multiple statements like
select count(*) from TABLECD
select count(*) from TABLE2CD
...
However, your query will try to do a count of the table TBL in the schema TMP.
You'll have to prepare the complete SQL statement, store it in a variable and pass it to the PREPARE statement (documentation ).
A rather complete stored procedure which somewhat fits your requirements can be found here . The result of the counts will be stored in a table COUNTERS which you can query afterwards.
//edit: this is the example from the topic, adapt to work (not tested since I have no DB2 instance to test atm):
CREATE PROCEDURE tableCount()
LANGUAGE SQL
BEGIN
DECLARE SQLCODE INTEGER DEFAULT 0;
DECLARE SQLSTATE CHAR(5);
DECLARE vTableName VARCHAR(20);
DECLARE vTableCount INTEGER;
DECLARE stmt varchar(2000);
DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE c1 CURSOR FOR
SELECT tabname from syscat.tables where tabschema='DB2ADMIN';
DECLARE C2 CURSOR FOR S2
DECLARE CONTINUE HANDLER FOR not_found
SET stmt = '';
Delete from COUNTERS;
OPEN c1;
getRows:
LOOP
FETCH c1 INTO vTableName;
IF SQLCODE = 0 THEN
SET stmt ='SELECT Count(*) FROM ' || vTableName;
PREPARE S2 FROM stmt;
OPEN C2;
SET vTableCount = 0;
FETCH C2 INTO vTableCount;
INSERT INTO COUNTERS (tableName, tableCount)
VALUES (vTableName, vTableCount);
CLOSE C2;
ELSE
LEAVE getRows;
END IF;
END LOOP getRows;
CLOSE c1;
END

What am I doing wrong in this MySQL stored procedure?

I'm trying to use the following stored procedure.
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `DeleteField`( IN _TABLENAME Text, IN _FIELDNAME text)
BEGIN
if exists (select * from information_schema.Columns
where table_name = _TABLENAME and column_name = _FIELDNAME)
then
alter table _TABLENAME drop column _FIELDNAME;
end if;
END
So I do Call('anytable','Anyfield') and I get the Error
Error Code:1146Table'Database._tablename'doesn't exist
This _tablename should be my parameter, not a string.
Plz some help before I hang myself, I love my life far too much.
I expect you will need to create a dynamic SQL query to do this.
An example of how to do this is at:
http://www.java2s.com/Code/SQL/Procedure-Function/Createadynamicstatementinaprocedure.htm
This would be the alter table replacement, though I have tested this.
DECLARE l_sql VARCHAR(4000);
SET l_sql=CONCAT_ws(' ',
'ALTER table ',_TABLENAME,' drop column ',_FIELDNAME);
SET #sql=l_sql;
PREPARE s1 FROM #sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;