I'm trying the following but I'm getting the following error:
ERROR 1054 (42S22): Unknown column 'f' in 'where clause'
I'm seriously confused because f is a parameter of createtableTest...
CREATE PROCEDURE createtableTest
(
tname2 varchar(20),
f varchar(20)
)
BEGIN
DROP TABLE IF EXISTS tname2;
CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like f;
END;
Since f is contains the value, a dynamic sql is needed so we can concatenate it with the original query,
DELIMITER $$
CREATE PROCEDURE createtableTest(IN tname2 varchar(20),IN f varchar(20))
BEGIN
DROP TABLE IF EXISTS tname2;
SET #sql = CONCAT('CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like ''%',f,'%''');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
for example, the value of f is hello, the concatenated string will produce
CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like '%hello%'
UPDATE
MySQL Prepared Statements
Aside from concatenation, you can also parameterized the value which is the best way, ex
DELIMITER $$
CREATE PROCEDURE createtableTest(IN tname2 varchar(20),IN f varchar(20))
BEGIN
DROP TABLE IF EXISTS tname2;
SET #sql = CONCAT('CREATE TABLE tname2 as SELECT * FROM data WHERE group_name like ?');
PREPARE stmt FROM #sql;
SET #val = CONCAT('%', f, '%');
EXECUTE stmt USING #val;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
Related
As the title mention, i have a problem to apply simple filter on a query result using the operator (IN) in a MySQL Stored Procedured.
The simple example of the Stored Procedured looking like this
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_example`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_example`(
`filter_uid_value` TEXT
)
BEGIN
SELECT
a.id
, a.name
, a.uid
FROM
employee_example a
WHERE 1=1
AND a.uid IN (filter_uid_value)
END$$
DELIMITER;
so when i call this stored procedured, for example like this
CALL sp_example("du4jgjVRJGs,oKxU3SzV8CK");
the filter is not apply, i wonder how can fix this
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_example`(
`filter_uid_value` TEXT
)
SELECT id, name, uid
FROM employee_example
WHERE FIND_IN_SET(uid, filter_uid_value);
DELIMITER not needed.
DELIMITER ;;
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_example`(
`filter_uid_value` TEXT
)
BEGIN
SET #sql := CONCAT( 'SELECT id, name, uid ',
'FROM employee_example ',
'WHERE uid IN ("',
REPLACE(filter_uid_value, ',', '","'),
'")' );
PREPARE stmt FROM #sql;
EXECUTE stmt;
DROP PREPARE stmt;
END;;
DELIMITER ;
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 ;
Goal
I want to create table as follow, the format of table name is tbl+_+date , but I want to write a procedure to create when I input a given date.
drop table if exists tbl_20200802 ;
create table tbl_20200802 (index (USERID) )
select * from a;
Try
DELIMITER //
DROP PROCEDURE IF EXISTS pro_ljj_push_pre_id;
CREATE PROCEDURE pro_ljj_push_pre_id(
IN test_date date
)
BEGIN
set #table_name=CONCAT('tbl_', test_date);
SET #t1 =CONCAT(
'drop table if exists '
,#table_name
,';'
,'\n'
,'create table ', tab_name
, ' (index (USERID))'
,'\n'
,'SELECT * FROM a');
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
--
END //
DELIMITER ;
CALL pro_ljj_push_pre_id('20200802');
1054 - Unknown column 'tab_name' in 'field list'
You have some syntax error in your code,
However, the following is your desired. Change the structure of table as you want.
DELIMITER $$
DROP PROCEDURE IF EXISTS pro_ljj_push_pre_id$$
CREATE PROCEDURE pro_ljj_push_pre_id(IN test_date VARCHAR(50))
BEGIN
set #table_name=CONCAT('tbl_', test_date);
SET #t1 =CONCAT('drop table if exists ',#table_name);
SELECT #t1;
PREPARE stmt3 FROM #t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
SET #t2 =CONCAT('create table ', #table_name, ' (id INT PRIMARY KEY, name VARCHAR(30));');
SELECT #t2;
PREPARE stmt4 FROM #t2;
EXECUTE stmt4;
DEALLOCATE PREPARE stmt4;
--
END $$
DELIMITER ;
CALL pro_ljj_push_pre_id('20200802');
I want to create a procedure for a select query in which the where clause will have a IN Clause. I had created one procedure like below butu its not working-
CREATE DEFINER = `root`#`localhost` PROCEDURE `agentin` ( IN `code` VARCHAR( 100 ) ) NOT DETERMINISTIC NO SQL SQL SECURITY DEFINER
BEGIN
SELECT * FROM AgentInformation WHERE AgentCode IN (code);
END
After putting the In clause values like --> IN ('CG001','CG002')
I am getting null values and the query made by phpmyadmin was
SET #p0 = '''CG001'',''CG002''';
CALL agentin (
#p0
);
Please help regarding it , Thanks
Here it is an example -
DELIMITER $$
CREATE PROCEDURE procedure1(IN id_param VARCHAR(255))
BEGIN
SET #sql = CONCAT('SELECT * FROM table WHERE id IN (', id_param, ')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
DELIMITER ;
SET #id = '1,2,3,4,5';
CALL procedure1(#id);
If i have this:
CREATE TABLE ftable (
id INT,
fvalue VARCHAR(14)
);
INSERT INTO ftable VALUES (1,'tableB'),(2,'tableA');
CREATE TABLE tableA (
value VARCHAR(14)
);
SELECT #tmp:=fvalue FROM ftable WHERE id=2;
How do I make it so I can do this:
INSERT INTO #tmp VALUES ('buhambug');
Becuase as far I know that throws a mysql error.Can someone show me a sqlfiddle of the solution? Or maybe I'm thinking about this the wrong way?
You need to use dynamic SQL to use a variable as an object name:
SET #tmp = (SELECT fvalue FROM ftable WHERE id=2);
SET #SQL = CONCAT('INSERT INTO ',#tmp,' VALUES (''buhambug'')');
PREPARE stmt FROM #SQL;
EXECUTE stmt;
SQL FIDDLE
You can't do in static sql.
You can do it in stored procedure:
delimiter $$
drop procedure if exists test_call$$
create procedure test_call(table_in varchar(100))
begin
set #q = concat("select * from ", table_in);
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
end$$
delimiter ;
call test_call('TeableA');
drop procedure if exists test_call;
In general dynamic read from dynamic tables is not a good decision