im creating stored procedure in my sql but getting error,error shows as below,help me Error Descrption:You hve an error in your sql syntax: ckeck the manual that corresponds to your MYSQL
server version fro the right syntax to use near 'END' at line 13
DELIMITER $$
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF
END $$
DELIMITER ;
You're missing a ; in this line:
END IF
It should be:
END IF;
IF is a statement and all statements must end in semi-colons. This one's no different. See the MySQL documentation for IF.
DROP PROCEDURE IF EXISTS myhealthcamp.area $$
CREATE PROCEDURE myhealthcamp.area
(
IN id INT,
IN ar VARCHAR(45)
)
BEGIN
if exists (select area_id from area where area_id = id)
Then
update area set areaname = ar where area_id=id;
else
insert into area(area_id, areaname) values(id,ar);
END IF;
semicolon missing
Thanks
Sanil
Related
The below is my Stored Procedure(Routine) to check whether or not a user with Username(input) exists in the database.
Inside the database, I already have a user with Username - 'dev'.
However, when I ran the below routine, it returned me with res = 1, which I expected it to be -1.
I called the routine this way. Please correct me too if I am calling it the wrong way. I am really new to MySQL Routines.
CALL usp_GetUserValidation ('dev', #ErrorCode)
Can any MySQL Routine pros here enlighten me on this? Thank you in advance guys :)
DELIMITER $$
CREATE PROCEDURE usp_GetUserValidation(IN `#Username` VARCHAR(255), OUT `#ErrorCode` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'To validate user login'
BEGIN
IF EXISTS
(SELECT UserID
FROM mt_User
WHERE UserName = #Username)
THEN
SET #ErrorCode = -1;
ELSE
SET #ErrorCode = 1;
END IF;
SELECT #ErrorCode AS res;
END$$
DELIMITER ;
It was simply your naming conventions for the parameters. It is finicky and does not like User Variable # signs in them.
You are just testing I can see, as you are returning both a resultset with the info and the OUT variable.
drop procedure if exists usp_GetUserValidation;
DELIMITER $$
CREATE PROCEDURE usp_GetUserValidation(IN pUsername VARCHAR(255), OUT pErrorCode INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT 'To validate user login'
BEGIN
IF EXISTS
(SELECT UserID
FROM mt_User
WHERE UserName = pUsername)
THEN
SET pErrorCode = -1;
ELSE
SET pErrorCode = 1;
END IF;
SELECT pErrorCode AS res;
END$$
DELIMITER ;
Schema:
-- drop table if exists mt_user;
create table mt_User
( UserID int auto_increment primary key,
UserName varchar(100) not null,
unique key(UserName)
);
insert mt_User(UserName) values ('dev');
select * from mt_User;
Test:
set #var1:=-4;
call usp_GetUserValidation('dev',#var1);
-- returns (-1) ---- Yea, we like that
select #var1;
-- (-1)
set #var1:=-4;
call usp_GetUserValidation('dev222',#var1);
-- returns 1 ---- Yea, we like that
select #var1;
-- 1
simple question
this is the code to creat a simple stored procedure
DELIMITER $$
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END $$
DELIMITER ;
and this is the 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 'DELIMITER' at line 1
try placing // instead of $$:
DELIMITER //
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END //
DELIMITER ;
I have a MySQL stored procedure that throws an error.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test_schema`.`TEST_SPROC` $$
CREATE PROCEDURE `test_schema`.`TEST_SPROC` (IN var0 INT, var1 INT)
BEGIN
DECLARE var0 INT;
DECLARE var1 INT;
SELECT COUNT(*) INTO var0 FROM INFORMATION_SCHEMA.`TABLES`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='original_table';
SELECT COUNT(*) INTO var1 FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF var0=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
END IF;
IF var1=1 THEN
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
END IF; #error is thrown here.
END $$
DELIMITER ;
Error:
Script line: 4 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 '; END IF;
END' at line 15
What is wrong with my if statement?
Change this line:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR;
to this:
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(100);
Of course you should specify a length for the VARCHAR column that is appropriate. I've just used VARCHAR(100) as an example.
You have to specify a length for the alter table statement. See the comment below:
IF varTable=1 THEN
RENAME TABLE test_schema.original_table TO test_schema.new_table;
SELECT COUNT(*) INTO varColumn FROM INFORMATION_SCHEMA.`COLUMNS`
WHERE `TABLE_SCHEMA`='test_schema' AND `TABLE_NAME`='new_table'
AND `COLUMN_NAME`='id';
IF varColumn=1 THEN
#The following statement must be "VARCHAR(255)" not just "VARCHAR"
ALTER TABLE test_schema.new_table CHANGE id AccountID VARCHAR(255);
ELSE
#statements.
END IF;
END IF;
I am new to creating procedures in mysql, i know how to create them in MSSQL, but i am not sure what is wrong with this, it says Syntax Error Near END
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID
END
The query in your procedure needs a semi colon after it:
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END
You may also need to set the delimiter to something. The MySQL documentation does this:
DELIMITER //
CREATE PROCEDURE GetNameByID(IN CustID INT)
BEGIN
SELECT * FROM Customers WHERE CustomerID = CustID;
END//
(but obviously not with your query)
You are missing the ; at the end of select statement
if i use one table in two different Stored procedure name, (one for insert , one for update command) it showing syntax error.
first i created studentrc SP:
delimiter //
create procedure studentrc(in student_name varchar(20),in Reg_no int(6),in mark1 int(3), in mark2 int(3),in total int(10))
begin
insert into studentrecords values(student_name,Reg_no,mark1,mark2,total);
end; //
no errors
next i create studentrcs SP:
delimiter //
create procedure studentrcs(inout Reg_no int(6))
begin
UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=?;
end;//
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 'UPDATE studentrecords
set student_name=?,mark1=?,mark2=?,total=?
where Reg_no=' at line 3
how can rectify this error...
I'll be the first to admit I'm not a SQL guru by any means, but shouldn't you be taking in more variables and using them in place of the question marks?
You need to put the news values in the args list
create procedure studentrcs(sname varchar(100),m1 varchar(100),m2 varchar(100),total int,inout Reg_noarg int(6))
begin
UPDATE studentrecords
set student_name=sname ,mark1=m1,mark2=m2,total=totalarg
where Reg_no=Reg_noarg ;
for "INSERT"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10),IN field3 INT(10), IN field4 VARCHAR(10))
BEGIN
INSERT INTO mytablename (FIELD_1,FIELD_2,FIELD_3,FIELD_4) VALUES
(field1,field2,field3,field4);
END $$
DELIMITER ;
size which i have given in IN parameter should be equal to field size in table
for "UPDATE"
DELIMITER $$
DROP PROCEDURE IF EXISTS `mydatabase`.`myprocedurename` $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `myprocedurename`(IN field1 VARCHAR(50),
IN field2 INT(10) )
BEGIN
UPDATE mytablename SET FIELD_1=filed1 WHERE FIELD_2=field2;
END $$
size which i have given in IN parameter should be equal to field size in table
hope this may help you.