issue with create table inside if/else block - mysql

I'm trying to create a table in a MySQL stored procedure depending upon the values of some of the passed parameters. My code is as follows:
DELIMITER //
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END
I'm getting an error stating: #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 5.
I'm not sure what I've done wrong here. The syntax looks fine to me and the queries seem well-formulated.

You're missing the END IF statement.
CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
BEGIN
IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
END IF;
END

Related

Syntax error in MySQL procedure while using if else insert query

I am trying to create a stored procedure with MySQL but I kept receiving the error prompt but i cant find the error in my query.
I'm coming from MS SQL background and I'm not sure how is my query wrong. Anyone understands the 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 '' at line 11
CREATE PROCEDURE SP_AddUpdateUser
(
IN InputUsername VARCHAR(512),
IN InputPassword VARCHAR(512),
IN InputEmail VARCHAR(512)
)
BEGIN
if(NOT EXISTS(SELECT 1 FROM TB_User WHERE Username = InputUsername)) then
INSERT INTO TB_User(Username, Password, Email, DateCreated)
SELECT InputUsername, InputPassword, InputEmail, Now();
else
UPDATE TB_User SET Password = InputPassword, Email = InputEmail WHERE Username = InputUsername;
end if;
END;;
You are taking the wrong approach in MySQL. It has a single statement that supports "upserts" -- and insert with on duplicate key update.
This is triggered when you have a unique index or constraint on the column that cannot be duplicated. In this case, username.
Start with a unique constraint or index:
create unique index unq_tb_user_username on TB_User(username);
You may already have this. Then, the body of the procedure would simply be:
CREATE PROCEDURE SP_AddUpdateUser
(
IN InputUsername VARCHAR(512),
IN InputPassword VARCHAR(512),
IN InputEmail VARCHAR(512)
)
BEGIN
INSERT INTO TB_User(Username, Password, Email, DateCreated)
SELECT InputUsername, InputPassword, InputEmail, Now()
ON DUPLICATE KEY UPDATE Password = InputPassword, Email = InputEmail;
END;
Here is a db<>fiddle.
Put ; after now(). In stored scripts you have to use semicolon after each statement.

Problem with the procedure for changing data in a specific table

I'm just starting my adventure in mysql. At the time of creating the procedure for changing the quantity in the table. I get this error
#1064 - Something is wrong in your syntax obok '' in line 5
And this is my procedure
Create procedure towar_add (IDStatusu Int, tIlosc Int)
begin
Update towar
set Ilosc = Ilosc + tIlosc
where ID = IDStatusu;
END
I've always created everything in SQL and MySQL is new to me

Running the following query from my PhpMyadmin SQL tab

I'm running the following query from my PhpMyadmin SQL tab:
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id
END
But everytime I'm getting this error msg:
#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 'FROM addbook
WHERE addbook.book_id = inserted.book_id
END' at line 10
Use delimiter
DELIMITER //
CREATE TRIGGER trg_bansach ON bill AFTER INSERT AS
BEGIN
UPDATE addbook
SET Quality = Quality - (
SELECT QualitySale
FROM inserted
WHERE book_id = addbook.book_id
)
FROM addbook
JOIN inserted ON addbook.book_id = inserted.book_id;
END
//
DELIMITER ;

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

Get the Unique ID for the Last Inserted Row

I want to get the Unique ID for the Last Inserted Row inside stored procedure, I make like this
DELIMITER //
CREATE PROCEDURE addNewUsers(IN userName varchar(128),IN password varchar(128), IN addedBy INT)
BEGIN
DECLARE id int default 0;
id = mysqli_insert_id (insert into `system_users`( `username`,`password`) values (userName ,md5(password)) );
IF id <> 0 THEN
insert into `user_profile`( `full_name`,`Date_time_ added`,`added_by`) values (userName ,CURRENT_TIMESTAMP(),addedBy ) where `user_id`=id ;
END IF
END //
DELIMITER ;
This error occur
#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 '= mysqli_insert_id (insert into `system_users`( `username`,`password`) values (' at line 7
I doubt it's from mysqli_insert_id , what should I do ?
Your mysqli_insert_id is the problem, you're writing a MySQL stored procedure, not PHP. You want to use the last_insert_id() function:
LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement.
You also need to fix your assignment syntax. Something more like this:
DELIMITER //
CREATE PROCEDURE addNewUsers(IN userName varchar(128),IN password varchar(128), IN addedBy INT)
BEGIN
DECLARE id int default 0;
insert into `system_users`( `username`,`password`) values (userName ,md5(password));
set id = last_insert_id();
if id <> 0 then
-- ...