1

I have a mysql stored procedure.

DELIMITER //  
CREATE PROCEDURE GetRecordsByAge(IN Age INT)  
BEGIN 
  SELECT * FROM test1 WHERE age = Age;  
END //  
DELIMITER ;

When I run this stored procedure through java then it is giving all records of the table.

But if I have a condition in select statement. Why this is happening?

here is set of lines of my code:-

CallableStatement cStmt = null;
cStmt = con.prepareCall("{ CALL GetSpecificRecord(?) }");
cStmt.setInt(1, 25);
cStmt.execute();
ResultSet rs1 = cStmt.getResultSet();

When I print this result set this will give all records of the table.

Where is the problem?

Thanks

2
  • you are calling GetSpecificRecord, but your proc is GetRecordsByAge Commented Feb 22, 2013 at 7:22
  • Have you tried testing the sp in mysql itself.. Are you getting the desired result? Iswanto spotted it!! Commented Feb 22, 2013 at 7:24

2 Answers 2

10

The reason why you are getting all records is because of column name collision causing the WHERE clause to be always true. Change the name of the parameter that is not the same with the name of the column you are comparing with,

DELIMITER //  
CREATE PROCEDURE GetRecordsByAge(IN _Age INT)  
BEGIN 
  SELECT * FROM test1 WHERE age = _Age;  
END //  
DELIMITER ;

The second is, you are calling different stored procedure. instead of GetRecordsByAge, you are calling GetSpecificRecord.

Sign up to request clarification or add additional context in comments.

Comments

4

Because the condition WHERE age = Age is always ture.

Change the parameter name Age to something else:

DELIMITER //  
CREATE PROCEDURE GetRecordsByAge(IN AgeParam INT)  
BEGIN 
  SELECT * FROM test1 WHERE age = AgeParam;  
END //  
DELIMITER ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.