变量为空时如何执行

时间:2019-07-15 04:14:56

标签: mysql stored-procedures

如果变量为空(例如_religion),我想执行子插入查询

我在if语句中尝试了is not null!= ''!= ""<> ''<> "",但没有用。

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_import_employees`(
        _emp_id BIGINT, 
    _lastname VARCHAR(100), 
    _firstname VARCHAR(100), 
    _middlename VARCHAR(100), 
    _suffix VARCHAR(50),
     _email VARCHAR(150), 
     _birthdate DATETIME, 
     _civil_status VARCHAR(100),
     _gender VARCHAR(25), 
     _religion VARCHAR(255), 
     _height VARCHAR(20), 
     _weight VARCHAR(20),
     _taxcode VARCHAR(5), 
     _company VARCHAR(255), 
     _division VARCHAR(255),
     _department VARCHAR(255), 
     _function VARCHAR(255), 
     _workbase VARCHAR(255),
     _designation VARCHAR(255), 
     _jobgrade VARCHAR(100), 
     _datehired DATETIME, 
     _status VARCHAR(150),
     _salary DOUBLE,
     _sss VARCHAR(55),
     _tin VARCHAR(55),
     _philhealth VARCHAR(55),
     _pagibig VARCHAR(55),
     _createdby VARCHAR(55),
     _address VARCHAR(55),
     _city VARCHAR(55),
     _region VARCHAR(55),
     _phone1 VARCHAR(55),
     _emperson VARCHAR(55),
     _emaddress VARCHAR(55),
     _emcontact VARCHAR(55)
     )
BEGIN
    DECLARE __religion INT DEFAULT 0;
    DECLARE __division INT DEFAULT 0;
    DECLARE __company INT DEFAULT 0;
    DECLARE __department INT DEFAULT 0;
    DECLARE __function INT DEFAULT 0;
    DECLARE __workbase INT DEFAULT 0;
    DECLARE __designation INT DEFAULT 0;
    DECLARE __jobgrade INT DEFAULT 0;
    DECLARE __status INT DEFAULT 0;


    SELECT IFNULL(autoid, 0) INTO __religion FROM hris_religion WHERE LOWER(TRIM(religion)) = LOWER(TRIM(_religion)) LIMIT 1; 

        IF(__religion = 0 || __religion IS NULL && CHAR_LENGTH(_religion) > 0) THEN 
        INSERT INTO hris_religion(religion, createdby, date_created)
        VALUES(_religion, _createdby, CURDATE());
        SELECT IFNULL(autoid, 0) INTO __religion FROM hris_religion WHERE LOWER(TRIM(religion)) = LOWER(TRIM(_religion)) LIMIT 1; 
        END IF;




    SELECT IFNULL(business_id, 0) INTO __division FROM hris_business_unit WHERE LOWER(TRIM(business_unit)) = LOWER(TRIM(_division)) LIMIT 1;

        IF(__division = 0 || __division IS NULL && CHAR_LENGTH(_division) > 0) THEN
        INSERT INTO hris_business_unit(business_unit, createdby, date_created)
        VALUES(_division, _createdby, CURDATE());
        SELECT IFNULL(business_id, 0) INTO __division FROM hris_business_unit WHERE LOWER(TRIM(business_unit)) = LOWER(TRIM(_division)) LIMIT 1;
        END IF; 




    SELECT IFNULL(comp_id, 0) INTO __company FROM hris_company WHERE LOWER(TRIM(comp_name)) = LOWER(TRIM(_company)) LIMIT 1;

        IF(__company = 0 || __company IS NULL && CHAR_LENGTH(_company) > 0) THEN
        INSERT INTO hris_company(comp_name, createdby, date_created)
        VALUES(_company, _createdby, CURDATE());
        SELECT IFNULL(comp_id, 0) INTO __company FROM hris_company WHERE LOWER(TRIM(comp_name)) = LOWER(TRIM(_company)) LIMIT 1;
        END IF;



    SELECT IFNULL(dept_id, 0) INTO __department FROM hris_department WHERE LOWER(TRIM(department)) = LOWER(TRIM(_department )) LIMIT 1;

        IF(__department = 0 || __department IS NULL && CHAR_LENGTH(_department) > 0) THEN
        INSERT INTO hris_department(department, createdby, date_created)
        VALUES(_department, _createdby, CURDATE());
        SELECT IFNULL(dept_id, 0) INTO __department FROM hris_department WHERE LOWER(TRIM(department)) = LOWER(TRIM(_department )) LIMIT 1;
        END IF;



    SELECT IFNULL(func_id, 0) INTO __function FROM hris_function WHERE LOWER(TRIM(function_name)) = LOWER(TRIM(_function)) LIMIT 1;

        IF(__function = 0 || __function IS NULL && CHAR_LENGTH(_function) > 0) THEN
        INSERT INTO hris_function(function_name, createdby, date_created)
        VALUES(_function, _createdby, CURDATE());
        SELECT IFNULL(func_id, 0) INTO __function FROM hris_function WHERE LOWER(TRIM(function_name)) = LOWER(TRIM(_function)) LIMIT 1;
        END IF;



    SELECT IFNULL(work_base_id, 0) INTO __workbase FROM hris_work_base WHERE LOWER(TRIM(work_base)) = LOWER(TRIM(_workbase)) LIMIT 1;

        IF(__workbase = 0 || __workbase IS NULL && CHAR_LENGTH(_workbase) > 0) THEN
        INSERT INTO hris_work_base(work_base, createdby, date_created)
        VALUES(_workbase, _createdby, CURDATE());
        SELECT IFNULL(work_base_id, 0) INTO __workbase FROM hris_work_base WHERE LOWER(TRIM(work_base)) = LOWER(TRIM(_workbase)) LIMIT 1;
        END IF;



    SELECT IFNULL(autoid, 0) INTO __designation FROM hris_designation WHERE LOWER(TRIM(designation)) = LOWER(TRIM(_designation)) LIMIT 1;

        IF(__designation = 0 || __designation IS NULL && CHAR_LENGTH(_designation) > 0) THEN
        INSERT INTO hris_designation(designation, createdby, date_created)
        VALUES(_designation, _createdby, CURDATE());
        SELECT IFNULL(autoid, 0) INTO __designation FROM hris_designation WHERE LOWER(TRIM(designation)) = LOWER(TRIM(_designation)) LIMIT 1;
        END IF;



    SELECT IFNULL(autoid, 0) INTO __jobgrade FROM hris_job_grade WHERE LOWER(TRIM(job_id)) = LOWER(TRIM(_jobgrade)) LIMIT 1;

        IF(__jobgrade = 0 || __jobgrade IS NULL && CHAR_LENGTH(_jobgrade) > 0) THEN
        INSERT INTO hris_job_grade(job_id, createdby, date_created)
        VALUES(_jobgrade, _createdby, CURDATE());
        SELECT IFNULL(autoid, 0) INTO __jobgrade FROM hris_job_grade WHERE LOWER(TRIM(job_id)) = LOWER(TRIM(_jobgrade)) LIMIT 1;
        END IF;


    SELECT IFNULL(status_id, 0) INTO __status FROM hris_employee_status WHERE LOWER(TRIM(status_desc)) = LOWER(TRIM(_status)) LIMIT 1;

        IF(__status = 0 || __status IS NULL && CHAR_LENGTH(_status) > 0) THEN
        INSERT INTO hris_employee_status(status_desc, createdby, date_created)
        VALUES(_status, _createdby, CURDATE());
        SELECT IFNULL(status_id, 0) INTO __status FROM hris_employee_status WHERE LOWER(TRIM(status_desc)) = LOWER(TRIM(_status)) LIMIT 1;
        END IF;



    INSERT INTO hris_employee(emp_id,  lastname, firstname, middlename, suffix, email, birthdate, civil_status, gender, religion_id,
                    height, weight, taxcode, company_id, business_id, dept_id, function_id, work_base_id, designation_id,
                    job_grade_id, hired_date, status_id, salary_basis, salary_rate, sss, tin, philhealth, pagibig, 
                    paddress, pcity, pprovince, pphone1, emergency_contact_person, emergency_address, emergency_contactno)
                VALUES(_emp_id, _lastname, _firstname, _middlename, _suffix, _email, _birthdate, _civil_status, _gender,
                    __religion, _height, _weight, _taxcode, __company, __division, __department, __function, 
                    __workbase, __designation, _jobgrade, _datehired, __status, 'Monthly', _salary, _sss, _tin, _philhealth, _pagibig,
                    _address, _city, _region, _phone1, _emperson, _emaddress, _emcontact);

    SELECT IF(ROW_COUNT() > 0, _emp_id, 0) AS result;
    END$$

DELIMITER ;

0 个答案:

没有答案