0

This is code of mysql stored procedure:

BEGIN
DECLARE sms INT(100);
SET sms=(SELECT COUNT(1) FROM tbl_intofone_alert_transaction WHERE smsid='sms_id');
IF(sms<=>0) THEN
INSERT INTO tbl_intofone_alert_transaction(MSISDN,Message,Tag,SenderID,InTime,deliverystatus,
memberid,smsid,updatetime,submit_type,wu_id,DeviceID) 
VALUES(mob,msg,tag,sender,NOW(),'0',memid,sms_id,NOW(),'NULL','NULL',DevId);
END IF;
   END$$ 

IF CONDITION in above code doing insertion for all the cases . what i have done wrong here?

Ekky
  • 684
  • 2
  • 14
  • 28
  • Which cases of sms do you want to accept ? – xlecoustillier Dec 18 '12 at 14:38
  • Well, what happens if you just run the first `SELECT` manually? And is it possible that `sms` is ever `NULL`? – Ja͢ck Dec 18 '12 at 14:38
  • it gives required result – Ekky Dec 18 '12 at 14:40
  • 2
    @Ekansh Really? Because `smsid` doesn't sound like a column that can contain a string. – Ja͢ck Dec 18 '12 at 14:42
  • Why you don't use the solution provided here: http://stackoverflow.com/questions/3164505/mysql-insert-record-if-not-exists-in-table It is much cleaner and does not require a variable. – CodeZombie Dec 18 '12 at 14:44
  • 2
    @Jack sorry i have found what was the problem actually . (1) i have assigned variable "sms_id" as string and (2) in the "IF statement" use of operator <=> it must be <=. thanx for pointimg that.. – Ekky Dec 18 '12 at 14:46
  • @Ekansh: Actually both `<=> 0` and `<= 0` will work fine, but isn't it much simpler to just use `= 0` to test for equality with zero? It seems like you are trying random operators until it works instead of thinking about how to solve the problem. – Mark Byers Dec 18 '12 at 14:59
  • thank you @MarkByers , i have use "=" as per my requirement. – Ekky Dec 18 '12 at 15:00

2 Answers2

2

it must be

   IF(sms<=0) THEN    //sms  less then or equal 0

or

  IF(sms>=0) THEN      //sms  bigger then or equal 0

<=> this is not comparaison in mysql

obs

  VALUES(mob,msg,tag,sender,NOW(),'0',memid,sms_id,NOW(),'NULL','NULL',DevId);

most of those values i dont know what they are , they looks wrong

they must be variables that u get them from your code.

in this code there is only NOW() and NULL which are right

echo_Me
  • 35,836
  • 5
  • 52
  • 76
1

Are you sure that you want to search for the string literal 'sms_id' here?

SELECT COUNT(1) FROM tbl_intofone_alert_transaction WHERE smsid='sms_id'

Probably you meant to use the value of the sms_id variable:

SELECT COUNT(1) FROM tbl_intofone_alert_transaction WHERE smsid = sms_id
Mark Byers
  • 719,658
  • 164
  • 1,497
  • 1,412