Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL error in a procedure #1351 - View's SELECT contains a variable or parameter

PhpMyAdmin doesn't accept the parameters "id_jugador_IN" inside the procedure.

This is the procedure we're executing:

SET GLOBAL event_scheduler = 1;

use traviandatabase;

DELIMITER $$

DROP PROCEDURE IF EXISTS sp_monitoritzacio_jugador $$

CREATE PROCEDURE sp_monitoritzacio_jugador(IN id_jugador_IN INT(10))

BEGIN

    CREATE OR REPLACE 
    VIEW dades_ususari AS 
    SELECT j.nom AS jugador, j.sexe AS sexe, j.edat AS edat, j.correu AS correu, a.nom AS alianca, p.nom AS pais, c.nom_ciutat AS ciutat
    FROM jugador AS j
    JOIN alianca AS a
    JOIN pais AS p
    JOIN ciutat AS c
    ON j.id_jugador = id_jugador_IN
    GROUP BY id_jugador_IN;

END $$

DELIMITER ;

CALL sp_monitoritzacio_jugador(1);

And this is the table "jugador":

DROP TABLE IF EXISTS `jugador`;
CREATE TABLE `jugador` (
  `id_jugador` int(10) NOT NULL AUTO_INCREMENT,
  `id_raca` int(10) NOT NULL,
  `id_pais` int(10) NOT NULL,
  `id_alianca` int(10) DEFAULT '0',
  `nom` varchar(20) NOT NULL,
  `sexe` enum('Home','Dona') NOT NULL,
  `edat` int(10) NOT NULL,
  `correu` varchar(20) NOT NULL,
  PRIMARY KEY (`id_jugador`),
  KEY `jugador-alianca` (`id_alianca`),
  KEY `id_pais` (`id_pais`),
  KEY `id_raca` (`id_raca`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

So it doesn't stop displaying the same error:

MySQL said: 1351 - View's SELECT contains a variable or parameter

like image 386
Roger Fernandez Guri Avatar asked Jul 02 '12 10:07

Roger Fernandez Guri


People also ask

How do I return an error from a stored procedure in MySQL?

BEGIN select "error message '%s' and errorno '%d'"; -- use missed the semicolon ROLLBACK; END; So it will actually display error msg with column name as 'ROLLBACK' instead of print and rollback the transaction.

How do you handle errors in stored procedure?

Return value returns the Stored Procedure Name if an error occurs in a Stored Procedure or trigger and the catch block is called. It returns NULL if the error did not occur within a Stored Procedure or trigger or it isb called outside the scope of a CATCH block. First we create a Stored Procedure.

How MySQL errors are handled?

Declaring a handlerIf a condition whose value matches the condition_value , MySQL will execute the statement and continue or exit the current code block based on the action . The action accepts one of the following values: CONTINUE : the execution of the enclosing code block ( BEGIN … END ) continues.

How do I show MySQL errors?

The SHOW COUNT(*) ERRORS statement displays the number of errors. You can also retrieve this number from the error_count variable: SHOW COUNT(*) ERRORS; SELECT @@error_count; SHOW ERRORS and error_count apply only to errors, not warnings or notes.


1 Answers

As you see - view cannot use variables, it is a limitation. You are trying to create a view with exact WHERE condition; construct the CREATE VIEW statement text firstly, then use prepared statements to execute the statement, it will help you. But, do you really need it, to create, create and create new view?

EDIT: This is the content of the reference in the comment.

Olexandr Melnyk A simple workaround for MySQL's limitation on local variables usage in views is to use a function, which returns variable's value:

create function book_subject
returns varchar(64) as
return @book_subject;

create view thematical_books as
select title
, author
from books
where subject = book_subject();
like image 73
Devart Avatar answered Sep 20 '22 05:09

Devart