JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

"SELECT ... INTO" Statements

This section describes how to create a table and insert data rows with a special SELECT statement.

SQL Server supports a special SELECT statement with an INTO clause, referred as SELECT INTO statement sometimes. It combines SELECT, CREATE TABLE and INSERT operations in a single statement. This statement should be treated as an update statement from the JDBC point of view, since it will not return any result set.

In this section, I want to use this SELECT INTO statement to build a new table with data from the existing sample table Customer. The new table will be called, Profile, in my schema, Herong. Here is the Java program that execute this SELECT INTO statement:

/**
 * SelectInto.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class SelectInto {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang;"
        + "database=AdventureWorksLT");
      Statement sta = con.createStatement(); 

// creating new table and insert data in a single statement
      int count = sta.executeUpdate(
        "SELECT TOP 10 CustomerID UserID, FirstName, LastName,"
        + " ModifiedDate LastAccessDate"
        + " INTO Herong.Profile FROM SalesLT.Customer");
      System.out.println("Number of rows inserted: "+count);

// getting the data back
      ResultSet res = sta.executeQuery(
        "SELECT * FROM Herong.Profile");
      System.out.println("List of Profiles: "); 
      while (res.next()) {
         System.out.println(
           "  "+res.getInt("UserID")
           + ", "+res.getString("FirstName")
           + ", "+res.getString("LastName")
           + ", "+res.getDate("LastAccessDate"));
      }
      res.close();

      sta.close();
      con.close();        
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

The output confirms that the SELECT INTO statement worked correctly:

Number of rows inserted: 10
List of Profiles:
  1, Orlando, Gee, 2004-10-13
  2, Keith, Harris, 2004-10-13
  3, Donna, Carreras, 2004-10-13
  4, Janet, Gates, 2004-10-13
  5, Lucy, Harrington, 2004-10-13
  6, Rosmarie, Carroll, 2004-10-13
  7, Dominic, Gash, 2004-10-13
  10, Kathleen, Garza, 2004-10-13
  11, Katherine, Harding, 2004-10-13
  12, Johnny, Caprio, 2004-10-13

Note that the executeUpdate() method did return the number of rows inserted into the new table correctly.

Sections in This Chapter

"SELECT ... INTO" Statements

"INSERT INTO" Statements

"INSERT INTO" Statements with INDENTITY Columns

"UPDATE" Statements

"DELETE FROM" Statements

Dr. Herong Yang, updated in 2007
"SELECT ... INTO" Statements