This is my Mysql code ,
It gives a
syntax error unexpected Declare_sym
delimiter #
create procedure StudentUpsert
(in in_regno int ,
in in_fNM varchar(50),
in in_mNM varchar(50),
in in_lNM varchar(50),
begin
declare regno_Count int ;
select Count(*) into regno_Count
fro students_info
if regno_Count > 0 then
update students_info
set firstname = in_fNM,
middlename = in_mNM,
lastname = in_lNM,
where regno = in_regno ;
else
insert into students_info
values (in_regno , in_fNM , in_mNM ,in_lNM );
end if ;
end #
delimiter ;
call StudentUpsert(9, 'ABC','NA','XYZ');
please help
syntax error unexpected Declare_sym
I don't see any Declare_sym in your posted query but your SQL code do have syntax error as pointed below. Not sure if those are just typo while posting the question
select Count(*) into regno_Count
fro students_info
^...... should be *from*
update students_info
set firstname = in_fNM,
middlename = in_mNM,
lastname = in_lNM, <----- remove this extra *,* here
where regno = in_regno ;
Change your procedure body to be like below
delimiter #
create procedure StudentUpsert
(in in_regno int ,
in in_fNM varchar(50),
in in_mNM varchar(50),
in in_lNM varchar(50))
begin
declare regno_Count int ;
select Count(*) into regno_Count
from students_info;
if regno_Count > 0 then
update students_info
set firstname = in_fNM,
middlename = in_mNM,
lastname = in_lNM
where regno = in_regno ;
else
insert into students_info
values (in_regno , in_fNM , in_mNM , in_lNM);
end if;
end#
Related
I created a parameterized STORED PROCEDURE with CRUD operation in MYSQL Version(6.0.11-alpha-community)
DELIMITER $$
CREATE PROCEDURE `mae`.`USP_CustomersToken_CURD`(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETIME , IN OPType INT)
BEGIN
IF (OPType = 1) --Insert
THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VALUES (CID, CName, CToken, CTokenTime)
ELSEIF (OPType = 2) --delete
THEN
DELETE FROM tbl_CustomersTokenInfo WHERE ID = CID
ELSEIF (OPType = 3) --Update
THEN
UPDATE tbl_CustomersTokenInfo SET CustomerName = CName, Token = CToken , TokenTime = CTokenTime WHERE ID = CID
ELSEIF (OPType = 4) --Select
THEN
SELECT * FROM tbl_CustomersTokenInfo WHERE ID =CID
ELSEIF (OPType = 5)--Fetch
THEN
SELECT * FROM tbl_CustomersTokenInfo
END IF
END$$
DELIMITER ;
but when I executing this SP I'm getting this below error :
Query: CREATE PROCEDURE mae.USP_CustomersToken_CURD(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETI...
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 'THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VAL' at line 4
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
I'm new to MYSQL commands and syntaxes if I did any mistake give the best approach .
MySQL (unlike most other DBMS) doesn't like comments introduced with -- where no space follows --.
You need to terminate all the statements, including the IF statement (after END IF), with a ;.
Correct that:
DELIMITER $$
CREATE PROCEDURE `mae`.`USP_CustomersToken_CURD`(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETIME , IN OPType INT)
BEGIN
IF (OPType = 1) -- Insert
THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VALUES (CID, CName, CToken, CTokenTime);
ELSEIF (OPType = 2) -- delete
THEN
DELETE FROM tbl_CustomersTokenInfo WHERE ID = CID;
ELSEIF (OPType = 3) -- Update
THEN
UPDATE tbl_CustomersTokenInfo SET CustomerName = CName, Token = CToken , TokenTime = CTokenTime WHERE ID = CID;
ELSEIF (OPType = 4) -- Select
THEN
SELECT * FROM tbl_CustomersTokenInfo WHERE ID =CID;
ELSEIF (OPType = 5)-- Fetch
THEN
SELECT * FROM tbl_CustomersTokenInfo;
END IF;
END$$
DELIMITER ;
I have a Studentstatus table which gets a list of students from student table.
I made this procedure to update or create a new record, but I get the following error:
Procedure or function SaveAdvStudStata has too many arguments specified.
CREATE PROCEDURE [dbo].[SaveAdvStudStata]
#studentId int,
#stata int,
#description varchar(MAX),
#date DATE,
#issuedBy nvarchar(128)
AS
BEGIN
if exists (select StudentId
from dbo.StudentStata
where StudentId = #studentId
)
begin
Update dbo.StudentStata
set Stata = #stata
,Description = #description
,Date = #date
,IssuedBy = #issuedBy
where StudentId = #studentId
end
else
begin
Insert into dbo.StudentStata(StudentId
,Stata
,Description
,Date
,IssuedBy
)
VALUES (#studentId
,#stata
,#description
,#date
,#issuedBy
)
end
END
How do I check if the firstname is not filled and I've tried not null like this:
DELIMITER go
Create procedure registerusers(
Out UserID tinyint(11),
IN iFirstName varchar(30),
IN iLastName varchar(30),
IN iPassword varchar(30),
IN iEmailAddress varchar(30),
IN iSalt varchar(40),
IN iRoleID varchar(1))
BEGIN
/* I've used not null and it works for empty firstname but when I
try to pass the firstname it didn't work and I kept getting an error message saying "fill out the firstname" */
If(iFirstName not null) then
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'Fill out the First Name ';
else
insert into users(
/* insert into user if its not empty */
FirstName,
LastName ,
Password ,
EmailAddress ,
Salt ,
RoleID
)
Values
(
iFirstName,
iLastName ,
iPassword ,
iEmailAddress ,
iSalt ,
iRoleID
);
set UserID = last_insert_id();
end if;
End
go
DELIMITER ;
However when I used
set #new_id = null;
call registerusers(#new_id,'','Jones','5566','jones#gmail.com','sdfd','1');
select #new_id;
it manages to display an error message but when I used
set #new_id = null;
call registerusers(#new_id,'Jason','Jones','5566','jones#gmail.com','sdfd','1');
select #new_id;
for inserting the firstname it kept displaying an error message other than wanting to insert the data.
I've tried if(firstname is null) but it didn't work because it went pass through message. For example if I used
set #new_id = null;
call registerusers(#new_id,'','Jones','5566','jones#gmail.com','sdfd','1');
select #new_id;
as empty for firstname it went through and it suppose to display an error message. If I used
set #new_id = null;
call registerusers(#new_id,'Jason','Jones','5566','jones#gmail.com','sdfd','1');
select #new_id;
it manages to go through.
MySQL stored procedure will execute but conditions are not working. Could someone clear up this issue?
The empty value is not checking with or condition. Does it need a replacement for or?
DELIMITER $$
CREATE DEFINER=`rebar`#`%` PROCEDURE `SearchInProgress`(
ClientID bigint,
GCName varchar(250),
TeamID int,
USPMID Bigint,
JobReceivedDate datetime,
importanceID Bigint
)
begin
select * from jobdetails
where
(clientid = ClientID or ClientID = "") and
(GCName = GCName or GCName ="") and
(TeamID = TeamID or TeamID ="") and
(ReceivedDate = JobReceivedDate or JobReceivedDate = "") and
(ImportanceID = importanceID or importanceID = "") and
(JobID in (select jobid from JobCoordinatorDetails where USProjectManagerID = USPMID) );
end
I think it could be brackets
This (clientid = ClientID or ClientID = "") could be written like this
(clientid = ClientID) OR (ClientID = "" )
Its justa thought. I might be wrong
DELIMITER $$
DROP PROCEDURE if exists SearchInProgress $$
CREATE PROCEDURE `SearchInProgress`(
aclientID bigint,
aGCName varchar(250),
aTeamID int,
aUSProjectManagerID bigint,
aReceivedDate datetime,
aImportanceID bigint
)
begin
select * from jobdetails
where
(clientid = aclientID or aclientID = '') and
(GCName = aGCName or aGCName = '') and
(TeamID = aTeamID or aTeamID = '') and
(ReceivedDate = aReceivedDate or aReceivedDate = '0000-00-00 00:00:00') and
(ImportanceID = aImportanceID or aImportanceID = '') and
(JobID in (select jobid
from JobCoordinatorDetails
where USProjectManagerID = aUSProjectManagerID) or aUSProjectManagerID = '')
;
end
The parameter name and the column name must be different. I've prepended a to the parameter names.
An empty datetime value is replaced by '0000-00-00 00:00:00': https://dev.mysql.com/doc/refman/5.7/en/datetime.html
I have written the following stored procedure which in HeidiSQL is giving me an Error 1064 at the line starting with SET pay_ref = SELECT CONCAT('KOS' ...
Let me firstly explain what's going on with this procedure. I have a table gamers with a BIGINT primary key with auto_increment. This proc is supposed to:
Take in some params from the user
Check if the user already exists in the db according to his/her email address, and spits back the word "DUPLICATE" if a reord does exist
Else it does the insert as normal
Then it reads in the ID of the new record created and converts it to a varchar, pads it with leading zeros and then gets concatenated with some other strings
This new string (which should read for example KOS00001ABCDEF) then gets updated to the pay_refcode field >>> this is how we have settled on generating a unique payment reference for the user
If all works out well it updates retval with the newly generated reference code to be read by PHP script.
DELIMITER //
CREATE PROCEDURE `InsertGamer` (
IN p_fname VARCHAR(30),
IN p_lname VARCHAR(30),
IN p_email VARCHAR(255),
IN p_favgame VARCHAR(60),
IN p_pay_suffix VARCHAR(6),
OUT retval VARCHAR(14)
)
BEGIN
DECLARE last_id BIGINT;
DECLARE pay_ref VARCHAR(14);
IF (EXISTS(SELECT * FROM gamers WHERE (email = p_email))) THEN
SET retval = 'DUPLICATE';
ELSE
INSERT INTO gamers (fname, lname, email, favgame, pay_refcode)
VALUES (p_fname, p_lname, p_email, p_favgame, NULL);
SET last_id = LAST_INSERT_ID();
SET pay_ref = SELECT CONCAT('KOS', (SELECT LPAD(CONVERT(last_id, VARCHAR(5)),5,'0')), p_pay_suffix);
UPDATE gamers
SET pay_refcode = pay_ref
WHERE application_id = last_id;
SET retval = pay_ref;
END IF;
END //
I cannot for the life of me figure out what the problem is and would sincerely appreciate any help from you. Thank you very much in advance!
You just need to remove the SELECT keyword from line which you set the value for pay_ref.
SET pay_ref = CONCAT('KOS', LPAD(CONVERT(last_id, CHAR(5)),5,'0'), p_pay_suffix);
full code:
DELIMITER //
CREATE PROCEDURE `InsertGamer` (
IN p_fname VARCHAR(30),
IN p_lname VARCHAR(30),
IN p_email VARCHAR(255),
IN p_favgame VARCHAR(60),
IN p_pay_suffix VARCHAR(6),
OUT retval VARCHAR(14)
)
BEGIN
DECLARE last_id BIGINT;
DECLARE pay_ref VARCHAR(14);
SET #count := (SELECT COUNT(*) FROM gamers WHERE email = p_email)
IF (#count > 0) THEN
SET retval = 'DUPLICATE';
ELSE
INSERT INTO gamers (fname, lname, email, favgame, pay_refcode)
VALUES (p_fname, p_lname, p_email, p_favgame, NULL);
SET last_id = LAST_INSERT_ID();
SET pay_ref = CONCAT('KOS', LPAD(CONVERT(last_id, CHAR(5)),5,'0'), p_pay_suffix);
UPDATE gamers
SET pay_refcode = pay_ref
WHERE application_id = last_id;
SET retval = pay_ref;
END IF;
END //
DELIMITER ;