---
title: "How can I create backup script for selected tables in Oracle?"  
description: "How can I create backup script for selected tables in Oracle?"  
author: "Utpal Vishwas"  
published: 2023-07-17  
updated: 2023-07-18  
canonical: https://www.mindstick.com/forum/159136/how-can-i-create-backup-script-for-selected-tables-in-oracle  
category: "oracle"  
tags: ["database backup", "database table", "oracle"]  
reading_time: 2 minutes  

---

# How can I create backup script for selected tables in Oracle?

How can I create [backup](https://www.mindstick.com/interview/1205/what-is-the-command-to-take-backup-and-restore-for-sharepoint-site) [script](https://www.mindstick.com/interview/477/how-can-i-execute-a-php-script-using-command-line) for selected [tables](https://www.mindstick.com/articles/336597/introduction-of-html-tables-for-web-development) in [Oracle](https://www.mindstick.com/articles/13016/alter-table-statement-or-command-in-oracle)?

## Replies

### Reply by Aryan Kumar

Sure, here is how you can create a backup script for selected tables in Oracle:

SQL

```plaintext
-- Create a backup script for selected tables.

SET SERVEROUTPUT ON;

DECLARE
  v_table_name VARCHAR2(30);
BEGIN
  -- Loop through the list of tables and create a backup for each one.
  FOR v_table_name IN ('table1', 'table2', 'table3') LOOP
    dbms_output.put_line('Backing up table ' || v_table_name);
    EXECUTE IMMEDIATE 'CREATE TABLE BAK_' || v_table_name || ' AS SELECT * FROM ' || v_table_name;
  END LOOP;
END;
/
```

This script will create a backup for the tables `table1`, `table2`, and `table3`. The backup tables will be named `BAK_table1`, `BAK_table2`, and `BAK_table3`.

To run the script, you would save it as a text file and then execute it from SQL*Plus. For example, if you saved the script as* `backup_script.sql`*, you would run it by typing the following command into SQL*Plus:

SQL

```plaintext
@backup_script.sql
```

This would create the backup tables for the specified tables.

Here is an explanation of the code in the script:

- The `SET SERVEROUTPUT ON` statement tells Oracle to display output from the script.
- The `DECLARE` statement declares a variable named `v_table_name` to hold the name of the current table.
- The `FOR` loop iterates through the list of tables and creates a backup for each one.
- The `dbms_output.put_line()` procedure displays a message to the console.
- The `EXECUTE IMMEDIATE` statement executes the specified SQL statement.
- The `END` statement marks the end of the `FOR` loop.


---

Original Source: https://www.mindstick.com/forum/159136/how-can-i-create-backup-script-for-selected-tables-in-oracle

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
