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"
Related
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 ;
I use MySQL to manage a database.
I have 2 tables named IDENTITY and OPTIONS in a database named WIFI.
IDENTITY contains 2 fields: USERNAME and PASSWORD.
OPTIONS contains 3 fields: USERNAME, WIFI_SSID and WIFI_PASSWORD.
And there is a procedure accepting these variables as argument:
arg01_USERNAME
arg02_PASSWORD
arg03_WIFI_SSID
arg04_WIFI_PASSWORD
I want to build a procedure in phpMyAdmin to do this operation:
This procedure should check if the arg01_USERNAME and arg02_PASSWORD match the data in IDENTITY. If data not found, then do nothing. Else search for arg01_USERNAME in table OPTIONS and update WIFI_SSID and WIFI_PASSWORD with arg03_WIFI_SSID and arg04_WIFI_PASSWORD. If arg01_USERNAME not found in OPTIONS, then insert a new record into OPTIONS.
Here is the SQL query to define procedure:
CREATE DEFINER=`WIFI`#`localhost` PROCEDURE `INSERT`(IN `arg01_USERNAME` INT(20) UNSIGNED, IN `arg02_PASSWORD` VARCHAR(32), IN `arg03_WIFI_SSID` VARCHAR(32) CHARSET utf8, IN `arg04_WIFI_PASSWORD` VARCHAR(32) CHARSET utf8)
NO SQL
IF EXISTS (
SELECT COUNT(*)
FROM `WIFI`.`IDENTITY`
WHERE `USERNAME` = arg01_USERNAME AND `PASSWORD` = arg02_PASSWORD)
THEN
INSERT INTO `WIFI`.`OPTIONS` (
`USERNAME`,
`WIFI_SSID`,
`WIFI_PASSWORD`
) VALUES (
arg01_USERNAME,
arg03_WIFI_SSID,
arg04_WIFI_PASSWORD
)
ON DUPLICATE KEY UPDATE
`WIFI_SSID` = arg03_WIFI_SSID,
`WIFI_PASSWORD` = arg04_WIFI_PASSWORD
END IF;
ENGINE=InnoDB DEFAULT CHARSET=utf8
Could you please point me out what is wrong with this code?
When I try to define the procedure using this code, phpMyAdmin tells me there is a syntax error near "END IF" part of the code(error 1064).
Sorry for my poor English.
You need to redefined Delimiter to something else, for eg: $$, so that parser does not trigger query execution when it sees ;
At the end, redefine the Delimiter back to ;.
Try:
DELIMITER $$
CREATE DEFINER=`WIFI`#`localhost` PROCEDURE `INSERT`(IN `arg01_USERNAME` INT(20) UNSIGNED, IN `arg02_PASSWORD` VARCHAR(32), IN `arg03_WIFI_SSID` VARCHAR(32) CHARSET utf8, IN `arg04_WIFI_PASSWORD` VARCHAR(32) CHARSET utf8)
NO SQL
IF EXISTS (
SELECT COUNT(*)
FROM `WIFI`.`IDENTITY`
WHERE `USERNAME` = arg01_USERNAME AND `PASSWORD` = arg02_PASSWORD)
THEN
INSERT INTO `WIFI`.`OPTIONS` (
`USERNAME`,
`WIFI_SSID`,
`WIFI_PASSWORD`
) VALUES (
arg01_USERNAME,
arg03_WIFI_SSID,
arg04_WIFI_PASSWORD
)
ON DUPLICATE KEY UPDATE
`WIFI_SSID` = arg03_WIFI_SSID,
`WIFI_PASSWORD` = arg04_WIFI_PASSWORD;
END IF $$
DELIMITER ;
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.
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 ;
I'm tumbled with a problem!
I've set up my first check constraint using MySQL, but unfortunately I'm having a problem. When inserting a row that should fail the test, the row is inserted anyway.
The structure:
CREATE TABLE user (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
uname VARCHAR(10) NOT NULL,
fname VARCHAR(50) NOT NULL,
lname VARCHAR(50) NOT NULL,
mail VARCHAR(50) NOT NULL,
PRIMARY KEY (id),
CHECK (LENGTH(fname) > 30)
);
The insert statement:
INSERT INTO user VALUES (null, 'user', 'Fname', 'Lname', 'mail#me.now');
The length of the string in the fname column should be too short, but it's inserted anyway.
I'm pretty sure I'm missing something basic here.
MySQL doesn't enforce CHECK constraints, on any engine.
Which leads me to ask, why would you declare the fname column as VARCHAR(50), but want to enforce that it can only be 30 characters long?
That said, the only alternative is to use a trigger:
CREATE TRIGGER t1 BEFORE INSERT ON user
FOR EACH ROW BEGIN
DECLARE numLength INT;
SET numLength = (SELECT LENGTH(NEW.fname));
IF (numLength > 30) THEN
SET NEW.col = 1/0;
END IF;
END;
As mentioned above you have to use a trigger, MySQL doesn't support check, also when you have multiple statements inside your trigger block, like declaring variables or control flows, you need to start it with begin and end and enclose your trigger inside two delimiters:
Note: If you use MariaDB use // after the first delimiter and before the second delimiter, otherwise if you use MySQL use $$ instead.
delimiter //
create trigger `user_insert_trigger` before insert on `user` for each row
begin
declare maximumFnameLength int unsigned;
declare fNameLength int unsigned;
set maximumFnameLength = 30;
set fNameLength = (select length(new.fNameLength));
if (fNameLength > maximumFnameLength) then
signal sqlstate '45000'
set message_text = 'First name is more than 30 characters long.';
end if;
end
//
delimiter ;