Firdaus Archive

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

MySQL Stored Procedure : Insert Data in Header and Detail within 1 Transaction

Tidak ada komentar

This is Generated by ChatGPT 2023-11-01

To insert data into a single-row header table and multiple-row detail table using a stored procedure in MySQL, you can follow these steps. In this example, we'll create two tables: header and detail, with a one-to-many relationship, and then create a stored procedure to insert data into both tables.

Assuming you have the following tables:

CREATE TABLE header (

    header_id INT AUTO_INCREMENT PRIMARY KEY,

    header_data VARCHAR(255)

);


CREATE TABLE detail (

    detail_id INT AUTO_INCREMENT PRIMARY KEY,

    header_id INT,

    detail_data VARCHAR(255),

    FOREIGN KEY (header_id) REFERENCES header(header_id)

);

Here's a stored procedure to insert data into both tables:

DELIMITER $$


CREATE PROCEDURE InsertHeaderAndDetails(

    IN headerData VARCHAR(255),

    IN detailDataArray JSON

)

BEGIN

    DECLARE done INT DEFAULT FALSE;

    DECLARE detailValue VARCHAR(255);

    DECLARE cur CURSOR FOR SELECT value FROM JSON_TABLE(detailDataArray, '$[*]' COLUMNS(value VARCHAR(255) PATH '$'));

    

    -- Insert data into the header table

    INSERT INTO header (header_data) VALUES (headerData);

    SET @lastHeaderID = LAST_INSERT_ID(); -- Get the last inserted header ID


    -- Insert data into the detail table

    OPEN cur;

    read_loop: LOOP

        FETCH cur INTO detailValue;

        IF done THEN

            LEAVE read_loop;

        END IF;

        INSERT INTO detail (header_id, detail_data) VALUES (@lastHeaderID, detailValue);

    END LOOP;

    CLOSE cur;

END $$


DELIMITER ;

 

This stored procedure takes two parameters:


headerData: The data to be inserted into the header table.

detailDataArray: A JSON array containing the data to be inserted into the detail table.

You can call the stored procedure like this:

CALL InsertHeaderAndDetails('Header Data', '["Detail 1", "Detail 2", "Detail 3"]');

This will insert a new row into the header table and multiple rows into the detail table with the corresponding header_id.

Please adjust the data types, table structures, and stored procedure parameters as needed to match your specific requirements. Additionally, ensure that you have proper error handling and validation in your production code for real-world scenarios.



 

 

 

Radmin VPN Alternative

Tidak ada komentar

ZeroTier

Freelan

Hamachi

sc https://www.saashub.com/zerotier-one-alternatives https://www.saashub.com/radmin-vpn-alternatives

Log USB insert / Eject Using USBLogView by nirsoft

Tidak ada komentar

USBLogView is a small utility that runs in the background and records the details of any USB device that is plugged or unplugged into your system. For every log line created by USBLogView, the following information is displayed: Event Type (Plug/Unplug), Event Time, Device Name, Description, Device Type, Drive Letter (For storage devices), Serial Number (Only for some types of devices), Vendor ID, Product ID, Vendor Name, Product Name, and more...

You can easily select one or more log records and then export them into csv/tab-delimited/xml/html file.


To Download Click This link

http://www.nirsoft.net/utils/usb_log_view.html

or This Direct Link

http://www.nirsoft.net/utils/usblogview.zip

Visual Studio Code - Removing Lines Containing criteria

Tidak ada komentar

This method dont need any Extension

1. Ctrl+F to open find.

2. Paste your string.

3. Alt+Enter to select all of the instances of the string on the page.

4. Ctrl+L to broaden the selection to the entire line of each instance on the page.

5. Delete/Backspace to remove those lines.


This method u need this extension https://marketplace.visualstudio.com/items?itemName=qcz.text-power-tools


Even its slow but its just an alternative in case u were too lazy to do the 1st method

[Fix Error] Kode Error 0x0000011b Windows 10 Sharing Printer

Tidak ada komentar

Kode error 0x0000011b muncul saat ingin melakukan sharing printer pada windows 10. Error tersebut disebabkan oleh update terbaru windows 10 2021-09 Cumulative Update for Windows 10 Version 21H1 for x64-based Systems (KB5005565).

Cara mengatasi problem tersebut yaitu :


Buka Windows Registry Editor (regedit)

Kemudian arahkan ke HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print

Kemudian buat DWORD-32 baru dengan nama RpcAuthnLevelPrivacyEnabled set Value Data : 0

Selanjutnya Restart PC / Laptop

Error 0x0000011b akan hilang dan sharing printer bisa digunakan kembali.


source : https://pdsi.unisayogya.ac.id/fix-error-kode-error-0x0000011b-windows-10-sharing-printer/