Parse Comma(,) String in MySql stored procedure - mysql

Stored procedure:
CREATE PROCEDURE lead_to_loan(xReffID_list text)
I want to use this xReffID_list variable in a select statement as
SELECT * FROM XXXX where xreffID IN (xReffID_list);
but the xreffID is a int Variable
How Can I use xReffID_list text which is a string of comma separated numbers in the INcondition for int variables ?
Stored procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS lead_to_loan$$
CREATE PROCEDURE lead_to_loan(XRefID_list text)
BEGIN
DECLARE loanCount int(11) default 0;
DECLARE matchCount int(11) default 0;
DECLARE loan_XRefID int(11);
DECLARE loan_LEADS360ID int(11);
DECLARE loan_email varchar(100);
DECLARE loan_phone varchar(30);
DECLARE loan_cellphone varchar(20);
DECLARE loan_workphone varchar(20);
DECLARE loan_closeDate datetime;
DECLARE loan_FundedDate datetime;
DECLARE lead_id int(11);
DECLARE lead_RefId varchar(100);
DECLARE lead_Email varchar(100);
DECLARE lead_DayPhone varchar(50);
DECLARE lead_EveningPhone varchar(20);
DECLARE lead_Cellphone varchar(20);
DECLARE lead_DateAdded varchar(30);
DECLARE done boolean default false;
DECLARE startTime datetime;
DECLARE cursor_loanDetail CURSOR FOR
SELECT XRefID,LEADS360ID,email,phone,cellphone,workphone,closeDate,FundedDate
FROM fsbcorponline.view_loandetail where find_in_set(XRefID, XRefID_list) > 0;
DECLARE cursor_loanMatchLeads CURSOR FOR
SELECT id,RefId,Email,DayPhone,EveningPhone,Cellphone,DateAdded
FROM fsbcorponline.leads360leads
WHERE RefId !="" AND RefId IS NOT NULL AND RefId =loan_LEADS360ID AND loan_LEADS360ID>0 OR
Email !="" AND Email IS NOT NULL AND Email =loan_email OR
DayPhone !="" AND DayPhone IS NOT NULL AND DayPhone = loan_workphone OR
EveningPhone !="" AND EveningPhone IS NOT NULL AND EveningPhone= loan_phone OR
Cellphone !="" AND Cellphone IS NOT NULL AND Cellphone =loan_cellphone;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
set startTime = now();
OPEN cursor_loanDetail;
cursor_loanDetail_loop: loop
fetch cursor_loanDetail into loan_XRefID,loan_LEADS360ID,loan_email,loan_phone,loan_cellphone,loan_workphone,loan_closeDate,loan_FundedDate;
if done then
set done = false;
leave cursor_loanDetail_loop;
END if;
SET loanCount = loanCount+1;
OPEN cursor_loanMatchLeads;
cursor_loanMatchLeads_loop: loop
fetch cursor_loanMatchLeads into lead_id,lead_RefId,lead_Email,lead_DayPhone,lead_EveningPhone,lead_Cellphone,lead_DateAdded;
if done then
set done = false;
leave cursor_loanMatchLeads_loop;
END if;
SET matchCount = matchCount+1;
INSERT INTO `fsbcorponline`.`leads_to_loan`(`lead_id`,`lead_RefId`,`lead_Email`,`lead_DayPhone`,`lead_EveningPhone`,`lead_Cellphone`,`lead_DateAdded`,`loan_XRefID`,`loan_LEADS360ID`,`loan_email`,`loan_phone`,`loan_cellphone`,`loan_workphone`,`loan_closeDate`,`loan_FundedDate`)
VALUES(lead_id,lead_RefId,lead_Email,lead_DayPhone,lead_EveningPhone,lead_Cellphone,lead_DateAdded,loan_XRefID,loan_LEADS360ID,loan_email,loan_phone,loan_cellphone,loan_workphone,loan_closeDate,loan_FundedDate)
ON duplicate key update loan_updateCount = loan_updateCount +1 ;
leave cursor_loanMatchLeads_loop;
END loop cursor_loanMatchLeads_loop;
CLOSE cursor_loanMatchLeads;
END loop cursor_loanDetail_loop;
close cursor_loanDetail;
INSERT INTO `fsbcorponline`.`log`(`processName`,`pageName`,`path`,`status`,`note`,`processStartTime`,`processEndTime`)
VALUES('Store Procedure','Lead_to_Loan','Database','1',CONCAT('Loan Matches ',matchCount,' of total ',loanCount),startTime,now());
END$$
DELIMITER ;

You can use find_in_set to do this:
SELECT * FROM XXXX WHERE find_in_set(xreffID, xreffID_list) > 0

Hey you can use cast() function available with Mysql
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
Hope this will help you.

Related

MYSQL := is not a valid at this position, expecting an identifier

The task is create a stored procedure which creates a statistic of students by city. The result should be like - -
`
delimiter $$
create procedure courcity()
begin
declare list_stud text default '';
declare _cty varchar(50);
declare _count int;
declare _name_stud varchar(50);
declare done integer default FALSE;
declare cty_cur cursor for select cty, count(*) as count from student group by cty order by cty;
declare stud_cur cursor for select name_stud from student where cty = _cty;
declare continue handler for NOT found set done = TRUE;
create temporary table return_table(
cty varchar(50),
count_stud int,
list_stud text default ''
);
begin
declare list_stud text default '';
declare _cty varchar(50);
declare _count int;
declare _name_stud varchar(50);
declare done integer default FALSE;
declare cty_cur cursor for select cty, count(*) as count from student group by cty order by cty;
declare stud_cur cursor for select name_stud from student where cty = _cty;
open cty_cur;
open stud_cur;
read_loop: LOOP
fetch cty_cur into _cty, _count;
IF done THEN
LEAVE read_loop;
read_loop1: loop
fetch stud_cur into _name_stud;
if done then leave read_loop1;
list_stud := list_stud || _name_stud || ', ';//here is the problem
end loop;
close stud_cur;
insert into return_table values (_cty, _count, list_stud);
list_stud := '';
end loop;
close cty_cur;
select * from return_table;
end;$$
delimiter ;
`
Can't create this procedure due to this problem.Can't find mistake.
IF THEN must be closed with END IF;
https://www.mysqltutorial.org/mysql-if-statement/
IF condition THEN
statements;
END IF;
|| is definitely not correct in MySQL. on the right side of := you should have a variable or a valid expression.

Mysql Store procedure stops iteration after some rows

Below procedure code running with out error . Line select FOUND_ROWS() returning n no. rows but cursor curs did not loop all rows
CREATE DEFINER=`root`#`%` PROCEDURE `middleLocationAutoUpdatePorc`()
BEGIN
declare vname varchar(45);
declare vmobile varchar(20);
declare vsapid varchar(6);
declare vuser varchar(45);
declare vday date;
declare vmintime time;
declare vmaxtime time;
declare vminLocation mediumtext;
declare vmaxLocation mediumtext;
declare vmiddlelocation mediumtext;
declare vdistance int(11);
declare vdrdid int(11);
declare vn_no_details int(11);
declare uatt varchar(45) default 'X';
declare adatta varchar(45) default 'X';
declare b_not_found BOOL DEFAULT FALSE;
declare curs CURSOR FOR
SELECT user ,day, mintime,maxtime_mobile,minLocation,maxLocation,distance,n_no_details,drdid FROM mydb.statusreport where day >= '2017-06-26';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET b_not_found = true;
OPEN curs;
loop1: LOOP
FETCH curs INTO vuser,vday,vmintime,vmaxtime,vminLocation,vmaxLocation,vdistance,vn_no_details,vdrdid;
IF b_not_found THEN
LEAVE loop1;
END IF;
-- select FOUND_ROWS();
case substring_index(vuser,"-",-1)
when 'RSM' then
select username,Name,mobile into vsapid,vname,vmobile from mydb.supervisor where idSu=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
when 'ASM' then
select userName,name,mobile into vsapid,vname,vmobile from mydb.executive where exeid=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
when 'F' then
select sapId,name,phone into vsapid,vname,vmobile from mydb.user where idUser=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER);
end case;
select userGiven , adminGiven into uatt ,adatta from userdaystatus st where st.date =vday and st.idUser=CONVERT(substring_index(vuser,"-",1),UNSIGNED INTEGER) and st.userType=substring_index(vuser,"-",-1);
set vmiddlelocation=( select location from mydb.dailyReportDetails as dtb where dtb.idDailyReport=vdrdid and dtb.counter>=(abs(vn_no_details/2) ) order by dtb.idDailyReportDetails asc limit 1);
call mydb.addOMiddleLocation(day(vday), month(vday),
year(vday),vuser, vname,
vsapid, vmobile, vmintime,
vmaxtime, SUBTIME(vmaxtime,vmintime),
vminLocation,
vmiddlelocation,
vmaxLocation,Round(vdistance/1000,2),
uatt, adatta);
END LOOP;
CLOSE curs;
END
The Procedure call mydb.addOMiddleLocation() just inserting Row on another table.There does not have any data type validation .
So what can be the problem ?
It happen for log wait time for a particular Query line
set vmiddlelocation=( select location from mydb.dailyReportDetails as dtb where dtb.idDailyReport=vdrdid and dtb.counter>=(abs(vn_no_details/2) ) order by dtb.idDailyReportDetails asc limit 1);

mysql procedure : syntax, syntax, syntax ...... too hard to find

Here is my procedure :
delimiter //
drop procedure if exists migContactToActor;
create procedure migContactToActor()
begin
declare vctaid int;
declare firstname char(255);
declare lastname char(255);
declare phone char(255);
declare cellphone char(255);
declare fax char(255);
declare mail char(255);
declare location char(255);
declare extcode char(255);
declare vcpyid int;
declare vquaid int;
declare userModif char(255);
declare entityIdResp int;
declare niveauCreat char(255) default "contactMigration2.21";
declare userCreat char(255) default "contactMigration2.21";
declare backIdResp int;
declare userIdResp int;
declare zipcode char(255);
declare country char(255);
declare dateModif char(255);
declare dateCreated char(255);
declare dateRelation char(255);
declare groupLabel char(255);
declare adminLogId int;
declare finContact boolean default 0;
declare ctt int default 1; /* decompte contacts */
declare entityName char(255) default ''; /* entite courante du user a linker */
declare entityId int default 0; /* et son Id */
declare vctaidPrev int default 0;
declare curs1 cursor for select
ctaid, ctafirstname,ctalastname,ctaphone,ctacellphone,ctaemail,
substr(concat_ws(' ',ctaaddress,ctaaddress2,ctacity,ctazipcode),1,255),ctacode, cpyid,quaid,C.enoid_resp,
ctafax,C.actid_bck,ctadate_created,ctacountry,ctadate_modified,C.actid_resp,ctauser_modified, G.hemlabel, R.sync_update
from contact C
left join hd_contact_group_relationship R on R.hbhid = C.ctaid
left join hd_contact_group G on G.hemid = R.hemid
where !ifnull(C.ctaflagdeleted,0) and !ifnull(G.hemflag_deleted,0);
declare continue handler for not found set finContact = 1;
declare continue handler for sqlexception
begin
rollback;
end;
select '======== debut ...';
drop table if exists ent221;
create temporary table ent221 as select enoid, enoname from entityowner where enoname like 'CLI.CG_%.CLI';
alter table ent221 add index(enoname); /* not unique in dev, fetch the first in prod if any */
set autocommit = 0; /* TTT */
start transaction; /* TTT */
open curs1;
contactloop:loop
fetch curs1 into vctaid, firstname,lastname,phone,cellphone,mail,location,extcode,vcpyid,vquaid,entityIdResp,
fax,backIdResp,dateCreated,country,dateModif,userIdResp,userModif,groupLabel,dateRelation;
if finContact then
close curs1;
leave contactloop;
end if;
if vctaid = vctaidPrev then
set vctaidPrev = vctaid;
if (mod(ctt,100) = 0) then
set #msg = concat(ctt, ' contacts migrated');
select #msg;
end if;
update OPMSequence set counter = (#wbuf1 := counter) + 1 where name = 'ACTOR';
insert into actor (actid,cpyid,actlastname,actfirstname,actemail,actphone,actmobilephone,
actlocalisation, actid_responsible_owner,actid_backup_owner,enoid_responsible_owner,
actuser_created,actniveau_created, actdate_created,actuser_modified,
actdate_modified,actcountry,actfax,actqualifid,actexternal_code)
values
(#wbuf1,vcpyid,lastname,firstname,mail,phone,cellphone,location,userIdResp,backIdResp,entityIdResp,
userCreat,niveauCreat,dateCreated,userModif,dateModif,country,fax,vquaid,extcode);
update adminLog set adltype = 'User', adlobject_id = #wbuf1,
adlobject_description = concat(adlobject_description,'//oldContact=',vctaid)
where adlobject_id = vctaid and adltype = 'Contact';
end if;
if groupLabel is not null then
set #entName = concat('CLI.CG_',groupLabel,'.CLI);
select enoid from ent221 where enoname = #entName limit 1 into #wbuf2;
if #wbuf2 is not null then
insert into actorentityrelationship (enoid,actid,aerdate_created,aerniveau_created) values
(#buf2,#wbuf1,dateRelation,niveauCreat);
enf if;
end if;
set ctt = ctt + 1;
end loop contactloop;
rollback; /* TTT */
end //
delimiter ;
call migContactToActor;
drop procedure migContactToActor;
Why this ???
Query OK, 0 rows affected, 1 warning (0.00 sec)
ERROR 1064 (42000): 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 ''.CLI);
select enoid from ent221 where enoname = #entName limit 1 into #wbuf2' at line 83
I think you missed the quotes
set #entName = concat('CLI.CG_',groupLabel,'.CLI);
with quotes
set #entName = concat('CLI.CG_',groupLabel,'.CLI');

Mysql stored procedure not returning any values

This is the first time I am working on Mysql stored procedure I know it is a lame question please spare me for this,
is it not possible to print any value after the END LOOP statement in MySQL procedure. If it is, how we can achieve this.
what I did for this:
BEGIN
DECLARE U_movingCity varchar(50);
DECLARE U_state varchar(50);
DECLARE U_education varchar(50);
DECLARE id2 int(10);
DECLARE RankPoint int(10) DEFAULT 0;
DECLARE movingCity2 varchar(50);
DECLARE state2 varchar(50);
DECLARE education2 varchar(50);
DECLARE cur1 CURSOR FOR SELECT id, state , education FROM user WHERE id != userid AND Enabled='y' AND Active='y';
SELECT state , education into U_state , U_education FROM user WHERE id = userid ;
OPEN cur1;
read_loop: LOOP
SET RankPoint := 0;
FETCH cur1 INTO id2, state2 , education2 ;
IF ((state2 = U_state)) THEN
SET RankPoint := RankPoint + 14;
END IF;
IF ((education2 = U_education)) THEN
SET RankPoint := RankPoint + 16;
END IF;
//this displays
select RankPoint;
END LOOP;
//this doesn't.
select id, RankPoint from user;
CLOSE cur1;
END
You should use id2 instead of id in your last query.
select `id2`, `RankPoint` from `user`;

Records are not inserting in to main table while running the mysql stored procedure

Im migrating procedures from oracle to mysql.In that records are not inserting into the main table.But im getting print msg as inserted sucessfully.Im not understanding what's the issue going on here.Plz help me with this issue.
Here is my procedure code:
In my procedure im declaring the cursor which has flag value 'N' in SD_CHANGE_TMP table.After that i have done some calculations.Based on these values im inserting into main table i.e SD_CHANGE table.Procedure is executed successfully.And im not getting syntax errors.But data is not inserted after calling the procedure.Plz help me out.....
CREATE PROCEDURE HIS_CHANGE()
BEGIN
declare v_CHANGE_ID DECIMAL(25,0);
declare v_MODIFIED_DATE DATETIME;
declare v_PLAN_START_DATE DATETIME;
declare v_PLAN_END_DATE DATETIME;
declare v_ACTUAL_END_DATE DATETIME;
declare v_ACTUAL_START_DATE DATETIME;
declare v_APPROVAL_STATUS_ID DECIMAL(25,0);
declare v_ASSIGNED_TO_ID DECIMAL(25,0);
declare v_CHANGE_OWNER_ID DECIMAL(25,0);
declare v_CLOSURE_CODE_ID DECIMAL(25,0);
declare v_CREATE_DATE DATETIME;
declare v_CREATED_BY_ID DECIMAL(25,0);
declare v_DESCRIPTION VARCHAR(4000);
declare v_ENHANCEMENT_AUDIT VARCHAR(30);
declare v_HOST_ID DECIMAL(25,0);
declare v_HOST_NAME VARCHAR(255);
declare v_IMPACT VARCHAR(64);
declare v_SEVERITY_ID DECIMAL(25,0);
declare v_PRODUCT_CTI_ID DECIMAL(25,0);
declare v_OPERATIONAL_CTI_ID DECIMAL(25,0);
declare v_RAISED_LOCATION_ID DECIMAL(25,0);
declare v_STATUS_ID DECIMAL(25,0);
declare v_SLA_ID DECIMAL(25,0);
declare v_SUMMARY VARCHAR(128);
declare v_CHANGE_KEY VARCHAR(128);
declare v_USERS_AFFECTED VARCHAR(60);
declare v_A_SRC DECIMAL(25,0);
declare v_TICKET_TYPE_ID DECIMAL(25,0);
declare v_FLAG CHAR(1) ;
declare v_A_CREATION_TIME TIMESTAMP;
declare temp DOUBLE;
declare t_his VARCHAR(2);
declare t_max_d DATETIME;
declare t_count DOUBLE DEFAULT 0;
declare t_hosti DOUBLE;
DECLARE duplicate_key INT DEFAULT 0;
declare done int default 0;
declare done1 int default 0;
declare done2 int default 0;
declare no_data int default 0;
declare ERROR int default 0;
DECLARE CHG_CUR CURSOR FOR SELECT * FROM SD_CHANGE_TMP WHERE FLAG = 'N' ORDER BY CHANGE_ID,MODIFIED_DATE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DECLARE continue HANDLER FOR 1062 SET duplicate_key=1;
DECLARE continue HANDLER for 1328 SET no_data = 1;
DECLARE continue HANDLER for SQLEXCEPTION SET ERROR = 1;
SELECT "OPENING THE CURSOR...";
OPEN CHG_CUR;
SELECT "before the loop....";
LOOP1:LOOP
outer_block:BEGIN
FETCH CHG_CUR INTO v_CHANGE_ID,v_MODIFIED_DATE,v_PLAN_START_DATE,v_PLAN_END_DATE,v_ACTUAL_END_DATE,v_ACTUAL_START_DATE,
v_APPROVAL_STATUS_ID,v_ASSIGNED_TO_ID,v_CHANGE_OWNER_ID,v_CLOSURE_CODE_ID,v_CREATE_DATE,v_CREATED_BY_ID,v_DESCRIPTION,
v_ENHANCEMENT_AUDIT,v_HOST_ID,v_HOST_NAME,v_IMPACT,v_SEVERITY_ID,v_PRODUCT_CTI_ID,v_OPERATIONAL_CTI_ID,v_RAISED_LOCATION_ID,
v_STATUS_ID,v_SLA_ID,v_SUMMARY,v_CHANGE_KEY,v_USERS_AFFECTED,v_A_SRC,v_TICKET_TYPE_ID,v_FLAG,v_A_CREATION_TIME;
select "Inside the loop";
inner_block:begin
BEGIN
SELECT COUNT(*) INTO temp FROM SD_CHANGE WHERE CHANGE_ID = v_CHANGE_ID;
IF temp = 0 THEN
select temp;
SET t_his = 'Y';
ELSE
SELECT MAX(MODIFIED_DATE) INTO t_max_d FROM SD_CHANGE WHERE CHANGE_ID= v_CHANGE_ID;
IF v_MODIFIED_DATE > t_max_d
THEN
select temp;
SET t_his = 'Y';
UPDATE SD_CHANGE SET HIS_FLAG = 'N' WHERE HIS_FLAG = 'Y' AND CHANGE_ID = v_CHANGE_ID;
ELSE
SET t_his = 'N';
END IF;
END IF;
END;
BEGIN
select concat('Inserting into SD_CHANGE table');
insert into SD_CHANGE(CHANGE_ID,MODIFIED_DATE,PLAN_START_DATE,PLAN_END_DATE,ACTUAL_END_DATE,ACTUAL_START_DATE,APPROVAL_STATUS_ID,ASSIGNED_TO_ID,CHANGE_OWNER_ID,
CLOSURE_CODE_ID,CREATE_DATE,CREATED_BY_ID,DESCRIPTION,ENHANCEMENT_AUDIT,HOST_ID,IMPACT,SEVERITY_ID,PRODUCT_CTI_ID,
OPERATIONAL_CTI_ID,RAISED_LOCATION_ID,STATUS_ID,SLA_ID,SUMMARY,CHANGE_KEY,HIS_FLAG,USERS_AFFECTED,A_SRC,FLAG,A_CREATION_TIME,TICKET_TYPE_ID)
values(v_CHANGE_ID,v_MODIFIED_DATE,v_PLAN_START_DATE,v_PLAN_END_DATE,v_ACTUAL_END_DATE,v_ACTUAL_START_DATE,v_APPROVAL_STATUS_ID,v_ASSIGNED_TO_ID,v_CHANGE_OWNER_ID,
v_CLOSURE_CODE_ID,v_CREATE_DATE,v_CREATED_BY_ID,v_DESCRIPTION,v_ENHANCEMENT_AUDIT,v_HOST_ID,v_IMPACT,v_SEVERITY_ID,v_PRODUCT_CTI_ID,
v_OPERATIONAL_CTI_ID,v_RAISED_LOCATION_ID,v_STATUS_ID,v_SLA_ID,v_SUMMARY,v_CHANGE_KEY,t_his,v_USERS_AFFECTED,v_A_SRC,'N',CURRENT_TIMESTAMP,v_TICKET_TYPE_ID);
SELECT CONCAT('Inserted Successfully 1 ',v_change_id) as "Result";
COMMIT;
UPDATE SD_CHANGE_TMP SET FLAG = 'Y' WHERE CHANGE_ID = v_CHANGE_ID AND MODIFIED_DATE = v_MODIFIED_DATE;
IF duplicate_key=1 then
begin
select CONCAT('Rejected id and last mod time ',v_change_id, v_modified_date);
UPDATE SD_CHANGE_TMP SET FLAG = 'D' WHERE CHANGE_ID = v_CHANGE_ID AND MODIFIED_DATE = v_MODIFIED_DATE;
end;
else
SELECT CONCAT('Inserted Successfully 2 ',v_change_id) as "Result";
END IF;
SET duplicate_key=0;
END;
if no_data=1 then
leave loop1;
end if;
END inner_block;
IF (done=1) then
leave loop1;
end if;
end outer_block;
select concat('loop ending.....');
IF(t_count = 1000)
THEN
COMMIT;
select t_count;
SET t_count = 0;
ELSE
SET t_count = t_count+1;
END IF;
END LOOP LOOP1;
CLOSE CHG_CUR;
select concat('close the cursor....');
UPDATE TIMELOG SET ETIME = CURRENT_TIMESTAMP WHERE PROCNAME = 'SD_CHANGE';
COMMIT;
END;
IDFMA - Insufficient Data For Meaningful Answer
You have not provided nearly enough information for us to help you. Write up a clear
description of your issue, provide structures and sample data etc...
delimiter ;
drop procedure if exists insert_sd_change;
delimiter #
-- took a wild stab at your datatypes
create procedure insert_sd_change
(
in p_change_id int unsigned,
in p_modified_date datetime,
in p_plan_start_date datetime,
in p_plan_end_date datetime,
in p_actual_end_date datetime,
in p_actual_start_date datetime,
in p_approval_status_id tinyint unsigned,
in p_assigned_to_id int unsigned,
in p_change_owner_id int unsigned,
in p_closure_code_id tinyint unsigned,
in p_create_date datetime,
in p_created_by_id int unsigned,
in p_description varchar(255),
in p_enhancement_audit tinyint unsigned,
in p_host_id int unsigned,
in p_impact varchar(255),
in p_severity_id int unsigned,
in p_product_cti_id int unsigned,
in p_operational_cti_id int unsigned,
in p_raised_location_id int unsigned,
in p_status_id tinyint unsigned,
in p_sla_id smallint unsigned,
in p_summary varchar(255),
in p_change_key int unsigned,
in p_his_flag tinyint unsigned,
in p_users_affected int unsigned,
in p_a_src varchar(255),
in p_ticket_type_id tinyint unsigned
)
proc_main:begin
-- p_ prefix for parameters, v_ prefix for local variables !
declare v_duplicate_key tinyint unsigned default 0;
if not exists (select 1 from sd_change where change_id = p_change_id and modified_date = p_modified_date) then
insert into sd_change
(
change_id, modified_date, plan_start_date, plan_end_date, actual_end_date, actual_start_date,
approval_status_id, assigned_to_id, change_owner_id, closure_code_id, create_date, created_by_id,
description, enhancement_audit, host_id, impact, severity_id, product_cti_id, operational_cti_id,
raised_location_id, status_id, sla_id, summary, change_key, his_flag, users_affected, a_src,
flag, a_creation_time, ticket_type_id
)
values
(
p_change_id, p_modified_date, p_plan_start_date, p_plan_end_date, p_actual_end_date, p_actual_start_date,
p_approval_status_id, p_assigned_to_id, p_change_owner_id, p_closure_code_id, p_create_date, p_created_by_id,
p_description, p_enhancement_audit, p_host_id, p_impact, p_severity_id, p_product_cti_id, p_operational_cti_id,
p_raised_location_id, p_status_id, p_sla_id, p_summary, p_change_key, p_his_flag, p_users_affected, p_a_src,
'N', now(), p_ticket_type_id
);
update sd_change_tmp set flag = 'Y' where change_id = p_change_id and modified_date = p_modified_date;
else
set v_duplicate_key = 1;
update sd_change_tmp set flag = 'D' where change_id = p_change_id and modified_date = p_modified_date;
end if;
end proc_main#
delimiter ;
call insert_sd_change(...);