CREATE OR REPLACE FUNCTION update_store_temp()
RETURNS VOID AS $$
BEGIN
    UPDATE store_temp
    SET 
        address = store.address,
        gps = store.gps,
        lat = store.lat,
        suburb = store.suburb,
        google_link = store.google_link,
        town = store.town,
        location = store.location,
        channel_image = store.channel_image
    FROM store
    WHERE store_temp.store_code = store.store_code;
END;
$$
 LANGUAGE plpgsql;
 
select update_store_temp()


-- Step 1: Truncate the store table
TRUNCATE TABLE store;

-- Step 2: Insert data from store_temp into store
INSERT INTO store (
    name,
    address,
    client_id,
    created_by,
    updated_by,
    updated_at,
    created_at,
    gps,
    lat,
    region,
    channel,
    store_code,
    suburb,
    banner,
    google_link,
    town,
    location,
    file,
    row,
    sheetname,
    active_products,
    avail_old,
    visibility,
    channel_image,
    issue_data,
    oos,
    negative_stock,
    soh_no_sales,
    no_sales_100_days,
    seven_day_cover,
    total_soh,
    non_issues,
    optimul,
    products_count,
    availability,
    updated_issues
)
SELECT 
    name, 
    address, 
    client_id, 
    created_by, 
    updated_by, 
    updated_at, 
    created_at, 
    gps, 
    lat, 
    region, 
    channel, 
    store_code, 
    suburb, 
    banner, 
    google_link, 
    town, 
    location, 
    file, 
    row, 
    sheetname, 
    active_products, 
    avail_old, 
    visibility, 
    channel_image, 
    issue_data, 
    oos, 
    negative_stock, 
    soh_no_sales, 
    no_sales_100_days, 
    seven_day_cover, 
    total_soh, 
    non_issues, 
    optimul, 
    products_count, 
    availability, 
    updated_issues
FROM store_temp;