Knowee
Questions
Features
Study Tools

Write a PL/pgSQL block using an explicit cursor that will transfer the record of account no, customer name and balance from the“account” table to the “branch_surat” table if the branch name is surat in the “account” table. Furthermore, delete the record fromthe “account” table whichever record transfers to the “branch_surat” table.Use the following tables:account (ano, customer_name, balance, branch_name)branch_surat (ano, customer_name, balance)Create tables with Primary Key, foreign key constraints in given schemas

Question

Write a PL/pgSQL block using an explicit cursor that will transfer the record of account no, customer name and balance from the“account” table to the “branch_surat” table if the branch name is surat in the “account” table. Furthermore, delete the record fromthe “account” table whichever record transfers to the “branch_surat” table.Use the following tables:account (ano, customer_name, balance, branch_name)branch_surat (ano, customer_name, balance)Create tables with Primary Key, foreign key constraints in given schemas

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a PL/pgSQL block that uses an explicit cursor to perform the operation you described:

DO $$
DECLARE
    cur CURSOR FOR SELECT ano, customer_name, balance FROM account WHERE branch_name = 'surat';
    rec RECORD;
BEGIN
    OPEN cur;
    LOOP
        FETCH cur INTO rec;
        EXIT WHEN NOT FOUND;
        
        INSERT INTO branch_surat (ano, customer_name, balance) VALUES (rec.ano, rec.customer_name, rec.balance);
        DELETE FROM account WHERE CURRENT OF cur;
    END LOOP;
    CLOSE cur;
END $$;

This block declares a cursor cur for the query that selects all records from the account table where branch_name is 'surat'. It then opens the cursor and enters a loop. In each iteration of the loop, it fetches the next record from the cursor into the rec record variable. If no more records are found, it exits the loop.

Then it inserts the fetched record into the branch_surat table and deletes the same record from the account table. After all records have been processed, it closes the cursor.

Please note that this code assumes that the ano field is the primary key in both tables, and that there are no foreign key constraints that would prevent the deletion of records from the account table. If such constraints exist, you will need to handle them appropriately in your code.

This problem has been solved

Similar Questions

Write PL/pgSQL using trigger for insertion of first_name, last_name, amount and payment_id into Customer_backup table whendeletion happens from Customer table considering below schema.Customer_backup (first_name, last_name, amount, payment_id)Customer (customer_id, payment_id, first_name, last_name, amount)Create tables with Primary Key, foreign key constraints in given schemas

Prepare a PL/pgSQL block that simulates an ATM transaction system.•If the withdrawal amount is less than or equal to 0, the block should display an "Invalid withdrawal amount" message.•If the withdrawal amount is greater than the account balance, the block should display an "Insufficient funds" message.•If the withdrawal amount is up to Rs. 20000, the block should perform a withdrawal and deduct the amount from the accountbalance.•If the withdrawal amount is up to Rs. 40000, the block should perform a withdrawal, deduct the amount, and apply a transactionfee 5%.•For withdrawal amounts greater than Rs. 40000, the block should perform a withdrawal, deduct the amount, and apply a highertransaction fee 18%.The block should also display the current account balance after each scenario

Create PL/pgSQL procedure for the increment of employees where in salary less than 35000 will get hike of 15% in their previoussalary and other will get 10% hike in their previous salary. Using following schema, Employees (id, name, department, salary)call the procedure by id and print employee’s name with their updated salary

Refer to the given schema. Write a query to create customer and branch tables with the constraints mentioned.

A) We have following relation orders(order_id,customer_id,order_date,amount). Create table with Primary Key, otherrelevant constraints in given schema.1) Find out the number of orders for each customer by customer_id and show only customer_id with number of orders above 5.2) Find out the total amount by order_id and order_date.B) Find the sum of a user-inserted number's first and last digits using PL/pgSQL function

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.