Blog pribadi untuk curhat, share tentang script, software, kuliah, project IT dan seputar IT.

MySQL Stored Procedure : Create a procedure to insert and update table header and details in mysql

Tidak ada komentar
Stack Overflow answer powered by AI 
To create a stored procedure in MySQL that can be used to insert and update table header and details, you can follow this example:

DELIMITER //

CREATE PROCEDURE insert_update_table (IN p_header_value VARCHAR(50), IN p_detail_value VARCHAR(50))
BEGIN
    -- Check if the header already exists in the table
    IF EXISTS (SELECT * FROM your_table WHERE header_column = p_header_value) THEN
        -- Update the existing record
        UPDATE your_table SET detail_column = p_detail_value WHERE header_column = p_header_value;
    ELSE
        -- Insert a new record
        INSERT INTO your_table (header_column, detail_column) VALUES (p_header_value, p_detail_value);
    END IF;
END //

DELIMITER ;


In this example, replace your_table with the actual name of your table, header_column with the name of the column where you want to store the header value, and detail_column with the name of the column where you want to store the detail value. To use the procedure, you can call it like this: 

CALL insert_update_table('Header Value', 'Detail Value'); 

 This will either update the existing record with the provided header value and detail value if the header value already exists, or insert a new record with the provided values if the header value doesn't exist in the table.

Tidak ada komentar :

Posting Komentar