I have a table books
.
+-------+-----------+------------+
| serial| book_name | book_author|
+-------+-----------+------------+
| 1 | Star Wars | 1 |
| 2 | Java | 2 |
| 3 | Alimals | 3 |
| 4 | Star Wars | 1 |
+-------+-----------+------------+
I want to use stored procedure to SELECT book_name
;
My stored procedure looks like this:
USE books;
DELIMITER //
CREATE PROCEDURE doselect(col VARCHAR(50), tab VARCHAR(50), cond INT(5))
BEGIN
SET @x = NULL;
SET @col = col;
SET @tab = tab;
SET @cond = cond;
SET @x = (SELECT @col FROM @tab WHERE b_id = @cond);
END //
DELIMITER ;
Then I call it:
CALL doselect('book_name', 'books', 3)
But id doesn't work. It doesn't see the variable @tab
. The mistake is:
Table books.@tab doesn't exists.
I also used this variant of SELECT condition:
SET @x = (SELECT 'col' FROM 'tab' WHERE b_id = 'cond');
The same mistake but now without @
:
Table books.tab doesn't exists.
How to pass variables right way in such a case? How to make it see my variables?
Thank you.