backtrack
backtrack

Reputation: 8144

MySQL stored procedure is not working

I am new to Mysql. I have a stored procedure,below is my SP

CREATE DEFINER=`root`@`localhost` PROCEDURE `insertGroup`(IN startdate date, 
IN  enddate date, 
 IN  amount int, 
IN  groupname char(50))
BEGIN

DECLARE c_id INT; 

set c_id  =(select  id from c_begin where startdate = startdate and enddate = enddate and amount = amount);


insert into c_group (c_beginid,Groupname) 
select * from (select c_id,groupname) as temp 
where not exists (
select c_beginid,groupname from c_group where c_beginid = c_id and groupname = groupname 
) ;


END

I am calling it like

call insertGroup ('2014-01-01','2015-12-31',100000,'GroupD');

But it is not inserting any row. I am trying to insert into "c_group" if the c_beginid and groupname is not exists.

But when i try like below

insert into c_group (c_beginid,Groupname) 
select * from (select 1,'GroupD') as temp 
where not exists (
select c_beginid,groupname from c_group where c_beginid = 1 and groupname = 'GroupD' 
) limit 1 ;

it is working.

So What went wrong in my "insertGroup" SP. Can any one help me ?

Thanks ,

Upvotes: 0

Views: 44

Answers (1)

Parth Akbari
Parth Akbari

Reputation: 651

Rename your input parameter to make it clear to the DB engine when you mean the parameter and when the column name.

where startdate = startdate and enddate = enddate and amount = amount 

is always true since the engine thinks you compare a column to itself.

Upvotes: 2

Related Questions