sql select into statement

SQL Advance

homeBack

Next

SQL SELECT INTO Statement

  • The SELECT INTO statement selects data from one table and inserts it into a different table. 
    The SELECT INTO statement is most often used to create backup copies of tables.

  • SQL SELECT INTO Syntax

    SELECT * 
    INTO new_table_name [IN externaldatabase]
    FROM old_tablename
     ”

    OR 

    SELECT column_name(s)
    INTO new_table_name [IN externaldatabase]
    FROM old_tablename
     ”

  • SQL SELECT INTO EXAMPLE

    Make a Backup Copy – Now we want to make an exact copy of the data in our “emp” table. 
    We use the following SQL statement: 

    SELECT *
    INTO emp_Backup
    FROM emp

    We can also use the IN clause to copy the table into another database: 

    SELECT *
    INTO emp_Backup IN ‘Backup.mdb’
    FROM emp

    We can also copy only a few fields into the new table: 

    SELECT Name2,Name1
    INTO emp_Backup
    FROM emp

  • SQL SELECT INTO EXAMPLE – With a WHERE Clause

    We can also add a WHERE clause. 
    The following SQL statement creates a “emp_Backup” table with only the persons who lives in the city “London”: 

    SELECT Name2,Name1
    INTO emp_Backup
    FROM emp
    WHERE City=’London’
     ”

  • SQL SELECT INTO EXAMPLE – Joined Tables

    Selecting data from more than one table is also possible. 
    The following example creates a “emp_job_Backup” table contains data from the two tables “emp” and “job”: 

    SELECT emp.Name2,job.Job_no INTO emp_job_Backup
    FROM emp
    INNER JOIN job
    ON emp.S.no=job.S.no
     ”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s