Question

It appears that performing a where clause on a table with Mysql works as expected (first code example). However, when a where clause is performed on a view, the where clause appears to be ignored.

To keep things simple, I've tested a simple where clause on a standard table- this performs as expected, additionally a 'proof test' was ran to see if a mysql procedure can do a simple select * on a view (it did). Thus, it's only when the where clause is added to quiers on views.

How can I perform a WHERE query within a stored procedure on a View?

Example code 1 (working with normal table):

DELIMITER //
CREATE PROCEDURE p1 (id INT, fname VARCHAR(250))
BEGIN
DECLARE staffId INT;
DECLARE name VARCHAR(250);
SET name = fname;
SET staffId = id;
SELECT * FROM Staff WHERE ID_Staff = staffId AND Fname = name;
END; //
DELIMITER

Result set returned from the above performs as expected, acting upon the where clause.

However, with when using a where clause on a view the where clause is ignored:

Example code (not working as expected)

DELIMITER //
CREATE PROCEDURE p2 (year INT)
BEGIN
DECLARE a CHAR(4);
SET a = year;
SELECT * FROM TotalHoursView WHERE Year = "2014";
END; //
DELIMITER ;

Note: 'TotalHoursView' is a view, not a table.

The result of the second procedure ignores the where clause, always returning the full contents of the view (SELECT * FROM TotalHoursView).

Does Mysql not support where clauses on views when called in a stored procedure? http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.pdf

Was it helpful?

Solution

Try to use another names for your variables, it's not CASE sensitive

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top