Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
Ejecute sql usando un procedimiento almacenado en mysql

Tengo el siguiente procedimiento almacenado. La idea es obtener una lista de bases de datos y ejecutar una declaración sql.

 DELIMITER $$ CREATE PROCEDURE updateMySQL ( IN theSQL varchar(4000) ) BEGIN DECLARE finished INTEGER DEFAULT 0; DECLARE theDatabases varchar(100) DEFAULT ""; -- declare cursor for employee email DEClARE curDatabase CURSOR FOR SELECT schema_name FROM information_schema.schemata where SCHEMA_NAME = 'mydb' order by 1; -- declare NOT FOUND handler DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1; OPEN curDatabase; getDatabase: LOOP FETCH curDatabase INTO theDatabases; IF finished = 1 THEN LEAVE getDatabase; END IF; -- build email list -- SET emailList = CONCAT(theDatabases,";",emailList); SET @sql:=CONCAT('USE ',@curDatabase); PREPARE dynamic_statement FROM @SQL; EXECUTE dynamic_statement; PREPARE dynamic_statement FROM @theSQL; EXECUTE dynamic_statement; END LOOP getDatabase; CLOSE curDatabase; END$$ DELIMITER ;

Estoy intentando ejecutar el procedimiento almacenado de esta manera,

 SET @theSQL = 'ALTER VIEW `Reports` AS SELECT DISTINCT `tableA`.`Id` AS `Id`, `tableA`.`letterId` AS `letterId` FROM `mytable` `tableA` ORDER BY 1'; call updateMySQL(@theSQL);

EDITAR Hubo un error al ejecutar el procedimiento, Código de error: 1064. Tiene un error en su sintaxis SQL; consulte el manual que corresponde a la versión de su servidor MySQL para obtener la sintaxis correcta para usar cerca de 'NULL' en la línea 1

estoy usando mysql 8.0.17

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Investigue cuidadosamente el Manual de referencia de MySQL 8.0 / Sentencias SQL / Sentencias preparadas , la sección "Sintaxis SQL permitida en sentencias preparadas".

Esta sección reclama la lista COMPLETA de declaraciones que están permitidas en declaraciones preparadas. ALTER VIEW NO aparece en la lista. Así que NO está permitido.

Use DROP VIEW y CREATE VIEW en su lugar.

Siempre reciba e investigue todos los mensajes de error.

over 4 years ago · Santiago Trujillo Report

0

Deberías cambiar esta parte.

 SET @sql:=CONCAT('USE ',@curDatabase); PREPARE dynamic_statement FROM @SQL; EXECUTE dynamic_statement; PREPARE dynamic_statement FROM @theSQL; EXECUTE dynamic_statement;

a esto:

 SET @sql:=CONCAT('USE ',@curDatabase); PREPARE dynamic_statement FROM @SQL; EXECUTE dynamic_statement; DEALLOCATE PREPARE dynamic_statement; /* don't forget to deallocate */ /* there's a difference between the variables @theSQL and theSQL (your parameter) */ /* IIRC prepare statements need user defined variables or a syntax error occurs. Therefore I simply assign the parameter to a user-defined variable */ SET @theSQL = theSQL; PREPARE dynamic_statement FROM @theSQL; EXECUTE dynamic_statement; DEALLOCATE PREPARE dynamic_statement;

Lea más sobre las variables definidas por el usuario aquí: https://dev.mysql.com/doc/refman/8.0/en/user-variables.html
Aquí se explican las diferencias: https://stackoverflow.com/a/1010042/447489
Cuando no los inicializa, su contenido es simplemente NULL . Dado que hay una diferencia entre las variables definidas por el usuario y las variables locales (y también su variable de parámetro), su solución actual no hizo nada.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!