I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;
Related
I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;
Hi i am creating some trigger for update the value in insert query before insert the row like this
CREATE TRIGGER `Insert members in Posts` BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END
But i received the error like
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 4
I am little confused what's the error in line no 4. Please explain someone. Thanks.
Depending on the client/ide used you may need to set delimiter for example
drop trigger if exists t;
delimiter $$
CREATE TRIGGER t BEFORE INSERT ON
`posts` FOR EACH ROW
BEGIN
DECLARE name_ varchar(100);
DECLARE contact_ varchar(16);
DECLARE status_ int;
DECLARE deleted_ int;
SELECT
`members`.`name`,
`members`.`contact`,
`members`.`status`,
`members`.`deleted`
INTO name_, contact_, status_, deleted_
FROM
`members`
WHERE
`members`.`member_id` = NEW.member_id;
SET NEW.member_name = name_;
SET NEW.member_contact = contact_;
SET NEW.member_status = status_;
SET NEW.member_deleted = deleted;
END $$
delimiter ;
please review https://dev.mysql.com/doc/refman/8.0/en/stored-routines.html
I am trying to write the following stored procedure but I keep getting a syntax error, which I've included under the SP.
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `test`(categoryID int, userID int)
BEGIN
DECLARE vbUserId INT DEFAULT ( SELECT userfield.field6 FROM userfield WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT 0;
CREATE TEMPORARY TABLE tmp engine=memory AS (select forumid,replace(replace(title_clean,'&','&'),'"','') as forumName,replycount as NumOfPosts, threadcount as NumOfThreads, 0 as hasChildren, showprivate as isprivate from forum where parentid=categoryID and displayorder!=0 and options&1=1 order by displayorder);
DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = 1;
OPEN cur_forums;
iterate_forums: LOOP
FETCH cur_forums INTO m_forumId, m_forumName, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate;
IF lastRow = 1 THEN LEAVE iterate_forums;
IF (m_isPrivate = 1)
SELECT CONCAT('Private: ', m_isPrivate);
END IF
END LOOP iterate_forums;
CLOSE cur_forums;
DROP TEMPORARY TABLE IF EXISTS tmp;
END$$
Here is the error I receive when I try to import this into a db:
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR N' at line 10
I've read the documentation and looked over previous SO questions, and also tried commenting out certain lines or running them on their own, but still can't figure out what I'm doing wrong.
Chuck tmp and the cursor. Instead, have simply
select "Private: 1"
FROM forum
WHERE EXISTS ( SELECT *
from forum
where parentid=categoryID
and displayorder!=0
and options&1=1
AND showprivate = 1 );
vbUserId seems to be unused; get rid of it.
change the order like: 1 declare, then open
....
DECLARE vbUserId INT DEFAULT ( SELECT userfield.field6 FROM userfield WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads, m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT 0;
DECLARE cur_forums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = 1;
OPEN cur_forums;
CREATE TEMPORARY TABLE tmp engine=memory AS (select forumid,replace(replace(title_clean,'&','&'),'"','') as .....
...
Fixed it by re-writing as so:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `test`(categoryID int, userID int)
BEGIN
DECLARE vbUserId INT DEFAULT (
SELECT userfield.field6 FROM userfield
WHERE userfield.field6 = userID );
DECLARE m_forumId, m_numOfPosts, m_numOfThreads,
m_hasChildren, m_isPrivate INT;
DECLARE m_forumName VARCHAR(100);
DECLARE lastRow INT DEFAULT FALSE;
DECLARE curForums CURSOR FOR select * from tmp;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET lastRow = TRUE;
CREATE TEMPORARY TABLE tmp engine=memory AS (
select forumid,
replace(replace(title_clean,'&','&'),'"','')
as forumName,
replycount as NumOfPosts,
threadcount as NumOfThreads,
0 as hasChildren,
showprivate as isprivate
from forum
where parentid=categoryID
and displayorder!=0
and options&1=1
order by displayorder);
OPEN curForums;
iterateForums: LOOP
FETCH curForums INTO m_forumId, m_forumName,
m_numOfPosts, m_numOfThreads,
m_hasChildren, m_isPrivate;
IF lastRow THEN
LEAVE iterateForums;
END IF;
IF m_isPrivate = 1 THEN
SELECT (m_isPrivate);
END IF;
END LOOP;
CLOSE curForums;
DROP TEMPORARY TABLE IF EXISTS tmp;
END$$
DELIMITER ;
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
FETCH curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
CLOSE curs1;
end ;
;;
delimiter ;
above throws an error : SQL 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 '' at line 14
create table test11(abc varchar(20), cde varchar(20), eg varchar(20), asda varchar(20));
insert into test11 values("Hello",'1989-08-09','sdads','22');
create table test1(abc varchar(20), cde int(20), eg date, asda varchar(20));
I don't what i am doing wrong. Syntax seems to be fine.
drop procedure if exists sp;
CREATE PROCEDURE sp()
BEGIN
DECLARE intoffer INT;
SELECT max(asda) INTO intoffer FROM test11;
IF (intoffer IS NULL) THEN
SET intoffer = 1;
ELSE
SET intoffer = intoffer + 1;
END IF;
INSERT INTO test1 (asda) VALUES (intoffer);
end;
This is another procedure throwing same error. After removing the if..end-if both the code seems to work fine.
You are Missing fetch from
drop procedure if exists test;
delimiter $$
create procedure `test`()
begin
DECLARE done INT DEFAULT FALSE;
DECLARE i1 VARCHAR(20);
DECLARE i2 DATE;
DECLARE i3 VARCHAR(20);
DECLARE i4 INT;
DECLARE curs1 CURSOR FOR SELECT * FROM test11;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs1;
insertLoop: LOOP
FETCH FROM curs1 INTO i1,i2,i3,i4;
if i4=22 then
insert into test1(abc) values(i1);
end if ;
END LOOP insertLoop;
CLOSE curs1;
end ;
;;
delimiter ;
I would like to create a table (two columns, first is auto incremental) which contains the term names, e.g. "SS 2000" and "WS 2000/2001" (for summer and winter term).
I tried the following:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
SET #YearEnd = 2014;
Declare #YearFrom integer = #YearEnd - 100;
Declare #Term varchar = '';
while #YearFrom < #YearEnd Begin
#Term = concat('SS ', #YearFrom);
Insert into terms (term) VALUES #Term;
Set #YearFrom = #YearFrom + 1;
End
END
but I already get an error in line 3: SQL query:
CREATE PROCEDURE create_terms()
BEGIN
Declare #YearEnd integer;
MySQL said: Documentation #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 '#YearEnd integer' at line 3
After reading the comments from Abishek and Stuart, I tried the following:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END;
DELIMITER ;
This results in just the DROP PROCEDURE command being successfully processed. Even when removing this line and changing the first lines to:
DELIMITER $$
CREATE PROCEDURE create_terms()$$
BEGIN
it doesn't work, the SQl console just writes "ERROR" and that's it.... :(
There are quite a bunch of errors here
You should use procedure variables
The Syntax for insert is INSERT INTO Table(columns) VALUES(values);
You probably also want to insert the term, not the year end
Your syntax for while is wrong
CREATE PROCEDURE create_terms()
BEGIN
DECLARE YearEnd INT;
Declare YearFrom INT;
Declare Term varchar(10);
SET YearEnd = 2014;
SET YearFrom = YearEnd - 100;
SET Term= '';
WHILE (YearFrom < YearEnd) DO
SET Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES (Term);
Set YearFrom = YearFrom + 1;
END WHILE;
END
Fiddle here
All variable staring # are user variables and need not to be declared, procedures has their local variables not prefix with #, try like this:
DELIMITER $$
DROP PROCEDURE IF EXISTS create_terms$$
CREATE PROCEDURE create_terms()
BEGIN
Declare YearEnd integer;
SET YearEnd = 2014;
Declare YearFrom integer = YearEnd - 100;
Declare Term varchar = '';
while YearFrom < YearEnd Begin
Term = concat('SS ', YearFrom);
Insert into terms (term) VALUES YearFrom;
Set YearFrom = YearFrom + 1;
End;
END;
DELIMITER ;
Here is help