Delimiter //
CREATE Procedure SearchTest(IN FieldName varchar(30),IN FieldValue varchar(30))
BEGIN
SET #query = CONCAT('select count(*) from Some_table_name where ', FieldName,'=',FieldValue);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
Delimiter ;
mysql> call SearchTest('accession_no','L138362194');
ERROR 1054 (42S22): Unknown column 'L138362194' in 'where clause'
Your database table does not have a column L138362194.
You can fix that by adding such a column, or by choosing a column that exists.
The error is generated because of missing quotes. Here is an SQL Fiddle that demonstrates the problem.
Modify the stored procedure:
DELIMITER //
CREATE PROCEDURE SearchTest(IN FieldName VARCHAR(30), IN FieldValue VARCHAR(30))
BEGIN
SET #query = CONCAT('SELECT COUNT(*) FROM `some_table_name` WHERE ', FieldName, ' = \'', FieldValue, '\';');
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
Related
In the procedure I'm writing I need to store the result of a dynamic SQL query into a local variable. Something like this:
DELIMITER //
CREATE PROCEDURE test_maxsum(uid varchar(25))
BEGIN
DECLARE maxsum int;
SET #SQL = CONCAT('SELECT MAX(sum_val) FROM ', uid);
PREPARE stmt FROM #SQL;
EXECUTE stmt INTO maxsum;
DEALLOCATE PREPARE stmt;
SELECT maxsum;
END//
DELIMITER ;
Local variables cannot be assigned in prepared statements. You can use user-defined variables though:
DELIMITER //
CREATE PROCEDURE test_maxsum(uid varchar(25))
BEGIN
SET #SQL = CONCAT('SELECT MAX(sum_val) INTO #maxsum FROM ', uid);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT #maxsum;
END//
DELIMITER ;
I am studying mysql. Here is my code of practicing stored procedure.
CREATE DEFINER=`raxuser`#`%` PROCEDURE `test`(in fc_frm_date char(5), in fc_to_date char(5))
BEGIN
declare frm_date char(5);
declare to_date char(5);
select fc_frm_date into #frm_date;
select fc_to_date into #to_date;
select distinct #frm_date, #to_date, fc_frm_date, fc_to_date;
set #s = CONCAT('select distinct #frm_date, #to_date, fc_frm_date, fc_to_date;');
PREPARE stmt FROM #s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
END
When I run it , the first select query get correct while the second one shows error code as
Error Code: 1054 Unknown column 'fc_frm_date' in 'field list'
I thought the two select queries should be equal but not. Can anyone help to explain?
Many thanks.
I think the right way to do this (UNTESTED) is:
set #s = CONCAT('select distinct ? , ? , ? , ?;');
PREPARE stmt FROM #s;
EXECUTE stmt USING #frm_date, #to_date, fc_frm_date, fc_to_date;
DEALLOCATE PREPARE stmt;
Try it:
CREATE DEFINER=`raxuser`#`%` PROCEDURE `test`(fc_frm_date char(5), fc_to_date char(5))
BEGIN
DECLARE frm_date CHAR(5);
DECLARE to_date CHAR(5);
SELECT fc_frm_date INTO #frm_date;
SELECT fc_to_date INTO #to_date;
SELECT fc_frm_date INTO #fc_frm_date;
SELECT fc_to_date INTO #fc_to_date;
SET #s = 'select distinct #frm_date, #to_date, #fc_frm_date, #fc_to_date';
PREPARE stmt FROM #s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SELECT #s;
END
i am trying to write a stored procedure which updates the field of a table as specified in the argument .i am getting syntax error in #sql statment.please help
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql='update department set ?=? where departmentid=?';
prepare stmt from #sql;
execute stmt using #att,#updated,#primk;
deallocate prepare stmt;
end if;
end
Parameters don't work for column or table names, only for values. Do it like this:
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql=CONCAT('update department set ', #att, '=? where departmentid=?;');
prepare stmt from #sql;
execute stmt using #updated,#primk;
deallocate prepare stmt;
end if;
end
I am trying to run the dynamic query using a stored procedure in which I am sending the where condition case for example.
My sample stored procedure is shown here:
CREATE PROCEDURE `Storedproc`(IN getwhereconditon varchar(1000))
BEGIN
set #param=getwhereconditon ;
SET #S=concat('Select * from table where (1)',#param);
PREPARE stmt1 FROM #s;
EXECUTE stmt1;
END
Here my stored procedure's name is Storedproc in which I am passing the where condition details and when I call my stored procedure I am getting this 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 '0tablecol=1' at line
Try
DELIMITER //
CREATE PROCEDURE myproc(IN _where VARCHAR(512))
BEGIN
SET #sql = 'SELECT * FROM table1';
IF COALESCE(_where, '') <> '' THEN
SET #sql = CONCAT(#sql, ' WHERE ', _where);
END IF;
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
or
DELIMITER //
CREATE PROCEDURE myproc(IN _where VARCHAR(512))
BEGIN
SET #sql = CONCAT('SELECT * FROM table1', COALESCE(CONCAT(' WHERE ', NULLIF(_where, '')), ''));
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
Sample usage:
CALL myproc(NULL);
CALL myproc('name=''John''');
CALL myproc('age > 25');
CALL myproc('age < 28 OR age > 30');
Here is SQLFiddle demo
Here is my code
Drop procedure if exists test//
CREATE PROCEDURE test(IN woeid VARCHAR(15))
BEGIN
SET #w1 := woeid;
SET #sql = CONCAT('CREATE OR REPLACE VIEW temp
AS
SELECT *
FROM test_table gp
WHERE gp.name =', #w1);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END//
Delimiter ;
call test('ABCD');
I am getting error as
Error code: 1054. Unknown column 'ABCD' in 'where' clause
Please help.
It sounds as though you're needlessly using views, when some other approach would be more appropriate.
However, the reason it isn't working is that you haven't quoted your string literal, so the resulting SQL contains WHERE gp.name = ABCD whereas it at very least needs to be WHERE gp.name = 'ABCD'. You can use MySQL's QUOTE() function for this purpose, but it's better to parameterise the value:
DELIMITER //
DROP PROCEDURE IF EXISTS test//
CREATE PROCEDURE test(IN woeid VARCHAR(15))
BEGIN
SET #w1:=woeid, #sql:=CONCAT('
CREATE OR REPLACE VIEW temp AS
SELECT *
FROM test_table
WHERE name = ?
');
PREPARE stmt FROM #sql;
EXECUTE stmt USING #w1;
DEALLOCATE PREPARE stmt;
SET #w1:=NULL, #sql:=NULL;
END//
DELIMITER ;
CALL test('ABCD');