SQL Syntax Error With Procedure with using COL_LENGTH() - mysql

I'm getting the below error when running the below. Looking at the code it looks correct to me. I'm not fully sure though.
Stored procedure creation failed: (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 'BEGIN ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '
CREATE PROCEDURE p()
BEGIN
IF COL_LENGTH('sitesettings', 'backgroundColor') IS NULL
BEGIN
ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '#202225';
END
IF COL_LENGTH('sitesettings', 'logintype') IS NULL BEGIN
ALTER TABLE sitesettings ADD logintype INT DEFAULT 1
END
IF NOT EXISTS (SELECT 'X'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'classicusers')
BEGIN
CREATE TABLE classicusers(
id TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL);
END
END;

I removed all syntax errors, si taht at least Workbench don't mind
The Syntax for IF is
IF condition THEN
-- do something
END IF
Everything else results in errors
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `p`()
BEGIN
IF COL_LENGTH('sitesettings', 'backgroundColor') IS NULL THEN
BEGIN
ALTER TABLE sitesettings ADD backgroundColor varchar(255) DEFAULT '#202225';
END;
END IF;
IF COL_LENGTH('sitesettings', 'logintype') IS NULL THEN
BEGIN
ALTER TABLE sitesettings ADD logintype INT DEFAULT 1 ;
END;
END IF;
IF NOT EXISTS (SELECT 'X'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'classicusers') THEN
BEGIN
CREATE TABLE classicusers(
id TEXT NOT NULL,
username TEXT NOT NULL,
password TEXT NOT NULL);
END;
END IF;
END$$
DELIMITER ;

Related

syntax error : 1064 , when creating a stored procedure

CREATE table parent_user
( userid int auto_increment PRIMARY KEY,
Username varchar(100) NOT NULL,
Password varchar(200) NOT NULL,
Email varchar(200) NOT NULL
);
EDIT : OK so I made some changes:
CREATE PROCEDURE `parent_reg` (
pUserName varchar(100)
pPassword varchar(200)
pEmail varchar(200)
)
as
Begin
Declare Count int
Declare ReturnCode int
Select Count = Count(Username)
from parent_user where Username = #Username
If Count > 0
Begin
Set ReturnCode = -1
End
Else
Begin
Set ReturnCode = 1
insert into parent_user values
(pUserName, pPassword, pEmail)
End
Select pReturnCode as ReturnValue
End
But I still got the same error-
Error 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 'pPassword varchar(200) pEmail varchar(200) ) ....'
The syntax error is at 'pPassword varchar(200)'
The code in the question is invalid syntax for MySQL Stored Procedure. It looks more like Microsoft SQL Server (Transact SQL) syntax.
Some observations:
MySQL procedure variables cannot start with # because that character is reserved for user-defined variables.
MySQL doesn't use a NVARCHAR type. The setting of the character_set_client variable in the session (at the time the procedure is created) is what controls the characterset of the procedure variables.
The line select * from parent_user, before the CREATE PROCEDURE looks entirely out of place.
Missing semicolons (statement terminators).
The INSERT is for a table with four columns; there are only three values and no column list.
If the goal is to create a stored procedure in MySQL, we'd need syntax closer to this:
DELIMITER $$
CREATE PROCEDURE parent_reg(p_username VARCHAR(100),
p_password VARCHAR(200), p_email VARCHAR(200)
)
BEGIN
DECLARE mycount INT;
DECLARE myreturncode INT;
SELECT COUNT(pu.username)
INTO mycount
FROM `parent_user` pu
WHERE pu.username = p_username;
IF (mycount > 0 ) THEN
SET myreturncode = -1;
ELSE
SET myreturncode = 1;
INSERT INTO `parent_user` (`username`, `password`, `email`)
VALUES (p_username, p_password, p_email);
END IF;
SELECT myreturncode AS `ReturnValue`;
END$$
DELIMITER ;
Maybe it's your database's collation. When installing SQL Server and choose your default collation, there's a "case sensitivity" checkbox. Certain collations are case sensitive and will affect your queries (and stored procedures).
A lot of vendors don't test their products on servers with case sensitive collations, which leads to runtime errors.
So just try to choose between "Username" and "UserName"

How to calculate using trigger?

CREATE TABLE table_test_trigger (
id INT(11) NOT NULL AUTO_INCREMENT,
a INT(11) DEFAULT NULL,
b INT(11) DEFAULT NULL,
c INT(11) DEFAULT NULL,
PRIMARY KEY (id)
);
DELIMITER $$
CREATE TRIGGER trigger1
BEFORE INSERT
ON table_test_trigger
FOR EACH ROW
BEGIN
SET NEW.c = NEW.a + NEW.b;
END
$$
I've tried this code.
Error:
#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 6
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `trigger1` BEFORE INSERT ON `table_test_trigger`
FOR EACH ROW BEGIN
SET NEW.c = NEW.a + NEW.b;
END;
$$
DELIMITER ;
It works fine ..there is no error in the trigger or table. Data will be inserted peacefully..!May be you have given some extra spaces in between while trying...Just check out that
Other than that there is no error it is fine...! (I tried it in my machine It worked fine..!)
Do one thing just neatly take it up into a notepad remove all extra spaces or character if any you see, then paste it into the terminal, It works fine.
The script is correct. It is possible that your MySQL client does not support delimiters. Your trigger has only one statement, so you can omit BEGIN-END clause and DELIMITER command.
CREATE TABLE table_test_trigger (
id INT(11) NOT NULL AUTO_INCREMENT,
a INT(11) DEFAULT NULL,
b INT(11) DEFAULT NULL,
c INT(11) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TRIGGER trigger1
BEFORE INSERT
ON table_test_trigger
FOR EACH ROW
SET NEW.c = NEW.a + NEW.b;

Set the value of a variable to the result from a SELECT statement in MySQL

I want to create the following function in Mysql, but the function does not get created but fails with an error
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE #AccountType varchar(50);
SELECT AccountType INTO #AccountType
FROM AccountType
WHERE AccountTypeID = AccountTypeID);
RETURN AccountType;
END $$
DELIMITER ;
A description of my table
CREATE TABLE AccountType(
AccountTypeID INT AUTO_INCREMENT PRIMARY KEY,
AccountType varchar(100) UNIQUE NOT NULL
);
The error I am getting is
ERROR 1064 (42000): 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 '#AccountType varchar(50);
I can't seem to find out what I am doing wrong. Can someone please help.
This should work:
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
SELECT AccountType INTO #AccountType FROM AccountType
WHERE AccountTypeID = AccountTypeID;
RETURN #AccountType;
END $$
DELIMITER ;
This will also work:
DELIMITER $$
CREATE FUNCTION MapAccountType(AccountTypeID INT)
RETURNS VARCHAR(50)
DETERMINISTIC
BEGIN
DECLARE v_AccountType varchar(50) DEFAULT NULL;
SELECT AccountType INTO v_AccountType FROM AccountType
WHERE AccountTypeID = AccountTypeID;
RETURN v_AccountType;
END $$
DELIMITER ;
The first example uses a "global" variable (those starting with an '#' symbol). The second example uses a "local" variable.

mysql stored procedure (with 2 insert) error

i have this table
create table eveniment( +
evenimentId bigint not null auto_increment primary key,
evenimentDenumire varchar(500),
adresaId int not null);
create table adresa(
adresaId bigint not null auto_increment primary key,
localitate varchar(500),
judet varchar(500),
codPostal varchar(50),
strada varchar(500),
nr varchar(50),
bl varchar(50),
ap varchar(5),
email varchar(500),
www varchar(500));
and this procedure
DELIMITER //
drop procedure IF EXISTS insertEveniment;
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
and i get this error
ERROR 1064 (42000): 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 'insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) values(' at line 4
if i try the insert into adresa ... separately in mysql command i don't get error , what i do wrong?
Try like this (tried and it works for me):
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0;
DECLARE vEvenimentId int DEFAULT 0;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
You are using drop procedure statement with wrong delimiter (you were setting delimiter to // but then you put ; at the end of your statement. Put your drop procedure statement before you set the delimiter, and it will work just fine. Also, put delimiters after DECLARE
DECLARE is a separate statement, thus it needs to be delimited with a ;, like INSERT or SELECT or any other statement.
Also, move the first DELIMITER statement to just before the beginning of the procedure declaration.
So,
DELIMITER //
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 ;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 ;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();

syntax error in mysql during tringger definition

I'm trying to use a trigger defined as follows
-- the table
CREATE TABLE codici_ddt(
id_ordine VARCHAR(15) NOT NULL,
id_invoice VARCHAR(15) NOT NULL,
ddt_numero INT(8) NOT NULL,
fatturazione DATE NOT NULL,
ddt VARCHAR(20) NOT NULL,
FOREIGN KEY(id_ordine) REFERENCES ordini_dal_web(id_ordine),
PRIMARY KEY(id_ordine)
);
--the_trigger
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt
FROM codici_ddt
WHERE data_fatturazione >= MAKEDATE(YEAR(NEW.data_fatturazione) ,1)
AND data_fatturazione < MAKEDATE(YEAR(NEW.data_fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1)
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;
the message returned from mysql is
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 'SET NEW.ddt =
CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.data_fatt' at
line 11
the CONCAT should be right, where is my error?
many thanks!
You miss ';' at the end of line.
SET NEW.ddt_numero = (ultimo_ddt+1);
Missing semi-colon. Also you are referencing data_fatturazione instead of just fatturazione.
Try this:
DELIMITER $$
CREATE TRIGGER genera_numero_ddt BEFORE INSERT ON codici_ddt FOR EACH ROW
BEGIN
DECLARE ultimo_ddt INT(8);
SELECT COALESCE(max(ddt_numero),1) INTO ultimo_ddt FROM codici_ddt
WHERE fatturazione >= MAKEDATE(YEAR(NEW.fatturazione) ,1)
AND fatturazione < MAKEDATE(YEAR(NEW.fatturazione)+1,1);
SET NEW.ddt_numero = (ultimo_ddt+1);
SET NEW.ddt = CONCAT(NEW.ddt_numero,'/',(SUBSTRING(SUBSTRING_INDEX(NEW.fatturazione,'-',1),-2)),'c');
END $$
DELIMITER ;