Sunday, June 30, 2013

select into

when developers need to create tables that have the same column definition as a table that already exists or they might need to create a table based on the results of a query. In either case, you can use the SELECT INTO command to create a new table.
By adding the INTO clause to a SELECT statement after the SELECT list, SQL Server creates the table name in the INTO clause, using the results if the SELECT, if the table does not already exist. If the table already exists, SQL Server returns an exception.
To create a table that has the same columns and data as another table already in the system, use SELECT INTO with SELECT*. The following query creates a table called Address 2 from the data in the Address table:

SELECT *                                                                                                                                                                                                             INTO person2 
FROM Person. person

SELECT INTO in commonly used in stored procedures to create local temporary tables. Instead of using the CREATE TABLE syntax and inserting the data into the temporary table, developers can use the SELECT INTO command to do both operations. The following query creates a local temporary table called # Female Employees that contains all employees IDs for female employees in the Employee table:

SELECT EmployeeId                                                                                                                                                                                    INTO # FemaleEmployees                                                                                                                                                                             FROM HumanResources. Employee                                                                                                                                                  WHERE Gender = ‘F’

select into
select into table
sql select into
sql server select into
tsql select into
select into temp table
select into sql
select into sql server
t-sql select into
select insert into
sql select into temp table
sql select into variable

1 comment:

  1. Nice work Randheer....Really informative and benificial...keep up the good work...looking forward to mor of your posts :)