Solved

Create table as on SQL Server

Posted on 1998-10-29
3
203 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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

770 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