
USE demo;

DROP PROCEDURE IF EXISTS demo.p_dog_save_command_category;

DELIMITER //
CREATE PROCEDURE demo.p_dog_save_command_category (
    IN a_comment VARCHAR(500),
	IN a_guid BINARY(36),
    IN a_id_user INT,
    IN a_debug BIT
)
BEGIN
    DECLARE v_can_admin BIT;
    DECLARE v_can_create BIT;
	DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_id_access_level_edit INT;
    DECLARE v_id_change_set INT;
    DECLARE v_id_permission_dog_new INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_time_start TIMESTAMP(6);
    
    DECLARE exit handler for SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            @sqlstate = RETURNED_SQLSTATE
            , @errno = MYSQL_ERRNO
            , @text = MESSAGE_TEXT
		;
        
        ROLLBACK;
        
		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error (
			  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
			, id_type INT NULL
			, code VARCHAR(100)
			, msg TEXT NOT NULL
		);

        INSERT INTO tmp_Msg_Error (
			  id_type
			, code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM demo.CORE_Msg_Error_Type MET
        WHERE MET.code = 'MYSQL_ERROR'
		;

        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error t_ERROR
        INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;

		DROP TABLE IF EXISTS tmp_Msg_Error;
    END;
    
    SET SESSION group_concat_max_len=15000;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_id_type_error_bad_data := (SELECT ERROR_TYPE.id_type FROM demo.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_bad_data LIMIT 1);
    SET v_id_permission_dog_new := (SELECT PERMISSION.id_permission FROM demo.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_CREATE' LIMIT 1);
    SET v_id_access_level_edit := (SELECT ACCESS_LEVEL.id_access_level FROM demo.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'EDIT' LIMIT 1);
    
    CALL demo.p_core_validate_guid ( a_guid );
    
    DROP TABLE IF EXISTS tmp_Command_Category_Copy;
    DROP TABLE IF EXISTS tmp_Command_Category;
    
    CREATE TEMPORARY TABLE tmp_Command_Category (
          id_temp INT
        , id_command_category INT
        , code VARCHAR(100)
        , name VARCHAR(250)
        , active BIT
        , is_new BIT
        , name_error VARCHAR(250)
    );

    CREATE TEMPORARY TABLE tmp_Command_Category_Copy (
          id_temp INT
        , id_command_category INT
        , code VARCHAR(100)
        , name VARCHAR(250)
        , active BIT
        , is_new BIT
        , name_error VARCHAR(250)
    );
        
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error (
		  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT NULL
        , code VARCHAR(100)
        , msg TEXT NOT NULL
	);
        
    
    -- Get data from Temp table
    INSERT INTO tmp_Command_Category (
		  id_temp
        , id_command_category
        , code
        , name
        , active
        , is_new
	)
    SELECT 
		  COMMAND_CATEGORY_T.id_temp
		, COMMAND_CATEGORY_T.id_command_category
        , COALESCE(
              COMMAND_CATEGORY_T.code
            , COMMAND_CATEGORY.code
        ) AS code
        , COALESCE(
              COMMAND_CATEGORY_T.name
            , COMMAND_CATEGORY.name
        ) AS name
        , COALESCE(COMMAND_CATEGORY_T.active, 1) AS active
        , CASE WHEN IFNULL(COMMAND_CATEGORY_T.id_command_category, 0) < 1 THEN 1 ELSE 0 END AS is_new
	FROM demo.DOG_Command_Category_Temp COMMAND_CATEGORY_T
    LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON COMMAND_CATEGORY_T.id_command_category = COMMAND_CATEGORY.id_command_category
    WHERE COMMAND_CATEGORY_T.guid = a_guid
    ;

    IF a_debug = 1 THEN
        SELECT 'Command_Category_Temp records';
        SELECT * FROM tmp_Command_Category;
        SELECT COUNT(*) FROM tmp_Command_Category;
    END IF;

    -- Error names
    UPDATE tmp_Command_Category t_COMMAND_CATEGORY
    SET t_COMMAND_CATEGORY.name_error = COALESCE(t_COMMAND_CATEGORY.code, t_COMMAND_CATEGORY.name, '(No Command Category)')
    ;

    IF a_debug = 1 THEN
        SELECT 'After set name_error';
        SELECT * FROM tmp_Command_Category;
        SELECT COUNT(*) FROM tmp_Command_Category;
    END IF;

    -- Missing Command Category Ids
    UPDATE tmp_Command_Category t_COMMAND_CATEGORY
    LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON t_COMMAND_CATEGORY.code = COMMAND_CATEGORY.code
    SET 
        t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY.id_command_category
        , t_COMMAND_CATEGORY.is_new = 0
    WHERE 
            IFNULL(t_COMMAND_CATEGORY.id_command_category, 0) < 1
        AND NOT ISNULL(COMMAND_CATEGORY.id_command_category)
    ;

    IF a_debug = 1 THEN
        SELECT 'After set missing id_command_category';
        SELECT * FROM tmp_Command_Category;
        SELECT COUNT(*) FROM tmp_Command_Category;
    END IF;

    -- Validation
    -- Missing mandatory fields
    -- code
    IF EXISTS ( SELECT * FROM tmp_Command_Category t_COMMAND_CATEGORY WHERE t_COMMAND_CATEGORY.code IS NULL ) THEN
        INSERT INTO tmp_Msg_Error (
            id_type
            , code
            , msg
        )
        SELECT
            v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('The following Command Category(s) do not have a valid Code: ', GROUP_CONCAT(t_COMMAND_CATEGORY.name_error SEPARATOR ', ')) AS msg
        FROM tmp_Command_Category t_COMMAND_CATEGORY
        WHERE t_COMMAND_CATEGORY.code IS NULL
        ;
    END IF;
    -- name
    IF EXISTS ( SELECT * FROM tmp_Command_Category t_COMMAND_CATEGORY WHERE t_COMMAND_CATEGORY.name IS NULL ) THEN
        INSERT INTO tmp_Msg_Error (
            id_type
            , code
            , msg
        )
        SELECT
            v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('The following Command Category(s) do not have a valid Command: ', GROUP_CONCAT(t_COMMAND_CATEGORY.name_error SEPARATOR ', ')) AS msg
        FROM tmp_Command_Category t_COMMAND_CATEGORY
        WHERE t_COMMAND_CATEGORY.name IS NULL
        ;
    END IF;

    /*
    -- Duplicates
    INSERT INTO tmp_Command_Category_Copy (
        id_temp
        , id_command_category
        , id_dog
        , id_command
        , hand_signal_description
        , notes
        , active
        , is_new
        , name_error
        , does_have_valid_command_category_id
    )
    SELECT 
          t_COMMAND_CATEGORY.id_temp
        , t_COMMAND_CATEGORY.id_command_category
        , t_COMMAND_CATEGORY.id_dog
        , t_COMMAND_CATEGORY.id_command
        , t_COMMAND_CATEGORY.hand_signal_description
        , t_COMMAND_CATEGORY.notes
        , t_COMMAND_CATEGORY.active
        , t_COMMAND_CATEGORY.is_new
        , t_COMMAND_CATEGORY.name_error
        , t_COMMAND_CATEGORY.does_have_valid_command_category_id
    FROM tmp_Command_Category t_COMMAND_CATEGORY
    ;

    IF a_debug = 1 THEN
        SELECT COUNT(*) AS Count_Temp_Link FROM tmp_Command_Category;
        SELECT COUNT(*) AS Count_Temp_Link_Copy FROM tmp_Command_Category_Copy;

        WITH 
              Combined_Links AS (
                SELECT 
                      t_COMMAND_CATEGORY.id_command_category
                    , t_COMMAND_CATEGORY.id_dog
                    , t_COMMAND_CATEGORY.id_command
                    , t_COMMAND_CATEGORY.name_error
                    -- , 1 AS is_from_temporary_table
                    -- , 0 AS is_from_permanent_table
                FROM tmp_Command_Category t_COMMAND_CATEGORY
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY.id_command_category
                UNION
                SELECT 
                      COMMAND_CATEGORY.id_command_category
                    , COMMAND_CATEGORY.id_dog
                    , COMMAND_CATEGORY.id_command
                    , IFNULL(
                        t_COMMAND_CATEGORY_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_COMMAND_CATEGORY_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_COMMAND_CATEGORY_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                    -- , 0 AS is_from_temporary_table
                    -- , 1 AS is_from_permanent_table
                FROM demo.DOG_Command_Category COMMAND_CATEGORY 
                LEFT JOIN tmp_Command_Category_Copy t_COMMAND_CATEGORY_COPY ON COMMAND_CATEGORY.id_command_category = t_COMMAND_CATEGORY_COPY.id_command_category
                INNER JOIN demo.DOG_Dog DOG ON COMMAND_CATEGORY.id_dog = DOG.id_dog
                INNER JOIN demo.DOG_Command COMMAND ON COMMAND_CATEGORY.id_command = COMMAND.id_command
                WHERE 
                        NOT ISNULL(COMMAND_CATEGORY.id_command_category)
                    AND t_COMMAND_CATEGORY.does_have_valid_command_category_id = 0
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_command_category
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON COMBINED_LINK.id_command_category = COMMAND_CATEGORY.id_command_category
            )
        SELECT * -- AS count_duplicate_link_row_numbers
        FROM Duplicate_Link_Row_Numbers;
    END IF;

    IF EXISTS (
        WITH 
              Combined_Links AS (
                SELECT 
                      t_COMMAND_CATEGORY.id_command_category
                    , t_COMMAND_CATEGORY.id_dog
                    , t_COMMAND_CATEGORY.id_command
                    , t_COMMAND_CATEGORY.name_error
                    -- , 1 AS is_from_temporary_table
                    -- , 0 AS is_from_permanent_table
                FROM tmp_Command_Category t_COMMAND_CATEGORY
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY.id_command_category
                UNION
                SELECT 
                      COMMAND_CATEGORY.id_command_category
                    , COMMAND_CATEGORY.id_dog
                    , COMMAND_CATEGORY.id_command
                    , IFNULL(
                        t_COMMAND_CATEGORY_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_COMMAND_CATEGORY_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_COMMAND_CATEGORY_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                    -- , 0 AS is_from_temporary_table
                    -- , 1 AS is_from_permanent_table
                FROM demo.DOG_Command_Category COMMAND_CATEGORY 
                LEFT JOIN tmp_Command_Category_Copy t_COMMAND_CATEGORY_COPY ON COMMAND_CATEGORY.id_command_category = t_COMMAND_CATEGORY_COPY.id_command_category
                INNER JOIN demo.DOG_Dog DOG ON COMMAND_CATEGORY.id_dog = DOG.id_dog
                INNER JOIN demo.DOG_Command COMMAND ON COMMAND_CATEGORY.id_command = COMMAND.id_command
                WHERE 
                        ISNULL(t_COMMAND_CATEGORY_COPY.id_command_category)
                    AND NOT ISNULL(COMMAND_CATEGORY.id_command_category)
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_command_category
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON COMBINED_LINK.id_command_category = COMMAND_CATEGORY.id_command_category
            )
        SELECT
              v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('Attempt to create duplicate Command Categorys on: ', GROUP_CONCAT(DUPLICATE_LINK.name_error SEPARATOR ', ')) AS msg
        FROM Duplicate_Link_Row_Numbers DUPLICATE_LINK
        WHERE DUPLICATE_LINK.index_link_as_duplicate = 2
        GROUP BY DUPLICATE_LINK.id_dog, DUPLICATE_LINK.id_command
    ) THEN
        INSERT INTO tmp_Msg_Error (
              id_type
            , code
            , msg
        )
        WITH 
              Combined_Links AS (
                SELECT 
                      t_COMMAND_CATEGORY.id_command_category
                    , t_COMMAND_CATEGORY.id_dog
                    , t_COMMAND_CATEGORY.id_command
                    , t_COMMAND_CATEGORY.name_error
                    -- , 1 AS is_from_temporary_table
                    -- , 0 AS is_from_permanent_table
                FROM tmp_Command_Category t_COMMAND_CATEGORY
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON t_COMMAND_CATEGORY.id_command_category = COMMAND_CATEGORY.id_command_category
                UNION
                SELECT 
                      COMMAND_CATEGORY.id_command_category
                    , COMMAND_CATEGORY.id_dog
                    , COMMAND_CATEGORY.id_command
                    , IFNULL(
                        t_COMMAND_CATEGORY_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_COMMAND_CATEGORY_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_COMMAND_CATEGORY_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                    -- , 0 AS is_from_temporary_table
                    -- , 1 AS is_from_permanent_table
                FROM demo.DOG_Command_Category COMMAND_CATEGORY 
                LEFT JOIN tmp_Command_Category_Copy t_COMMAND_CATEGORY_COPY ON COMMAND_CATEGORY.id_command_category = t_COMMAND_CATEGORY_COPY.id_command_category
                INNER JOIN demo.DOG_Dog DOG ON COMMAND_CATEGORY.id_dog = DOG.id_dog
                INNER JOIN demo.DOG_Command COMMAND ON COMMAND_CATEGORY.id_command = COMMAND.id_command
                WHERE 
                        ISNULL(t_COMMAND_CATEGORY_COPY.id_command_category)
                    AND NOT ISNULL(COMMAND_CATEGORY.id_command_category)
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_command_category
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
                -- LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON COMBINED_LINK.id_command_category = COMMAND_CATEGORY.id_command_category
            )
        SELECT
              v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('Attempt to create duplicate Command Categorys on: ', GROUP_CONCAT(DUPLICATE_LINK.name_error SEPARATOR ', ')) AS msg
        FROM Duplicate_Link_Row_Numbers DUPLICATE_LINK
        -- LEFT JOIN demo.DOG_Dog DOG ON DUPLICATE_LINK.id_dog = DOG.id_dog
        -- LEFT JOIN demo.DOG_Command COMMAND ON DUPLICATE_LINK.id_command = COMMAND.id_command
        WHERE DUPLICATE_LINK.index_link_as_duplicate = 2
        GROUP BY DUPLICATE_LINK.id_dog, DUPLICATE_LINK.id_command
        ;
    END IF;
    */
    
    -- Permissions
    -- Can Create
	CALL demo.p_dog_calc_user(
          a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_id_user -- ids_user
        , '' -- a_auth0_ids_user
        , '' -- a_names_user
        , '' -- a_emails_user
        , 1 -- a_require_all_id_search_filters_met
        , 1 -- a_require_any_id_search_filters_met
        , 0 -- a_require_all_non_id_search_filters_met
        , 0 -- a_require_any_non_id_search_filters_met
        , v_id_permission_dog_new -- ids_permission
        , v_id_access_level_edit -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CU_T.has_access, 0)
    INTO
          v_can_create
    FROM demo.DOG_Calc_User_Temp CU_T
    WHERE CU_T.GUID = a_guid
    LIMIT 1
    ;
	
	CALL demo.p_dog_clear_calc_user(
		  a_guid
        , 0 -- a_debug
	);
    
    IF v_can_create = 0 THEN
		DELETE t_ME
        FROM tmp_Msg_Error t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error (
			  id_type
            , code
			, msg
		)
		VALUES (
			  v_id_type_error_no_permission
            , v_code_type_error_no_permission
			, 'You do not have permission to edit Commands.'
		)
        ;
    END IF;

    IF EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
    	IF a_debug = 1 THEN
	    	SELECT * from tmp_Command_Category;
	    END IF;

        DELETE FROM tmp_Command_Category;
    END IF;

    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		START TRANSACTION;
		
			INSERT INTO demo.DOG_Dog_Change_Set ( 
                  comment
                , id_user_updated_last_by
                , updated_last_on
            )
			VALUES ( 
                  a_comment
                , a_id_user
                , v_time_start
            )
			;
			
			SET v_id_change_set := LAST_INSERT_ID();
			
			UPDATE demo.DOG_Command_Category COMMAND_CATEGORY
			INNER JOIN tmp_Command_Category t_COMMAND_CATEGORY 
				ON  COMMAND_CATEGORY.id_command_category = t_COMMAND_CATEGORY.id_command_category
				AND t_COMMAND_CATEGORY.is_new = 0
			SET 
				  COMMAND_CATEGORY.code = t_COMMAND_CATEGORY.code
				, COMMAND_CATEGORY.name = t_COMMAND_CATEGORY.name
				, COMMAND_CATEGORY.active = t_COMMAND_CATEGORY.active
				, COMMAND_CATEGORY.id_change_set = v_id_change_set
			;
			
			INSERT INTO demo.DOG_Command_Category (
				  code
				, name
				, active
				, id_user_created_by
				, created_on
			)
			SELECT
				  t_COMMAND_CATEGORY.code                       AS code
				, t_COMMAND_CATEGORY.name                       AS name
				, t_COMMAND_CATEGORY.active                     AS active
				, a_id_user                                     AS created_by
				, v_time_start                                  AS created_on
			FROM tmp_Command_Category t_COMMAND_CATEGORY
			WHERE 
				    t_COMMAND_CATEGORY.is_new = 1
				AND t_COMMAND_CATEGORY.active = 1
			;
			
		COMMIT;
    END IF;
	
	START TRANSACTION;

		DELETE FROM demo.DOG_Command_Category_Temp
		WHERE GUID = a_guid
        ;
		
	COMMIT;
    
    -- Errors
    SELECT 
          t_ERROR.id_error
		, t_ERROR.id_type
        , t_ERROR.code
        , ERROR_TYPE.name
        , ERROR_TYPE.description
        , ERROR_TYPE.is_breaking_error
        , ERROR_TYPE.background_colour
        , ERROR_TYPE.text_colour
		, t_ERROR.msg
    FROM tmp_Msg_Error t_ERROR
	INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
	;
    
	IF a_debug = 1 THEN
		SELECT * FROM tmp_Command_Category;
	END IF;

    DROP TEMPORARY TABLE tmp_Command_Category;
    DROP TEMPORARY TABLE tmp_Msg_Error;
    
	IF a_debug = 1 THEN
		CALL demo.p_core_debug_timing_reporting ( v_time_start );
	END IF;
END //
DELIMITER ;



/*

'ripplesipplenippletippledipplekipple'
DELETE FROM demo.DOG_Command_Category WHERE id_command_category > 740;
* /
delete
from demo.DOG_Command_Category_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from demo.DOG_Command_Category_Temp
;
select COUNT(*)
from demo.DOG_Command_Category_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from demo.DOG_Command_Category
;
select COUNT(*)
from demo.DOG_Command_Category
;

INSERT INTO demo.DOG_Command_Category_Temp (
      id_command_category
    , code
    , name
    , active
    , guid
)
VALUES (
      -1 -- id_command_category
    , 'ACTIVITIES' -- code
    , 'A Sport Is Fun' -- name
    , 1 -- active
    , 'ripplesipplenippletippledipplekipple'
);

CALL demo.p_dog_save_command_category (
	'nipples'
    , 'ripplesipplenippletippledipplekipple'
    , 1
    , 1
);


select 
	*
    -- COUNT(*)
-- delete
from demo.DOG_Command_Category_Temp
;
select COUNT(*)
from demo.DOG_Command_Category_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from demo.DOG_Command_Category
;
select COUNT(*)
from demo.DOG_Command_Category
;

*/