Firdaus Archive

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

MySQL LAST_INSERT_ID() how it works at multi-users environment

Tidak ada komentar

 Untuk mengambil data id yang terakhir di insertkan bisa memakai fungsi berikut

LAST_INSERT_ID()

LAST_INSERT_ID() gives you the last autogenerated id on the connection you execute it on, it does not return the last insert id globally produced by the MySQL server.


sauce : https://stackoverflow.com/questions/5835677/last-insert-id-how-it-works-at-multi-users-environment

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.