Вы находитесь на странице: 1из 3

--------------------------------------------------------------------------Simple

Query----------------------------------------------------------------------------------

CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_cusomer`()


begin
START TRANSACTION;
SAVEPOINT sp1;
delete from customer_stg /*avoid duplicate value*/
where Customer_name in(select Customer_name from customer)
and
City in(select City from customer);
insert into
customer(Customer_name,Customer_legal_name,Customer_short_name,Customer_nick_name,
Customer_category_id,Parent_customer_id,Institute_starting_year,Number_of_students,
Address_line1,Address_line2,City,
State,Country,Pincode,Contact_phone_no1,Contact_phone_no2,
Contact_cell_no1,Contact_cell_no2,Contact_email_id,Primary_contact_name,
Primary_contact_phone_no,Primary_email_id,Primary_contact_cell_no,web_url,
Remarks,Primary_rep_id,Secondary_rep_id,Customer_start_date,Customer_end_date,
Is_display,Display_seq_id,Is_active)
select /*insert value*/
Customer_name,Customer_legal_name,Customer_short_name,Customer_nick_name,
Customer_category_id,Parent_customer_id,
Institute_starting_year,Number_of_students,Address_line1,Address_line2,City,
State,Country,Pincode,Contact_phone_no1,Contact_phone_no2,Contact_cell_no1,Contact_cell_
no2,
Contact_email_id,Primary_contact_name,Primary_contact_phone_no,Primary_email_id,
Primary_contact_cell_no,web_url,Remarks,Primary_rep_id,Secondary_rep_id,
Customer_start_date,Customer_end_date,Is_display,Display_seq_id,Is_active from
customer_stg;
ROLLBACK TO SAVEPOINT sp1;
Truncate table customer_stg;
/*Truncate table customer_stg*/
COMMIT;
End$$

-------------------------------------------------------complex-------------------------------------

CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_question_details`()


begin
DECLARE a INT;
SET a = 1;
START TRANSACTION;
SAVEPOINT sp1;
/*insert value to question_details */

while (a<1000) do
insert into question_details
(Question_id,Question_details,Question_answer,Question_answer_details)
select question_id,option_a,option_a_ans_y_n,reason_for_answer_a from question_stg
where question_id=a;
insert into question_details
(Question_id,Question_details,Question_answer,Question_answer_details)
select question_id,option_b,option_b_ans_y_n,reason_for_answer_b from question_stg
where question_id=a;
insert into question_details
(Question_id,Question_details,Question_answer,Question_answer_details)
select question_id,option_c,option_c_ans_y_n,reason_for_answer_c from question_stg
where question_id=a;
insert into question_details
(Question_id,Question_details,Question_answer,Question_answer_details)
select question_id,option_d,option_d_ans_y_n,reason_for_answer_d from question_stg
where question_id=a;
insert into question_details
(Question_id,Question_details,Question_answer,Question_answer_details)
select question_id,option_e,option_e_ans_y_n,reason_for_answer_e from question_stg
where question_id=a;
SET a=a+1;
END while;
ROLLBACK TO SAVEPOINT sp1;
commit;
END$$

----------------------------------------complex2-----------------------------------------
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_student`()
begin
START TRANSACTION;
SAVEPOINT sp1;
delete from students_stg /*Avoid duplicate value*/
where student_email_id in(select student_email_id from students);
update students_stg
set
is_global_student=0
where is_global_student!="y";
update students_stg
set
is_global_student=1
where is_global_student="y";
insert into
students(is_global_student,student_first_name,student_last_name,student_email_id, /*insert
value*/
student_alt_email_id,student_cell_no,student_phone_no,customer_id,enrollment_no,course_type
_id,study_year,
teacher_id,accadamy_year,parents_first_name,parents_last_name,parents_email_id,sales_rep_i
d,Is_Active)
select
is_global_student,student_first_name,student_last_name,student_email_id,
student_alt_email_id,student_cell_no,student_phone_no,customer_id,enrollment_no,course_type
_id,study_year,
teacher_id,accadamy_year,parents_first_name,parents_last_name,parents_email_id,sales_rep_i
d,Is_Active
from students_stg;
update students
set
parents_student_id= student_id;
insert into user_master (USERS_ID,USER_EMAIL_ID,USER_PASSWORD) /*insert value to user
master for student*/
select lpad(student_id,6,'0'),student_email_id,md5(student_first_name)
from students where student_email_id not in (select USER_EMAIL_ID from user_master);
update user_master,user_role
set /*update the role_id and type_tid*/
user_master.USER_ROLE_ID=user_role.USER_ROLE_ID
where user_role.USER_ROLE_NAME like "go%" and user_master.USER_EMAIL_ID in
(select student_email_id from students where is_global_student=1);
update user_master,user_role
set
user_master.USER_ROLE_ID=user_role.USER_ROLE_ID
where user_role.USER_ROLE_NAME like "stu%" and user_master.USER_EMAIL_ID in
(select student_email_id from students where is_global_student=0);
update user_master,user_type
set
user_master.USER_TYPE_ID=user_type.USER_TYPE_ID
where user_type.USER_TYPE_NAME like "%stu%" and user_master.USER_EMAIL_ID in( select
student_email_id
from students);
update user_master,user_role
set
user_master.USERS_ID=concat(user_role.USER_ROLE_CD,user_master.USERS_ID)
where user_role.USER_ROLE_ID=user_master.USER_ROLE_ID and
user_master.USER_EMAIL_ID in(select student_email_id from students_stg);
insert into user_master (USERS_ID,USER_EMAIL_ID,USER_PASSWORD) /*insert value to
user master for parent*/
select lpad(parents_student_id,6,'0'),parents_email_id,md5(parents_first_name)
from students where parents_email_id not in (select USER_EMAIL_ID from user_master);
update user_master,user_role
set
user_master.USER_ROLE_ID=user_role.USER_ROLE_ID
where user_role.USER_ROLE_NAME like "par%" and user_master.USER_EMAIL_ID in
(select parents_email_id from students );
update user_master,user_type
set /*update the role_id and type_tid*/
user_master.USER_TYPE_ID=user_type.USER_TYPE_ID
where user_type.USER_TYPE_NAME like "%aren%" and user_master.USER_EMAIL_ID
in( select parents_email_id
from students);
update user_master,user_role
set
user_master.USERS_ID=concat(user_role.USER_ROLE_CD,user_master.USERS_ID)
where user_role.USER_ROLE_ID=user_master.USER_ROLE_ID and
user_master.USER_EMAIL_ID in(select parents_email_id from students_stg);
ROLLBACK TO SAVEPOINT sp1;
Truncate table students_stg;
/*Truncate table students_stg*/
COMMIT;
End$$

Вам также может понравиться