I do my filter with column of table pass through parameter(field after where in line 7) procedure but pass parameter column of table ,mysql doesn't know
use testphantrang;
DELIMITER $$
CREATE PROCEDURE `filter`(in field varchar(40), in giatri varchar(40),in index_of_page int)
begin
declare xx int ;
set xx = index_of_page*15;
SELECT * from test where field like CONCAT('%', giatri,'%') limit xx,15;
END; $$
DELIMITER ;
call `filter`('email','b',0);
Below is my table:
DELIMITER $$
CREATE PROCEDURE `filter`(in field varchar(40), in giatri varchar(40),in index_of_page int)
begin
declare xx int ;
set xx = index_of_page*15;
set #query = CONCAT('SELECT * from test where field rlike ''', giatri,''' limit ',xx,',15;');
PREPARE dynamic_statement FROM #query;
EXECUTE dynamic_statement;
DEALLOCATE PREPARE dynamic_statement;
END; $$
DELIMITER ;
call `filter`('email','b',0);
Related
I have a stored procedure in MySQL like:
DELIMITER $$
CREATE PROCEDURE addIns(IN insCode VARCHAR(21), IN insName VARCHAR(21), IN stat BIT(1))
BEGIN
INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active)
VALUES (insCode, insName, stat);
END$$
DELIMITER ;
But I want like this:
DELIMITER $$
CREATE PROCEDURE addIns(IN _institution)
BEGIN
INSERT INTO institution(institution.institution_code, institution.institution_name, institution.is_active)
VALUES (_institution.institution_code, _institution.institution_name, _institution.is_active);
END$$
DELIMITER ;
Because I want to use this stored procedure in Java Spring App.
How can I provide it?
My instutuion table is:
CREATE PROCEDURE addIns(IN _institution)
BEGIN
#sql := CONCAT( 'INSERT INTO institution (institution_code, institution_name, is_active) SELECT institution_code, institution_name, is_active FROM ' , _institution);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END
Plzzz Answer
I want to create a table in a stored procedure
But tabel name is stored in a variable.
Example
create procedure ABC
(
#tablename. // is a parameter in which table
name is stored
)
AS
BEGIN
Create table #tablename
(
Colmn one....
Colmn. two...
...............
)
End
but error is shown where
table #tablename
Q ) how can I fix this
I want to set a table name which is stored in a variable #tablename
You can't do that this way, You need prepared statements
DROP procedure IF EXISTS `ABC`;
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `ABC`(
IN _tablename VARCHAR(50) -- is a parameter in which table
-- name is stored
)
BEGIN
SET #sql = CONCAT('
Create table ',_tablename,'
(
Colmn1 INT,
Colmn2 INT
);');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
End$$
DELIMITER ;
I want to call a procedure with some conditions,
This is my code.
DELIMITER $$
USE `jijo_db`$$
DROP PROCEDURE IF EXISTS `view_all_user_details_with_limit`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `view_all_user_details_with_limit`(IN StartNo INT,IN EndNo INT, IN OrderVal VARCHAR(10),IN Cond VARCHAR(50))
BEGIN
SELECT * FROM `tbl_user_details` WHERE Cond ORDER BY OrderVal LIMIT StartNo,EndNo;
END$$
DELIMITER ;
procedure call - CALL view_all_user_details_with_limit(0,10,'',"NAME LIKE '%a%'");
but I dont get any result. why ????
If you want to have variable WHERE, LIMIT, and ORDER BY conditions, you will need to create a prepared statement in your stored procedure.
Try something like this:
DELIMITER $$
USE `jijo_db`$$
DROP PROCEDURE IF EXISTS `view_all_user_details_with_limit`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `view_all_user_details_with_limit`(IN StartNo INT,IN EndNo INT, IN OrderVal VARCHAR(10),IN Cond VARCHAR(50))
BEGIN
SET #q = CONCAT('SELECT * FROM `tbl_user_details` WHERE ', Cond);
IF OrderVal != '' THEN
SET #q = CONCAT(#q, ' ORDER BY ', OrderVal);
END IF;
SET #q = CONCAT(#q, ' LIMIT ', StartNo, ', ', EndNo - StartNo + 1);
PREPARE stmt FROM #q;
EXECUTE stmt;
END$$
DELIMITER ;
Your problem seems to be happening in WHERE Cond.
A MySQL Stored Procedure won't interpret that string as an expression, but will instead attempt to cast it to a Boolean.
You can see by running SELECT CAST("NAME LIKE '%a%'" AS UNSIGNED); that the string will be interpreted as False, and thereby you won't get any results.
I suggest instead that you accept in your Cond variable a string such as '%a%' and then update your query to:
SELECT *
FROM `tbl_user_details`
WHERE `NAME` LIKE Cond
ORDER BY OrderVal
LIMIT StartNo, EndNo;
I have two stored procedure the first parameter with static and it works well
but when I but dynamic parameter it gives me an error Undeclared variable: limitvar
1/procedure
`DELIMITER $$
DROP PROCEDURE IF EXISTS `gestprospects`.`recClientsFiltre` $$
CREATE PROCEDURE `recClientsFiltre`(IN params text, in limitvar int , in offsetvar int )
BEGIN
SET #query := concat(concat("SELECT ... WHERE CONCAT(clients.id,'-') in (",params),") limit 10,10");
PREPARE stmt FROM #query ;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;`
2/procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `gestprospects`.`recClientsFiltre` $$
CREATE PROCEDURE `recClientsFiltre`(IN params text, in limitvar int , in offsetvar int )
BEGIN
SET #query := concat(concat("SELECT ... WHERE CONCAT(clients.id,'-') in (",params),") limit limitvar,offsetvar");
PREPARE stmt FROM #query ;
EXECUTE stmt ;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
Thank you for your reply
You are including the litteral strings limitvar and offsetvar in your dynamic query, whereas you want to include their value.
#query := CONCAT(
"SELECT ... WHERE CONCAT(clients.id,'-') IN (", params,")",
" LIMIT ", limitvar, ", ", offsetvar);
Notice how you can concatenate several strings in one call to CONCAT();
I've created a stored procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `zero`.`sp_for_insert_into_account_db`$$
CREATE PROCEDURE `zero`.`sp_for_insert_into_account_db` (usr_key char(6),usr_name varchar(15),usr_password varchar(15),OUT output_message INT)
BEGIN
DECLARE no_of_row INT;
SELECT COUNT(*) INTO no_of_row from account_db;
IF no_of_row < 4 THEN
SET #s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (',usr_key,usr_name,usr_password,')');
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET output_message=1;
ELSE
SET output_message=0;
END IF;
END$$
DELIMITER ;
I'm calling it with query
call sp_for_insert_into_account_db('a','b','c',#output_ message);
The error is like:
Column count does not match value...
I'm passing 4 arguments...
Why is this error occurring?
I've already checked with this syntax (by default parameter is IN type)
sp_for_insert_into_account_db(IN usr_key char(6),
IN usr_name varchar(15),
IN usr_password varchar(15),
OUT output_message INT)
Problem is also here:
SET #s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (',usr_key,usr_name,usr_password,')');
You are trying to insert 3 values and the concatenation returns 1
use this instead:
SET #s = CONCAT('insert into account_db (USR_KEY,USR_NAME,USR_PWD) VALUES (\'',usr_key,'\',\'',usr_name,'\',\'',usr_password,'\')');
Not entirely sure why you're using prepared statements/dynamic sql when you dont need to ?? See the following example which i've cleaned up for you a little:
drop procedure if exists sp_for_insert_into_account_db;
delimiter #
create procedure sp_for_insert_into_account_db
(
in p_usr_key char(6),
in p_usr_name varchar(15),
in p_usr_pwd varchar(15),
out p_output_message tinyint unsigned
)
begin
declare v_no_of_row int unsigned default 0;
set p_output_message=0;
select count(*) into v_no_of_row from account_db;
if v_no_of_row < 4 then
insert into account_db(usr_key, usr_name, usr_pwd) values (p_usr_key, p_usr_name, p_usr_pwd);
set p_output_message = 1;
end if;
end#
delimiter ;
call sp_for_insert_into_account_db (...);
EDIT
are you a COBOL PROGRAMMER FROM THE 1970'S AND IS THAT WHY YOU HAVE TO USE CAPS ?