Solved

Create table as on SQL Server

Posted on 1998-10-29
3
217 Views
Last Modified: 2008-03-03
What's the syntax to do the following.

I do it on Oracle all the time!

create table temp as select * from other_table

This creates a new table temp with the same strucure and copies all the data into it.
Or I can do

create table temp as col1,col2,ect from other_table

Thanks...
0
Comment
Question by:fox_dogg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
Stefaan earned 20 total points
ID: 1090893
Hi,

You were very close there, the correct Statemet for SQL Server 6.5 would be :

SELECT Col1, Col2, Col3
INTO <newTableName>
FROM <OriginalTableName>

SELECT INTO is a two-step operation.  

The first step creates the new table, so you have to make sure that the user who is executing this statement has the CREATE TABLE permission in the destination database.

The second step inserts the specified rows into the new table. If the second step fails for any reason (hardware failure, exceeding a system resource, and so on), the new table will exist but have no rows.

If you just want to create a new table with the same table definition, but with no records in it, you could use the same statement, and append a WHERE clausule which will always be false.  This way the first step will create the table, but no records will be inserted.

I think that this is what you are looking for.

Greetings and best regards,


Stefaan Lesage

0
 

Expert Comment

by:scarlett
ID: 1090894
Put a # in front of the new table name to make it a temporary table, i.e., #temp_table
0
 

Author Comment

by:fox_dogg
ID: 1090895
Thanks perfect!
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question