I have written a stored procedure which enrols a student in a table. But before doing so, it checks weather the student is already present, if it does then no insertion takes place.
DELIMITER //
CREATE PROCEDURE test(IN sid varchar(6), IN us varchar(20), IN pswd varchar(20))
BEGIN
declare temp varchar(20);
declare x int;
declare y int;
set temp=(select username from login_student where s_id=sid);
IF(temp == NULL)
THEN insert into login_student values(sid, us, pswd);
ELSEIF (temp != NULL)
THEN set x=x+1; //have written it randomly
ELSE
set y=y+1; //have written it randomly
END IF;
END //
DELIMITER ;
login_student table schema is defined as:
CREATE TABLE login_student
(s_id varchar(6),
username varchar(20),
password varchar(20)
);
It's giving me some syntax error, can any one mark out the mistake please?
Two problems I see:
You need to use IS NULL and IS NOT NULL instead of == NULL and != NULL
You are using the wrong syntax for MySQL comments. Use -- not //
It should also be noted that you will never reach the ELSE in this case, since temp is either NULL or NOT NULL, so you could simplify it to an IF/ELSE if you want.
And I would recommend using SELECT ... INTO instead of SET to set the temp value.
Putting it all together:
DELIMITER //
DROP PROCEDURE IF EXISTS test //
CREATE PROCEDURE test(IN sid varchar(6), IN us varchar(20), IN pswd varchar(20))
BEGIN
declare temp varchar(20);
declare x int;
declare y int;
select username into temp from login_student where s_id=sid;
IF(temp IS NULL)
THEN insert into login_student values(sid, us, pswd);
ELSEIF (temp IS NOT NULL)
THEN set x=x+1; -- have written it randomly
ELSE
set y=y+1; -- have written it randomly
END IF;
END //
DELIMITER ;
Would be nice to get the text of an error, but I think it should be:
IF(temp IS NULL)
THEN insert into login_student values(sid, us, pswd);
ELSEIF (temp IS NOT NULL)
THEN set x=x+1; //have written it randomly
ELSE
set y=y+1; //have written it randomly
END IF;
Related
I have a mysql procedure that it doesn´t work an IF condition with AND. I'm not sure what is the correct syntax.
CREATE PROCEDURE insere_orcamento(
IN ida VARCHAR(10),
IN volta VARCHAR(10),
OUT result INT
)
BEGIN
IF (ida='sim' AND volta=NULL) THEN
SET result = 50;
ELSEIF (ida=NULL AND volta='sim') THEN
SET result = 51;
END IF;
END
The value NULL must be checked with IS
Or like akina said you can also use <=>
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `insere_orcamento`(
IN ida VARCHAR(10),
IN volta VARCHAR(10),
OUT result INT
)
BEGIN
IF (ida='sim' AND volta IS NULL) THEN
SET result := 50;
ELSEIF (ida IS NULL AND volta = 'sim') THEN
SET result := 51;
END IF;
END$$
DELIMITER ;
Please help me understand,
I need to make stored procedure for creating new user into db.
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
DECLARE newname VARCHAR(25);
DECLARE xid INT;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;
when i call add_user('peter');
it shows me:
newname/null
where do i go wrong ?
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE x VARCHAR(25) DEFAULT 'mark';
This actually creates two variables named x. The first is the function parameter, and the second is a local variable which is in scope inside the BEGIN ... END block. According to MySQL's scoping rules, only one of these variables can be visible, which means that the parameter x is hidden and inaccessible inside the BEGIN ... END block.
The solution is simply to delete the line
DECLARE x VARCHAR(25) DEFAULT 'mark';
and, if you need to assign a default value, do it in an IF block:
IF x IS NULL THEN
SET x = 'mark';
END IF;
The complete function definition should be
delimiter //
CREATE PROCEDURE add_user (x VARCHAR(25))
BEGIN
DECLARE newname VARCHAR(25);
DECLARE xid INT;
IF x IS NULL THEN
SET x = 'mark';
END IF;
SELECT x, id INTO newname, xid
FROM users WHERE x = x;
SELECT newname;
END;
delimiter ;
Let's see if I can edit this and put the whole procedure in.
I am trying to convert an Oracle database to MySQL. I have all the tables, keys, indexes, and views converted. I now need to convert a stored procedure to MySQL.
I have most of it done, and there is only one hang up on my code:
set dns1_tmp = X.X.X.X;
SET dns2_tmp = X.X.X.X;
This gives me an error of 1064 Syntax Error: Missing semicolon
I have tested the rest of my procedure, and it works fine. It creates it, runs it, and retrieves data from it, but only if I remove those two lines.
Any ideas on what I can do?
Whole stored procedure:
DELIMITER //
USE `TEST`//
DROP PROCEDURE IF EXISTS `proc_IN`//
CREATE DEFINER=`root`#`localhost` PROCEDURE `proc_IN`
(IN DNIS VARCHAR(20),
IN MSISDN VARCHAR(20),
IN AVPAIR1 VARCHAR(20),
IN AVPAIR2 VARCHAR(20),
IN GROUPID VARCHAR(20),
OUT DNS1 VARCHAR(15),
OUT DNS2 VARCHAR(15),
OUT AUTHSTAT VARCHAR(100))
BEGIN
declare dns1_tmp varchar(15);
declare dns2_tmp varchar(15);
set dns1_tmp = X.X.X.X;
SET dns2_tmp = X.X.X.X;
DECLARE avpair1_tmp varchar(15);
DECLARE avpair2_tmp varchar(15);
DECLARE grpid_tmp varchar(15);
DECLARE C_USER CURSOR FOR SELECT AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID FROM GRP, ALLMEMBER WHERE ALLMEMBER.GROUPID=GRP.GROUPID
UNION
SELECT AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID FROM GRP;
OPEN C_USER;
FETCH C_USER INTO AVPAIR1, AVPAIR2, DNS1, DNS2, GROUPID;
LOOP
FETCH C_USER INTO avpair1_tmp, avpair2_tmp, dns1_tmp, dns2_tmp, grpid_tmp;
INSERT INTO duplog VALUES(DNIS, MSISDN, avpair1_tmp, avpair2_tmp, dns1_tmp,dns2_tmp, grpid_tmp, SYSDATE);
END LOOP;
IF C_USER%ROWCOUNT > 1 THEN
INSERT INTO duplog VALUES(DNIS, MSISDN, AVPAIR1, AVPAIR2, DNS1,DNS2, GROUPID, SYSDATE);
SET AUTHSTAT := 'ok';
elseif C_USER%ROWCOUNT = 1 THEN
SET AUTHSTAT := 'ok';
ELSE
SET AUTHSTAT := NULL;
END IF;
CLOSE C_USER;
COMMIT;
END //
DELIMITER ;
I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'
I have a little problem. Looks like the procedure does not exist. Somehow it's dropped after the creation. I get different error each time i change something. I'm not really sure what's causing the error, maybe I'm not allowed to drop procedures and creating them in the same query.
I hope you guys can help me out.
drop procedure if exists refIntChk;
DELIMITER //
CREATE PROCEDURE refIntChk(IN district INT(11), OUT b INT(1))
BEGIN
DECLARE b INT(1);
IF district IN (select dist FROM t13)
THEN
SET b = 1;
ELSE
SET b = 0;
END IF;
END; //
DELIMITER ;
drop procedure gen if exists ;
DELIMITER //
CREATE PROCEDURE gen()
BEGIN
DECLARE rows INT(11) DEFAULT (SELECT COUNT(dist) FROM t13);
DECLARE district INT(11);
DECLARE custname VARCHAR(16);
DECLARE revenue FLOAT;
DECLARE x INT DEFAULT 10000;
DECLARE outvar INT(11);
WHILE x > 0
DO
SET district = FLOOR(RAND()*rows)+1;
CALL refIntChk(district, outvar);
IF outvar = 1
THEN
SET custname = substring(MD5(RAND()), -16);
SET revenue = (RAND() * 10);
INSERT INTO t14 VALUES(NULL, custname, district, revenue);
SET x = x - 1;
END IF;
END WHILE;
END;//
DELIMITER ;
CALL gen();
When you get errors, it's usually good to run each statement, one by one, and see which one is producing the error.
The second DROP procedure statement should be:
drop procedure if exists gen;