Link to home
Start Free TrialLog in
Avatar of kingjely
kingjelyFlag for Australia

asked on

Mysql passing string into a parameter

Hi guys,

Is there any way possible to simply create a parameter,
and  Call pass it a string like this.




DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE PROCEDURE `test`(IN wherestr VARCHAR(255))
BEGIN
      select *
      from summary
      where  wherestr
      group by storename;
END $$
DELIMITER ;

CALL `test`('storenumber = 2')
Avatar of Kalpan
Kalpan
Flag of India image

Please refer the follwoing

DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE PROCEDURE `test`(IN wstr VARCHAR(255))
BEGIN
      select *
      from summary
      where  wherestr = wstr
      group by storename;
END $$
DELIMITER ;

CALL `test`('2')
Avatar of kingjely

ASKER


Hi Kmax
I get, Unknown column 'wherestr' in where clause
As error suggests you don't have wherestr as a field...
You can ofcourse pass the string, just replace the FIELDNAME that you want to compare with the passed parameter.

 select *       from summary      where  FIELDNAME = wstr       group by storename;

Hi again,

Kate,
With this, I want to just create an empty parameter and call pass it a string, which the procedure excecutes.

LIKE this even, (with no where clause, just the `wherestr`)



DELIMITER $$
DROP PROCEDURE IF EXISTS `test` $$
CREATE PROCEDURE `test`(IN wherestr VARCHAR(255))
BEGIN
      select *
      from summary
      wherestr
      group by storename;
END $$
DELIMITER ;

call  `wherestr'(WHERE STORENUMBER =2 and BRAND NUMBER = 2)

I hope that makes sense to you =D
sorry that call should be

call  `test3'(WHERE STORENUMBER =2 and BRAND NUMBER = 2)
SOrry again,

call  `test'(WHERE STORENUMBER =2 and BRAND NUMBER = 2)
SOLUTION
Avatar of Kalpan
Kalpan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Hi Kmax, I understand somewhat how to do simple stored procedure and call,
BUT, I want to type the where clause into the call `where`(parameter)

call `test`('`storenumber` =2 and `brandnumber` = 2');


actually i will not be typing it in, it will be passed from another application.
The application will pass into call the `where` clause, based on a list box where a user ticks which stores, brands, departments ect they wish to include in a report.

For what i need to know,

can you call pass the whole where clause, in the parameter, as a string,



Hi Flytox06,

I think this is what i need,

could you include the call i would need.

I have this
call `test`('`storenumber` =2 and `brandnumber` = 2');
but it just passes EVERYTHING , not just store2, and brand 2.
Thanks GUYS
if you call test("storenumber =2 and brandnumber = 2"); then it will exactly launch :

SELECT * FROM summary storenumber =2 and brandnumber = 2 (which is a wrong syntax).

So, modify the prepared statement to include WHERE after summary or simply call :

test("WHERE storenumber =2 and brandnumber = 2");

If I where you, I would add WHERE after the summary keyword. And be careful to sql injection...