
DELIMITER $$ 
DROP PROCEDURE IF EXISTS `<%= masterDbName %>`.`sp_pipeline` $$ 
CREATE PROCEDURE `<%= masterDbName %>`.`sp_pipeline` () SQL SECURITY INVOKER 
BEGIN

    SELECT 'USE sample_database;';

    SELECT '\nTRUNCATE TABLE sample_database.user;';
	IF EXISTS (SELECT 1 FROM sample_database.user) THEN
        SELECT 'INSERT INTO sample_database.user (id, name, cdate) VALUES';
        SELECT CONCAT(''
            , '(', CASE WHEN id IS NULL THEN 'NULL' ELSE id END
            , ', ', CASE WHEN name IS NULL THEN 'NULL' ELSE CONCAT('''', name, '''') END
            , ', UTC_TIMESTAMP'
            , CASE WHEN (SELECT MAX(id) FROM sample_database.user) = id THEN ');' ELSE '),' END
            )
        FROM sample_database.user
        ORDER BY id;
    END IF;

END $$

CALL `<%= masterDbName %>`.`sp_pipeline`();  

DROP PROCEDURE IF EXISTS `<%= masterDbName %>`.`sp_pipeline` $$ 
DELIMITER ;
