How do I throw an error handler if parameter doesn't exist in table?

32
October 25, 2019, at 4:00 PM

So I have this stored procedure:

delimiter //
create procedure sp_finish_campaign(in c_title varchar(30))
begin 
    update campaign set CAMPAIGNFINISHDATE = CURDATE()
    where TITLE = c_title;
end//
delimiter ;

I want to know how to throw an error handler with a message if the c_title parameter doesn't exist in the campaign table. I have tried using

DECLARE EXIT HANDLER FOR SQLSTATE '42000' SELECT 'Error!';

and some if statements but either the syntax is wrong or it comes up with an error 1054 unknown column title in fieldlist.

Answer 1

use this code:

create procedure sp_finish_campaign(in c_title varchar(30))
begin 
    update campaign set CAMPAIGNFINISHDATE = CURDATE()
    where TITLE = c_title;
    if @@rowcount=0
    begin
      DECLARE @ErrorMessage  NVARCHAR(4000);
      DECLARE @V_ErrorCode   INT;
      DECLARE @ErrorState    INT;
      SELECT @V_ErrorCode   = 50000 ,
             @ErrorMessage  = c_title+ ' is not exists' ,
             @ErrorState    = 1
      THROW @V_ErrorCode,@ErrorMessage,@ErrorState
    end
end//
READ ALSO
Derive new column of weekly unique users alongside daily unique users in MySQL

Derive new column of weekly unique users alongside daily unique users in MySQL

I have a table where I have daily unique users in the month of JanuaryI am trying to derive a new column (in the same table) that shows total weekly unique users

26
Laravel PDOException(code: 0) failed loading cafile stream

Laravel PDOException(code: 0) failed loading cafile stream

I get this error when I try to perform a DB operation from the public siteThis is from a server that connects remotely to a dedicated database server

30
The cursor of pymysql fail to exercute SQL language

The cursor of pymysql fail to exercute SQL language

I would like to use pymysql to update the data in mysql databaseBut I don't know what's going wrong with my code

36