MySQL - Using dynamicsql on Update - mysql

I'm working with MySQL 5.6 and trying to a simple procedure seen below.
drop procedure if exists procsBook;
DELIMITER $$
CREATE PROCEDURE procsBook()
main:begin
DECLARE abc VARCHAR(256);
DECLARE def VARCHAR(256);
DECLARE field_name2 VARCHAR(256);
DECLARE field_names VARCHAR(256);
DECLARE bkid BIGINT(20);
SET abc:= NULL;
SET def:= NULL;
SET field_name2:= NULL;
SET field_names:= NULL;
SET bkid:= 0;
select a.field_name,b.column_name into abc,def from table1 a join table2 b where a.field_label=b.field_label and b.table_name='Table13' and b.section_name='English' group by a.field_name limit 1;
IF(abc is not null)
then
IF (SUBSTRING(abc,7,1)=1)
THEN
SELECT book1,bookId INTO field_names,bkid FROM shelf1 WHERE rowstate=0;
SET #vSelectQury = CONCAT('update shelf2 set def = ' , field_names, ' where book_id = ', bkid);
PREPARE vDynamicSSql FROM #vSelectQury;
EXECUTE vDynamicSSql;
DEALLOCATE PREPARE vDynamicSSql;
END IF;
END IF;
END $$
DELIMITER ;
Call:
call procsBook();
This is throwing the following error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where book_id = 22' at line 1
Any help please?

I think you need to add quotes around field_names.
SET #vSelectQury = CONCAT('update shelf2 set ', def, ' = \'' , field_names,
'\' where book_id = ', bkid); PREPARE vDynamicSSql FROM #vSelectQury;
(or if your setup is different you may need to change the escaping for ' character)
Updated you should not directly use def, but use the value of def in this case.

Related

create multiple views with a mysql stored procedure

I wanted to build around 900 views(1 view for 1 table and based on 2 conditions) and i have all the table names in a table in mysql environment. I have created a stored proc . For test purpose i used limit 1.
I get below error when i call the sp.
Actually I re-wrote a mssql sp, did i miss anything here. please help
CALL ca_uim.sp_viewcreation_ontime() Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1 0.000 sec
USE `ca_uim`;
DROP procedure IF EXISTS `sp_viewcreation_ontime`;
DELIMITER $$
USE `ca_uim`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_viewcreation_ontime`()
BEGIN
DECLARE qos varchar(255);
DECLARE pos int;
DECLARE r_table varchar(255);
DECLARE view varchar(255);
DECLARE cview varchar(2048);
DECLARE done int default 0;
DECLARE qos_cursor CURSOR FOR SELECT DISTINCT qos,r_table FROM S_QOS_DATA ORDER BY 2 limit 1;
DECLARE continue handler for not found set done = 1;
OPEN qos_cursor;
-- Perform the first fetch.
FETCH qos_cursor INTO qos, r_table;
-- Check ##FETCH_STATUS to see if there are any more rows to fetch.
WHILE not done
DO
-- Check QOS name for '-' character & replace with '_' if exist
SET pos = LOCATE('-',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for '/' character & replace with '_' if exist
SET pos = LOCATE('/',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for '(' character & replace with '_' if exist
SET pos = LOCATE('(',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Check QOS name for ')' character & replace with '_' if exist
SET pos = LOCATE(')',qos, 1);
IF pos != 0
THEN
SET qos = INSERT(qos, pos, 1, '_');
END IF;
-- Create view
SET view = CONCAT('V_',qos);
SET cview = CONCAT('CREATE VIEW ',view,' AS ',
'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.samplestdev,D.samplerate,D.tz_offset ',
'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');
BEGIN
-- Suppress Error message for Views that don't exist
DECLARE CONTINUE HANDLER FOR 1051
set #stmt_str = CONCAT('DROP VIEW ',view);
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
END;
BEGIN
-- Create the View, Catch tables that don't have samplestdev & samplerate fields
DECLARE CONTINUE HANDLER FOR 1054
set #stmt_str = cview;
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
/* PRINT CONCAT('Created View: ' , view) */
END;
BEGIN
DECLARE CONTINUE HANDLER FOR 1054
SET cview = CONCAT('CREATE VIEW ',view,' AS ',
'SELECT Q.source,Q.target,Q.origin,Q.robot,Q.probe,D.sampletime,D.samplevalue,D.tz_offset ',
'FROM S_QOS_DATA Q JOIN ',r_table,' D ON Q.table_id=D.table_id');
set #stmt_str = cview;
prepare stmt from #stmt_str;
execute stmt;
deallocate prepare stmt;
/* PRINT CONCAT('Created View: ' , view) */
END;
-- PRINT 'qos: ' + #qos + ' ' + #r_table+' '+#view
-- PRINT #cview
-- This is executed as long as the previous fetch succeeds.
FETCH qos_cursor INTO qos, r_table;
END WHILE;
CLOSE qos_cursor;
END$$
DELIMITER ;
Thanks

error in my procedure MySQL find value INT in all tables database

I want find a number of UserId from all tables call searchUser(3,'UserId')
error: 0 14:30:14 call searchUser(3,'UserId') Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
DELIMITER $$
CREATE PROCEDURE `searchUser`( in_search int(11),in_column_name varchar(50) )
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE searchUserId int (11);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR
SELECT concat('SELECT COUNT(*) INTO #CNT_VALUE FROM `',table_name,'` WHERE `', in_column_name,'` = "',in_search,'"') ,table_name,column_name FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA = 'comments' and column_name=in_column_name ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
PREPARE trunc_cmd FROM "TRUNCATE TABLE temp_details;";
EXECUTE trunc_cmd ;
OPEN table_cur;
table_loop:LOOP
FETCH table_cur INTO db, tbl, clmn;
SET #searchUserId = searchUserId;
SELECT searchUserId;
PREPARE searchUserId FROM #searchUserId;
EXECUTE searchUserId;
SET COUNTER = #CNT_VALUE;
SELECT COUNTER;
IF COUNTER>0 THEN
INSERT INTO temp_details VALUES(db,tbl,clmn);
END IF;
IF done=1 THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE table_cur;
SELECT * FROM temp_details;
END
Problem must be due to:
DECLARE searchUserId int (11);
and:
SET #searchUserId = searchUserId;
SELECT searchUserId;
PREPARE searchUserId FROM #searchUserId;
EXECUTE searchUserId;
I assume that you are thinking searchUserId has a value. But nowhere in the code you assigned a value to it. By default it is a NULL. And hence the statement EXECUTE searchUserId is translate to EXECUTE NULL. This caused the error you specified.
To resolve it, you should first assign a proper value to the searchUserId variable declared.
BTW, why are you using the same variable name searchUserId for a local variable, global variable, and statement alias? It would confuse the readers of the program and hence is not advised to practice.
I guess problem lies in this statement-creation-statement:
SELECT concat('SELECT COUNT(*) INTO #CNT_VALUE FROM `',table_name,'` WHERE `', in_column_name,'` = "',in_search,'"') ,table_name,column_name FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA = 'comments' and column_name=in_column_name ;
Please execute the statement standalone and see what you get.

Stored procedure error on CALL

I am trying to call a procedure which compiles successfully but on calling I get this error:
Query: call proc5
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
This is my Stored procedure:
DELIMITER $$
CREATE DEFINER = `root` #`localhost` PROCEDURE `proc5` ()
BEGIN
DECLARE done BOOL DEFAULT FALSE ;
DECLARE tablename VARCHAR (100) ;
DECLARE tracktables CURSOR FOR
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'db1' ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE ;
OPEN tracktables ;
myloop :
LOOP
FETCH tracktables INTO tablename ;
IF done
THEN CLOSE tracktables ;
LEAVE myloop ;
END IF ;
SET #s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM ',
#tablename
) ;
PREPARE stmt1 FROM #s ;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1 ;
END LOOP ;
END $$
DELIMITER ;
Actually, I want to select all the tables from a database and insert those tables into one table which is in another database using MySQL Cursors. And when I call this stored procedure I get the above error.
The problem is that you are mixing declared variables and impromtu #vars.
var -> tablename does not equal var -> #tablename.
Change the set line to:
SET #s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM `'
,tablename
,'`'
) ;
Now it should work.
The backticks ` should not be needed, but are there just in case.

Mysql error in stored procudure

This stored procedure is to search through all tables and columns in database.
DELIMITER $$
DROP PROCEDURE IF EXISTS get_table $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
PROCEDURE `auradoxdb`.`get_table`(in_search varchar(50))
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE search_string VARCHAR(250);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR
SELECT concat(SELECT COUNT(*) INTO #CNT_VALUE
FROM `’,table_schema,’`.`’, table_name,’`
WHERE `’, column_name,’` REGEXP ”’,in_search,”’
)
, table_schema
, table_name
, column_name
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA NOT IN (‘information_schema’,'test’,'mysql’);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
# #Truncating table for refill the data for new search.
PREPARE trunc_cmd FROM “TRUNCATE TABLE temp_details;”
EXECUTE trunc_cmd ;
OPEN table_cur;
table_loop:LOOP
FETCH table_cur INTO search_string,db,tbl,clmn;
# #Executing the search
SET #search_string = search_string;
SELECT search_string;
PREPARE search_string FROM #search_string;
EXECUTE search_string;
SET COUNTER = #CNT_VALUE;
SELECT COUNTER;
IF COUNTER>0 THEN
# # Inserting required results from search to tablehhh
INSERT INTO temp_details VALUES(db,tbl,clmn);
END IF;
IF done=1 THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE table_cur;
# #Finally Show Results
SELECT * FROM temp_details;
END $$
DELIMITER ;
Following error occurs when execute this..
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'SELECT COUNT(*)
INTO #CNT_VALUE FROM `’,table_schema,’`.`’,table_name,’`' at line 12
(0 ms taken)
could any body please help me to solve this?
It appears that you are trying to dynamically build an SQL statement for each table. To do this, you will need to quote the text parts as if it was a normal string and concatenate the dynamic parts. Replacing the query with the following should work:
SELECT concat('SELECT COUNT(*) INTO #CNT_VALUE FROM ',
table_schema,'.', table_name,
' WHERE ', column_name,' REGEXP ''',in_search,''''
)
, table_schema
, table_name
, column_name
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA NOT IN ('information_schema','test','mysql');

Error when calling Mysql Stored procedure

This is my stored procedure to search throgh all databases,tables and columns.
This procedure got created with out any error.
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydb`.`get_table`$$
CREATE DEFINER=`root`#`%` PROCEDURE `get_table`(in_search varchar(50))
READS SQL DATA
BEGIN
DECLARE trunc_cmd VARCHAR(50);
DECLARE search_string VARCHAR(250);
DECLARE db,tbl,clmn CHAR(50);
DECLARE done INT DEFAULT 0;
DECLARE COUNTER INT;
DECLARE table_cur CURSOR FOR
SELECT concat('SELECT COUNT(*) INTO #CNT_VALUE FROM ',
table_schema,'.', table_name,
' WHERE ', column_name,' REGEXP ''',in_search,''''
)
,table_schema,table_name,column_name
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA NOT IN ('mydb','information_schema');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
# #Truncating table for refill the data for new search.
PREPARE trunc_cmd FROM 'TRUNCATE TABLE temp_details';
EXECUTE trunc_cmd ;
OPEN table_cur;
table_loop:LOOP
FETCH table_cur INTO search_string,db,tbl,clmn;
# #Executing the search
SET #search_string = search_string;
SELECT search_string;
PREPARE search_string FROM #search_string;
EXECUTE search_string;
SET COUNTER = #CNT_VALUE;
SELECT COUNTER;
IF COUNTER>0 THEN
# # Inserting required results from search to table
INSERT INTO temp_details VALUES(db,tbl,clmn);
END IF;
IF done=1 THEN
LEAVE table_loop;
END IF;
END LOOP;
CLOSE table_cur;
# #Finally Show Results
SELECT * FROM temp_details;
END$$
DELIMITER ;
But when calling this procedure following error occurs.
call get_table('aaa')
Error Code : 1064 You have an error in
your SQL syntax; check the manual that
corresponds to your MySQL server
version for the right syntax to use
near 'delete REGEXP 'aaa'' at line 1
(0 ms taken)
Where does "delete" come from? Do you have a column_name with that name? If so, use better names, not reserved ones, or use nasty backticks ` or ANSI-quotes " around the column name.
Constructions like this are vulnerable to SQL injection.