how to define a stored procedures which accepts 2 parameteres


Problem :

I´m trying to define a stored procedures which accepts 2 parameteres , one would be the table column which has to be equal with the second parameter i will provide. Code :

 DELIMITER $$

    CREATE DEFINER=`root`@`localhost` PROCEDURE `selectUserByField`(IN _field varchar(150) , IN _value varchar(150))
    BEGIN
  SET @sql = CONCAT('SELECT * FROM Users WHERE', _field, '=' ,_value);
  PREPARE stmt FROM @sql;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
    END

The thing is that i don´t know how to provide the _value param as a string. If i run it like this i get a Mysql 1064 near ´=myEmail´( params where ´userEmail´,´myEmail´). Thanks !



Solution :

In your below code, you are missing a space after WHERE. It should be like below; give a space after WHERE and in =

SET @sql = CONCAT('SELECT * FROM Users WHERE ', _field, ' = ' ,_value);

Mysql Tutorials

Mysql Howto..

MYSQL show uncounted

How can I delete a huge number of rows without freeze the UI?

How can I tell the raw size of a MySQL DB snapshots in Amazon RDS?

MYSQL: How to check if a string contains a value within the same row

How to display the tables within MySQL? Keeps showing empty set

MySQL replication - How to verify replicated data?

How to group MySQL results based on field

How to asynchronously update Mysql row count?

How to Append “Decimal” from MySQL query result in Python

How to translate this “R” formula into MySQL/PHP?

How to pick an html 'a' tag using php, especially by POST or GET technique?

How to select the most popular rows from today

How to make the hosting server run specific PHP code in timely manner?

How to store the properties of the goods in mysql? [closed]

How to replace words in a sql database

How to copy 1 column from one db to another db and set constants for another column in the 1st db during the INSRT INTO statement?

How To have MYSQL only show results that have been modified in the last 11 seconds

How to reorder row in SELECT query in MYSQL?

(Win7/Putty) How to use MySQL Workbench to connect to a DB server which is only accessible after ssh'ing to a jump server

Mysql workbench shows wrong database instances

How to fetch from Mysql something that has date for this week (from this monday till sunday) [closed]

How to get the last record before the last one in mysql?

If birthdate's are stored as type “Date” in MySQL, how would you determine if it's a user's birthday today in PHP?

How to define when you should hide “Show More” button?

How to fetch current year and month from MySQL in JAVA?

how to read my blob column from mysql to string type in hibernate

How to list the top 10 most duplicate rows?

How to delete mysql db rows remotely?

MySql How to set a local variable in an update statement (Syntax?)

How to upload timestamp data from a file into mysql