I want to create a stored procedure which accepts all the values in the IN parameter as a single string.
DELETE FROM object
WHERE Type NOT IN
('ListGrid',
'TextField',
'SpinBox',
'MenuButton',
'ListGrid',
'RadioButton',
'DropDown',
'PopUp',
'Element',
'Checkbox',
'TreeDropDown',
'TblColumn',
'Button',
'Link',
'Filter',
'TblRow',
'GridRow',
'Popup')
This is an example of one I've tried but it does not work.
DELIMITER //
CREATE PROCEDURE deleteObjectTypes(IN p_type VARCHAR(255))
BEGIN
SET #query = CONCAT ('DELETE FROM object WHERE Type NOT IN (',p_type,')');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
I get the following error:
#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 ''List)' at line 1
When running this query:
CALL deleteObjectTypes("'ListGrid1','TextField1','SpinBox1','MenuButton1','ListGrid2','TextField2','SpinBox2','MenuButton2','ListGrid3','TextField3','SpinBox3','MenuButton3','ListGrid4','TextField4','SpinBox4','MenuButton4','ListGrid5','TextField5','SpinBox5','MenuButton5','ListGrid6','TextField6','SpinBox6','MenuButton6'")
You need to change the VARCHAR size to it's maximum value (or a lower significant value).
DELIMITER //
CREATE PROCEDURE deleteObjectTypes(IN p_type VARCHAR(65535))
BEGIN
SET #query = CONCAT ('DELETE FROM object WHERE Type NOT IN (',p_type,')');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
However, note that the limit is lower if you use a multi-byte character set:
VARCHAR(21844) CHARACTER SET utf8
As seen here.
(sorry i cannot add comments too low reputation)
Your procedure looks ok, maybe the problem is somewhere else? note that we have defined as a varchar 255 characters and the example you provided more than this number (291 characters)
You should give this a try (shortened example):
DELETE
FROM
object
WHERE
NOT FIND_IN_SET( Type, 'ListGrid,TextField,SpinBox,MenuButton,ListGrid' );
and with stored procedure
DELIMITER //
CREATE PROCEDURE deleteObjectTypes(IN p_type VARCHAR(255))
BEGIN
DELETE
FROM
object
WHERE
NOT FIND_IN_SET( Type, p_type );
END //
DELIMITER ;
CALL deleteObjectTypes( 'ListGrid1,TextField1,SpinBox1,MenuButton1,ListGrid2,TextField2,SpinBox2,MenuButton2,ListGrid3,TextField3,SpinBox3,MenuButton3,ListGrid4,TextField4,SpinBox4,MenuButton4,ListGrid5,TextField5,SpinBox5,MenuButton5,ListGrid6,TextField6,SpinBox6,MenuButton6' );
Related
I'm trying to create a stored proc that will take two input parameters
1.Table name
2. Column name
and find the maximum from the column.
The stored proc is created successfully in DB.Here is the definition
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GETMAXIMUMID`(IN `tab_name` VARCHAR(40), IN `col_name` INT(10))
BEGIN
SET #t1 =CONCAT('SELECT max(',col_name,') FROM ',tab_name );
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END$$
DELIMITER ;
Now I am calling it as
CALL GETMAXIMUMID('tbl_segment','id');
but it is always returning 0 as result for every table.
Please let me know where I am wrong.I am trying stored proc for the first time and this is what I have learnt in few days.
You declared col_name is an integer parameter, but you pass 'id' as the value.
MySQL converts a string to an integer by reading any initial digit characters and ignoring the rest. If there are no digit characters, the value is converted to 0.
So you are preparing and executing this query:
SELECT MAX(0) FROM tbl_segment
Which naturally returns zero.
CREATE
/* DEFINER=`root`#`localhost` */
PROCEDURE `GETMAXIMUMID`(IN `tab_name` VARCHAR(64), IN `col_name` VARCHAR(64))
BEGIN
SET #t1 =CONCAT('SELECT max(',col_name,') FROM ',tab_name );
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
END
fiddle
Can anybody help me with this JSP problem.
Im trying to update the database using code similar to this:
So, I have this on my Servlet:
String QueryCondition = "id = 1";
That will be passed to this stored procedure:
CREATE
DEFINER=`root`#`localhost`
PROCEDURE `storedprocedure_1`(QueryCondition TEXT)
BEGIN
UPDATE users SET name = 'John'
WHERE QueryCondition;
END
I was thinking if this is possible because the update always fail.
If this isn't possible can you recommend how can i do such thing
You can use it in a stored procedure but with a prepared statement.
Example:
DELIMITER //
CREATE
DEFINER=root#localhost
PROCEDURE storedprocedure_1(QueryCondition TEXT)
BEGIN
SET #query := CONCAT( 'UPDATE users SET name = \'John\' WHERE ',
QueryCondition );
PREPARE stmt FROM #query;
EXECUTE stmt;
DROP PREPARE stmt;
END;
//
DELIMITER ;
I am trying to get this query to work
delimiter //
CREATE PROCEDURE test2(IN tbl CHAR(64), IN col CHAR(64))
BEGIN
SET #s = CONCAT('SELECT ',col,' FROM ',tbl );
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
//
delimiter ;
This is the code used to call it
CALL test2(groups,NAME)
And this is the error I get:
1 Unknown column 'groups' in 'field list' SQL2.sql 1 12
The table name is groups, and the column name is NAME - Why is this not working
Well, you have written a stored procedure that takes character strings, and you have passed identifiers.
Try calling it thusly:
CALL test2('groups', 'NAME')
What I want to do is,create a table in mysql by passing the table name as a parameter in the stored procedure.I'm using following code for stored procedure in mysql.
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE', tablename, '(month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //
and when i call it
CALL createtable('account');
I get the following error
You have an error in your SQL syntax; check the MySQL server version for the right syntax to us...
I don't know where I'm wrong..
You forgot the spaces before and after your table name. Try
DELIMITER //
CREATE PROCEDURE createtable(IN tablename varchar(20))
BEGIN
SET #s=CONCAT('CREATE TABLE ', tablename, ' (month varchar(20))');
PREPARE stmt FROM #s;
EXECUTE stmt;
END //
I'm trying to set up a stored proc that (in part) drops a user. I've tried several versions of DROP USER that "expand" the argument and I can't find one that works. Can someone help?
DELIMITER //
CREATE PROCEDURE `dropuser`( IN inUser varchar(255) )
BEGIN
#DROP USER concat(inUser,"#localhost");
#DROP USER inUser "#localhost";
#DROP USER "inUser#localhost";
END//
DELIMITER ;
CALL dropuser("asdf");
All three of them fail in the same way:
ERROR 1064 (42000) at line 4: 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 '(inUser,"#localhost");
Obviously I'm getting the expansion wrong.
delimiter //
drop procedure if exists dropuser //
create procedure dropuser( in inUser varchar(255) )
begin
set #str = concat('drop user ', "'",inUser,"'#","'localhost'");
prepare stmt from #str;
-- select #str;
execute stmt;
deallocate prepare stmt;
end//
delimiter ;
call dropuser('pippo');