I am creating a procedure called AccLikesVid inserting into a table named a_likes_v (table infomation):
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), type BOOL)
BEGIN
IF (type = 1) THEN
INSERT INTO a_likes_v VALUES (username, vidid, NOW(), 1);
ENDIF;
END$$
But when I execute the above code, MySQL Workbench generates an error: Error Code 1193. Unknown system variable 'now'
Could you tell me what's wrong with my code?
You should use:
DELIMITER $$
CREATE PROCEDURE AccLikesVid(username VARCHAR(30), vidid INT(11), v_type BOOL)
BEGIN
IF (v_type = 1) THEN -- TYPE is keyword, avoid such identifiers
INSERT INTO a_likes_v(account_name, video_id, dtime, liked)
VALUES (username, vidid, NOW(), 1); -- avoid blind insert
END IF; -- END IF not ENDIF
END$$
DBFiddle Demo
Related
I created two procedures for insert and delete respectively and would like to add them into just one procedure separated according to user's output. Any suggestions on how to do that?
create table departamento (
dnome varchar(100),
dnumero int primary key,
cpf_gerente bigint,
data_inicio_gerente date
);
Insert procedure:
delimiter //
create procedure inserir_departamento(in pr_dnome varchar(100), in pr_dnumero int, in pr_cpf_gerente bigint, in pr_data_inicio_gerente date)
begin
insert into departamento values(pr_dnome, pr_dnumero, pr_cpf_gerente,pr_data_inicio_gerente);
end //
delimiter ;
#drop procedure inserir_departamento;
call inserir_departamento('Teste', 6, 99988777767,'1978-11-11');
Delete procedure:
delimiter $$
create procedure deletar_departamento(in pr_dnumero int)
begin
delete from departamento
where dnumero = pr_dnumero;
end $$
delimiter ;
#drop procedure deletar_departamento;
call deletar_departamento(6);
I tried doing this to "mix" both procedures, but without success (I just receive a syntax error message):
delimiter $$
create procedure modificar_departamento(in insert enum("i"), in delete enum("d"), in pr_dnome varchar(100), in pr_dnumero int, in pr_cpf_gerente bigint, in pr_data_inicio_gerente date)
begin
if insert == "i" then
insert into departamento values(pr_dnome, pr_dnumero, pr_cpf_gerente,pr_data_inicio_gerente);
else if delete == "d" then
delete from departamento where dnumero = pr_dnumero;
end if;
end $$
delimiter ;
Any idea on how to do that?
= not ==. elseif not else if. Don't use reserved words as variable or parameter names, or if you do put them in backticks.
fiddle
I am trying to create this stored procedure in MySQL. However, MySQL does not accept this procedure and displays that there is a syntax error. Can someone explain what I am doing wrong? By the way, I am new to writing stored procedures.
DELIMITER $$
CREATE definer=`root`#`localhost` PROCEDURE 'login_insert'
( IN uname VARCHAR(20),
IN paword VARCHAR(20),
OUT result INT)
BEGIN
INSERT INTO db1.login_tab
( username,
pword,
)
VALUES
(
uname,
paword,
)
IF ##ERROR = 0
SET #result = 0
ELSE SET #result = 5
RETURN #result
END$$
DELIMITER ;
Changing it to the following as per below suggestion is not helping either. I have removed the return statement completely
DELIMITER $$
CREATE definer=`root`#`localhost` PROCEDURE 'login_insert'
( IN uname VARCHAR(20),
IN paword VARCHAR(20),
OUT result INT)
BEGIN
INSERT INTO db1.login_tab
( username,
pword
)
VALUES
(
uname,
paword
)
END$$
DELIMITER ;
I am getting this error
Error Code: 1415. Not allowed to return a result set from a trigger
when I am trying to execute this code below:
DELIMITER //
DROP TRIGGER IF EXISTS after_payment_insert//
CREATE TRIGGER after_payment_insert
AFTER INSERT
ON Payment FOR EACH ROW
BEGIN
SET #p = (Select ProductCode from Product Where ProductName = New.ItemName Limit 1);
Call insertInvoice(New.Email,New.InvoiceNumber, #p, New.ItemName, '1', New.Total, New.Status);
END//
DELIMITER ;
Also tried this one:
DECLARE var_productcode varchar(10);
Select ProductCode into var_productcode from Product Where ProductName = New.ItemName Limit 1;
Call insertInvoice(New.Email,New.InvoiceNumber, var_productcode, New.ItemName, '1', New.Total, New.Status);
Both of the code above result to error code 1415.
The insertInvoice is just a simple stored procudure for inserting data from the invoice table.
Below is the InsertInvoice proc
DELIMITER //
DROP PROCEDURE IF EXISTS insertInvoice//
CREATE PROCEDURE insertInvoice(
IN insert_Email VARCHAR(255),
IN insert_InvoiceNumber VARCHAR(10),
IN insert_ProductCode VARCHAR(50),
IN insert_ItemName VARCHAR(10),
IN insert_Quantity VARCHAR(10),
IN insert_Price VARCHAR(10),
IN insert_Status VARCHAR(50))
BEGIN
INSERT INTO Invoice(Email, InvoiceNumber, ProductCode, ItemName, Quantity, Price, Status)
VALUES(insert_Email, insert_InvoiceNumber,insert_ProductCode, insert_ItemName, insert_Quantity, insert_Price, insert_Status);
END
//DELIMITER ;
But when I use the normal Insert Into (....) Values(...) it just works.
Hi im trying to insert using this sp with insert statment of:
call insertuser (1, '077788899965', 'Digest 1.0', ':=', 'asjdfhiuoadshgiadufg');
SP CODE:
DELIMITER$$
CREATE PROCEDURE insertuser (IN IdParam INT, IN UserNameParam varchar(64), IN AttributeParam varchar(64), IN OpParam char(2), IN ValueParam varchar(253))
BEGIN
// Delete user if they already exist
DELETE FROM radcheck
WHERE username = UserNameParam;
// Insert
INSERT INTO radcheck (id, username, atrribute, op, value)
SELECT (IdParam, UserNameParam, AttributeParam, OpParam, ValueParam);
END$$
But I am getting error of:
ERROR 1241 (21000): Operand should contain 1 column(s)
Any idea how to resolve this?
DELIMITER $$
CREATE PROCEDURE insertuser (IN IdParam INT, IN UserNameParam varchar(64),
IN AttributeParam varchar(64), IN OpParam char(2),
IN ValueParam varchar(253))
BEGIN
DELETE FROM radcheck
WHERE username = UserNameParam;
INSERT INTO radcheck (id, username, atrribute, op, `value`)
SELECT IdParam, UserNameParam, AttributeParam, OpParam, ValueParam;
END
$$
There were 2 problems:
missing space after DELIMITER
// is not a comment start in MySQL. Use /* */
If you use a SQL IDE like MySQL Workbench such errors will be highlighted and are easier to fix.
So I have this stored procedure, that I have tried to write a few different way, all to no avail.
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
((CALL GetUserId(USER_EMAIL)), NAME, GENDER);
END
I am getting this 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 'CALL GetUserId(USER_EMAIL)), NAME, GENDER)' at line 6
I have tried tinkering with for a while.
GetUserId works. I tried to store the result in a temporary variable and then insert it but that did not work.
Not to be shameless but If you can determine a solution, a solution where the CALL GetUserId is stored in a variable would be best.
You can't use it like this. Rewrite your GetUserId procedure with an OUT parameter.
Something like this:
DELIMITER $$
CREATE PROCEDURE GetUserId(IN p_email varchar(20), OUT p_id int)
BEGIN
SELECT id INTO p_id FROM users where email = p_email;
/*or whatever your procedure does*/
END$$
DELIMITER ;
Then your procedure CreateHero would look like this:
DELIMITER $$
CREATE PROCEDURE `CreateHero`
(IN USER_EMAIL VARCHAR(40), IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
DECLARE v_id int;
CALL GetUserId(USER_EMAIL, v_id);
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES
(v_id, NAME, GENDER);
END$$
DELIMITER ;
Old thread but could help some one looking for later...
Base on fancyPants answer but more beautiful like this:
DELIMITER $$
CREATE FUNCTION GetUserId(IN p_email varchar(20)) RETURNS int
BEGIN
RETURN(SELECT id FROM users where email = p_email);
/*or whatever your function does*/
END$$
DELIMITER ;
And then:
DELIMITER $$
CREATE PROCEDURE `CreateHero` (IN USER_EMAIL VARCHAR(40),
IN NAME VARCHAR(16), IN GENDER VARCHAR(6))
BEGIN
INSERT INTO HEROES (USER_ID, NAME, GENDER)
VALUES (GetUserId(USER_EMAIL), NAME, GENDER);
END$$
DELIMITER ;