Syntax problem of bacth sql execution in procedure - mysql

Failed to run followwing code in mysql 8.0.
drop table if exists test ;
create table if not exists test(rowid int);
delimiter$$
drop procedure if exists line_sum $$
create procedure line_sum()
begin
declare i int ;
declare exe_sql varchar(100);
set i=5 ;
while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int') ;
prepare ppsql from exe_sql ;
execute ppsql ;
deallocate prepare ppsql ;
set i = i-1;
end while ;
end$$
delimiter ;
It reported:
You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near 'while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int'
at line 11
However my colleague and I have checked it many times, finding no issues in the syntax and getting more confusion.

Related

can't create a table in mysql with dynamic sql

This is my code in MySQL.
USE database;
DROP procedure IF EXISTS CreateTable;
DELIMITER $$
USE ims_data$$
CREATE PROCEDURE CreateTable ()
BEGIN
Set #SqlQuery = Concat('DROP TABLE IF EXISTS mytemptable;');
Set #SqlQuery = Concat(#SqlQuery,'\r\n','create table mytemptable');
Set #SqlQuery = Concat(#SqlQuery,'\r\n','(');
Set #SqlQuery = Concat(#SqlQuery,'\r\n','Column1 int,');
Set #SqlQuery = Concat(#SqlQuery,'\r\n','Column2 varchar(500)');
Set #SqlQuery = Concat(#SqlQuery,'\r\n',');');
Set #SqlQuery = Concat(#SqlQuery,'\r\n','Select * from mytemptable;');
#Select #SqlQuery;
PREPARE Statement From #SqlQuery;
EXECUTE Statement;
DEALLOCATE PREPARE Statement;
END$$
DELIMITER ;
call GetUploadInformation();
I am trying to create a table but it is giving me an error.
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table mytemptable (Stockist_Code int,Status varchar(500) ); Sele' at line 2
This is the output of query.
DROP TABLE IF EXISTS mytemptable;
create table mytemptable
(
Column1 int,
Column2 varchar(500)
);
Select * from mytemptable;
Which is working fine when executing this code withoug calling the procedure.
PREPARE/EXECUTE can only process one statement at a time. You're trying to execute two with the ;.
The error message gives you a clue in that it ran the two statements together.
You'll have to run them as separate statements.

Mysql Procedure Syntax error on Update

DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;

Stored procedure if mysql

create procedure update_keyoffice(out p_key varchar(30),out p_hostname varchar(30))
begin
declare p_keyoff varchar(30);
select keyoff into p_keyoff from masterpc where keyoffice = p_key;
if(p_keyoff = p_key) then
insert into keyofficehistory (id,keyoff,hostname,datecreate) values
(null,p_key,p_hostname,now())
end if
end
error
SQL query:
CREATE PROCEDURE update_keyoffice( out p_key varchar( 30 ) , out
p_hostname varchar( 30 ) ) BEGIN DECLARE p_keyoff varchar( 30 ) ;
MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near '' at line 3
While defining stored procedures and triggers, you have to use custom delimiter which, it seems, is missing in your code.
Change your code as below:
delimiter //
-- your stored procedure code here
-- but don't forget to use new delimiter after end statement
end; //
-- now reset delimiter
delimiter ;
Refer to:
CREATE PROCEDURE and CREATE FUNCTION Syntax

EXECUTE in MySQL Stored Procedure gives syntax error

I'm new to MySQL and databases in general. I'm trying to create a MySQL stored procedure but keep getting a vague syntax error:
"you have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'sps; EXECUTE stmt USING nn,ee,ll,cc,uu; COMMIT END' at line 11"
If I remove the line "PREPARE stmt ..." then the stored procedure is created. When I put the line back in, I get the error again.
What is it that I am doing wrong?
DELIMITER //
CREATE PROCEDURE `account_create` (nn VARCHAR(25),
ee BIGINT,
ll BIGINT,
cc VARCHAR(100),
uu VARCHAR(25))
BEGIN
DECLARE newId BIGINT;
DECLARE sps VARCHAR(50);
START TRANSACTION;
set sps = 'INSERT INTO account SET name=?, entity=?, ledger=?, tblname=tmpXXX, creation_date=CURDATE(), comment=?, uname=?';
PREPARE stmt FROM sps;
COMMIT;
END//
You must use a User Defined Variable to execute a prepared statement. Rewrite as:
...
BEGIN
DECLARE newId BIGINT;
START TRANSACTION;
set #sps = 'INSERT INTO account SET name=?, entity=?, ledger=?, tblname=tmpXXX, creation_date=CURDATE(), comment=?, uname=?';
PREPARE stmt FROM #sps;
COMMIT;
END//

Stored procedure error on CALL

I am trying to call a procedure which compiles successfully but on calling I get this error:
Query: call proc5
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1
This is my Stored procedure:
DELIMITER $$
CREATE DEFINER = `root` #`localhost` PROCEDURE `proc5` ()
BEGIN
DECLARE done BOOL DEFAULT FALSE ;
DECLARE tablename VARCHAR (100) ;
DECLARE tracktables CURSOR FOR
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE TABLE_SCHEMA = 'db1' ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE ;
OPEN tracktables ;
myloop :
LOOP
FETCH tracktables INTO tablename ;
IF done
THEN CLOSE tracktables ;
LEAVE myloop ;
END IF ;
SET #s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM ',
#tablename
) ;
PREPARE stmt1 FROM #s ;
EXECUTE stmt1 ;
DEALLOCATE PREPARE stmt1 ;
END LOOP ;
END $$
DELIMITER ;
Actually, I want to select all the tables from a database and insert those tables into one table which is in another database using MySQL Cursors. And when I call this stored procedure I get the above error.
The problem is that you are mixing declared variables and impromtu #vars.
var -> tablename does not equal var -> #tablename.
Change the set line to:
SET #s = CONCAT(
'INSERT INTO db2.test1 SELECT * FROM `'
,tablename
,'`'
) ;
Now it should work.
The backticks ` should not be needed, but are there just in case.