Incorrect parameters in the call to native function 'concat' - mysql

I am trying to create store procedure as follows
CREATE PROCEDURE `Test1` (IN projName VARCHAR(200), IN tablName VARCHAR(200))
BEGIN
set #cmd = concat('Create table', tablName ' as ( select name from samprojects.projects where type=',projName')');
PREPARE stmt FROM #cmd;
EXECUTE stmt;
END
And getting below error
ERROR 1583: Incorrect parameters in the call to native function 'concat'
Any idea how to resolve this.
I am using MYSQL workbench for running sql queries
SQL script generated by MYSQL workbench is as follows
USE `samprojects`;
DROP procedure IF EXISTS `Test1`;
DELIMITER $$
USE `samprojects`$$
CREATE PROCEDURE `Test1` (IN projName VARCHAR(200), IN tablName VARCHAR(200))
BEGIN
set #cmd = concat('Create table', tablName, ' as ( select name from samprojects.projects where type=',projName')');
PREPARE stmt FROM #cmd;
EXECUTE stmt;
END$$
DELIMITER ;

You are missing a comma after tablName:
set #cmd = concat('Create table', tablName, ' as ( select name from samprojects.projects where type=''',projName, ''')');

Related

how to input table as a variable in MySQL

I am trying to get data from a table using dynamic procedure with MySQL query.
Is there any way to do it specifically?
I am trying with this procedure below however the variable is still returning as a column.
See code below:
DELIMITER $
DROP PROCEDURE IF EXISTS REC_DATA$
CREATE PROCEDURE REC_DATA( IN_TABLE_NAME VARCHAR(20) )
BEGIN
BEGIN
SET #in_table = IN_TABLE_NAME;
SET #sql_stmt = CONCAT(' SELECT student_id FROM ' , #in_table);
SELECT #sql_stmt;
PREPARE stmt FROM #sql_stmt;
EXECUTE stmt;
END;
END$
DELIMITER ;
I am receiving this error: ERROR: ERROR 1054 (42S22): Unknown column 'SCORE' in 'field list'
Someone can help me?
Thanks!
I can replicate this error if I fail to enclose the parameter in the call statement with single quotes.
Follow the correct code:
DELIMITER $
DROP PROCEDURE IF EXISTS REPORT$
CREATE PROCEDURE REPORT( IN_TAB_NAME VARCHAR(20) )
BEGIN
DECLARE sql_stmt VARCHAR(100);
BEGIN
SET #in_table = IN_TAB_NAME;
SET #sql_stmt = CONCAT(' SELECT [columns] FROM ' , #in_table,
' GROUP BY [column]');
SELECT #sql_stmt;
PREPARE stmt FROM #sql_stmt;
EXECUTE stmt;
END;
END$
DELIMITER ;

Error when calling a SP from another one

I have created the following MySQL SP successfully..
CREATE DEFINER=`root`#`%` PROCEDURE `Common_Proc_Create_NewId`
(
TableName VARCHAR(250),
ColumnName VARCHAR(150),
OUT ReturnId BIGINT
)
BEGIN
DECLARE varb BIGINT;
SET #NewId:= CONCAT('SELECT (IFNULL(MAX(', ColumnName, '), 0) + 1) INTO ', varb, ' FROM ', TableName);
PREPARE Stmnt FROM #NewId;
EXECUTE Stmnt;
DEALLOCATE PREPARE Stmnt;
SET ReturnId = varb;
END$$
But when this was called from another SP I got the following error:
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
Calling SP
CREATE DEFINER=`root`#`%` PROCEDURE `Masters_Proc_Create_BranchType`(
BranchTypName VARCHAR(100)
)
BEGIN
CALL Common_Proc_Create_NewId('Masters_BranchType', 'BranchTypeId', #Id);
INSERT INTO Masters_BranchType (BranchTypeId, BranchTypeName) VALUES (#Id, BranchTypName);
SELECT #Id;
END$$
In your stored procedure Common_Proc_Create_NewId the part into varb was causing the issue and think it's not allowed that way in a prepared statement (not sure though). Instead the way you are doing, try like below and it works fine (a sample code included)
delimiter //
CREATE PROCEDURE dynamic1(IN tbl VARCHAR(64), IN col VARCHAR(64), OUT ret int)
BEGIN
SET #s = CONCAT('SELECT #i := (IFNULL(MAX(', col, '), 0) + 1) FROM ', tbl);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set ret = #i;
END
//
delimiter ;
call dynamic1('test1','col',#id);
select #id;

How can i create a procedure for select statement in 'IN Clause' for phpmyadmin

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);

Mysql stored procedure dynamic select with out variable

When I didn't use out variable, the stored procedure worked correctly, but when I execute the stored procedure with out variable, this error shows up:
MySQL said: #1327 - Undeclared variable: sp_result
Code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `test3`(OUT `sp_result` INT(11), IN `sp_where_param` VARCHAR(100), IN `sp_where_value` VARCHAR(100), IN `sp_table_name` VARCHAR(100))
NO SQL
BEGIN
DECLARE tbl_id VARCHAR(100);
SET tbl_id = CONCAT(sp_table_name,'_id');
SET #temp1=CONCAT('SELECT count(',tbl_id,') INTO sp_result FROM ',sp_table_name,' WHERE ',sp_where_param,' = \'',sp_where_value,'\'');
PREPARE stmt1 FROM #temp1;
EXECUTE stmt1;
END
Maybe without out variable also doesn't work :(
Try to use user variable -
CREATE DEFINER = 'root'#'localhost'
PROCEDURE test3 (OUT `sp_result` int(11), IN `sp_where_param` varchar(100), IN `sp_where_value` varchar(100), IN `sp_table_name` varchar(100))
NO SQL
BEGIN
DECLARE tbl_id varchar(100);
SET tbl_id = CONCAT(sp_table_name, '_id');
SET #temp1 = CONCAT('SELECT COUNT(', tbl_id, ') INTO #sp_result FROM ', sp_table_name, ' WHERE ', sp_where_param, ' = \'', sp_where_value, '\'');
PREPARE stmt1 FROM #temp1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
set sp_result = #sp_result;
END
...and add DEALLOCATE PREPARE statement.
it should be-
SET tbl_id = CONCAT(sp_table_name,'_id');
SET #temp1=CONCAT('SELECT count(',tbl_id,') INTO
like this
SET #tbl_id:= CONCAT(sp_table_name,'_id');
SET #temp1:=CONCAT('SELECT count(',tbl_id,') INTO

How to use variable as the table identifier in MYSQL

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