I am using mysql which i have a stored procedure which has an input variable.
I want to use this variable in a select statement (with like clause).
Eg:
DELIMITER $$
DROP PROCEDURE IF EXISTS `DeleteDataByTransactionID` $$
CREATE DEFINER=`root`#`%` PROCEDURE `DeleteDataByTransactionID`(in **$TransactionID** varchar(50))
BEGIN
delete from sqlstatements where tempsql like '%'+ **$TransactionID** + '%';
END $$
DELIMITER ;
Thanks
DELIMITER $$
DROP PROCEDURE IF EXISTS `DeleteDataByTransactionID`
$$
CREATE DEFINER=`root`#`%` PROCEDURE `DeleteDataByTransactionID`(TransactionID VARCHAR(50))
BEGIN
DELETE
FROM sqlstatements
WHERE tempsql LIKE CONCAT('%', TransactionID, '%');
END
$$
DELIMITER ;
Actually, the accepted answer's version is open to SQL injection if the caller does not properly parameterize the call to the stored procedure. I would recommend utilizing a prepared statement in the stored procedure as follows to be safe instead of relying on the caller:
DELIMITER $$
CREATE PROCEDURE `DeleteDataByTransactionID`
(
TransactionID VARCHAR(50)
)
BEGIN
SET #sql =
"DELETE
FROM sqlstatements
WHERE
tempsql LIKE CONCAT('%', ?, '%')";
SET #transid = TransactionID;
PREPARE stmt FROM #sql;
EXECUTE stmt USING #transid;
DEALLOCATE PREPARE stmt;
END
$$
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
delimiter //
DROP PROCEDURE IF EXISTS drop_table//
create procedure drop_table(in table_name varchar) #创建带参数的存储过程
begin
drop table if EXISTS table_name;
end//
delimiter ;
call drop_table('a')
i use Navicat to create a procedure to drop a parameter table in mysql.
Error:Invalid Stored Procedure Syntax
As commented by P.Salmon, you cannot pass substitute the procedure argument (ie a variable) directly in a sql statement. You need dynamic sql: build the query as a string, then execute it.
delimiter //
create procedure drop_table(in p_table_name varchar(200))
begin
set #q = concat('drop table if exists ', p_table_name);
prepare stmt from #q;
execute stmt;
deallocate prepare stmt;
end//
delimiter ;
Demo on DB Fiddle
Note: the varchar datatype needs a length.
I have the following stored procedure defined:
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
CREATE TABLE newTable LIKE `domain_template`;
END;;
DELIMITER ;
However when I call the procedure e.g.
CALL Create_Domain_Table('test_domain_table');
it creates a new table with the a name of newtable and not test_domain_table.
Is there something wrong with my syntax or am I referencing the parameter incorrectly?
Thanks
Use Dynamic SQL on this,
DROP PROCEDURE IF EXISTS `Create_Domain_Table`;
DELIMITER ;;
CREATE PROCEDURE `Create_Domain_Table`(IN newTable VARCHAR(255))
BEGIN
SET #sql = CONCAT('CREATE TABLE `', newTable,'` LIKE domain_template');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;;
DELIMITER ;
I want to make a MySQL Stored Procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
READS SQL DATA
BEGIN
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN ('+'+COMMUNITYIDS+'+')
AND p.pim_rin IN ('+'+RINSERIES+'+');
END $$
DELIMITER ;
As you can see I have two arguments to call this procedure RINSERIES which would be text format like '12312,1234,1239' similarly COMMUNITYIDS is like '2,5,8' but When I would pass, it is interpreting like
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN ('12312,1234,1239')
AND p.pim_rin IN ('2,5,8');
But I want this query to look like following
SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN (12312,1234,1239)
AND p.pim_rin IN (2,5,8);
How can I do this? Thanks!
Try this:
DELIMITER $$
DROP PROCEDURE IF EXISTS `SECTOR_INDUSTRY_SP` $$
CREATE PROCEDURE `SECTOR_INDUSTRY_SP`(IN RINSERIES TEXT, IN COMMUNITYIDS TEXT)
READS SQL DATA
BEGIN
SET #s = CONCAT("SELECT *
from PROFESSIONAL_IDENTITY_MERGED p , std_company_detail s
where (p.pim_company_id = s.cid) AND p.pim_community_id IN (", COMMUNITYIDS, ")
AND p.pim_rin IN (",RINSERIES,")");
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
I wrote a store procedure. It executes properly, but when I call the procedure, it shows the error:
Error Code : 1327 Undeclared variable:
Third
Please see my procedure below:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql_stam = CONCAT('SELECT news INTO ', #news,' FROM ',newsInfoTable,' WHERE ',CURDATE(),'=?;');
PREPARE s1 FROM #sql_stam;
SET #where_param = DATE_FORMAT(date_time,'%Y-%m-%d');
EXECUTE s1 USING #where_param;
SELECT #news;
END$$
DELIMITER ;
Calling parameter:
USE db_test;
CALL test_proc('tbl_morning_news');
Change your code into:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql_stam = CONCAT( 'SELECT news INTO #news FROM ',newsInfoTable
,' WHERE DATE(`date_time`) = CURDATE()' );
PREPARE s1 FROM #sql_stam;
SELECT #news;
END$$
DELIMITER ;