views:

651

answers:

6

This is one of those "I probably should know this, but I don't" questions. How do I make a copy of a table in Enterprise Manager? Not export the data into a different database, just make a copy of the table within the same database. At this point, I don't care whether the data comes with or not, but the table definition should definitely be duplicated.

If I do Ctrl+C on a selected table and paste the results into a text editor, it gives me a Create Table statement that looks promising, but I can't for the life of me find any place to run that statement.

Edit: note that I'm asking about SQL Server Enterprise Manager. This is NOT the same thing as "SQL Server Management Studio". There's no "New Query" button, neither in the top left corner nor anywhere else. In fact, the word 'query' does not occur anywhere in EM except on the Tools menu, and there it's a link to Query Analyzer.

A: 

Try this:

SELECT *
INTO new_table_name
FROM old_table_name
Scott Whitlock
Try this WHERE?
Martha
@Marth - right click on the table, Open->Return top (100) rows, then click the SQL button on the tool bar. Replace the SELECT statement with this one, and then press the ! button.
Scott Whitlock
+3  A: 
SELECT * INTO MyNewTable FROM MyOldTable

The new table will be created without primary keys, foregin keys nor anything else

Jhonny D. Cano -Leftware-
1. I specifically need the keys and indexes, not so much the data. 2. Where would I put this statement in order to run it?
Martha
+2  A: 

Copy a table with all content (without keys and constraints)

select * into tablecopy from table

Copy a table without content (without keys and constraints)

select top 0 * into tablecopy from table

There's a way to copy it with all constraints, in the right click menu (of the table) then all tasks then script as then create to then new query editor window (or a file). You will have to modify the generated script so the constraints and keys are named differently though, and then do an

insert into tablecopy select * from table

to populate it.

EDIT: If what you are actually asking is a place to run SQL in the Enterprise Manager, try either the Query Analyzer or the Management Studio.

Vinko Vrsalovic
+1  A: 

Using TSQL...

SELECT * INTO MyTableCopy FROM MyTable WHERE 1=2

Using EM, right-click on the object > All Tasks > Generate SQL Script.

Aaron Alton
Generate SQL Script... and then what? It gives me a window with almost nothing enabled, and none of it makes much sense.
Martha
On the third tab, check the four "table scripting" options, and then press "OK".
Aaron Alton
+2  A: 

If you're talking 2000 enterprise manager: Tools -> Query analyzer Will give you a place to execute the query.

feihtthief
Downvoter, explain yourself!
Vinko Vrsalovic
Thank you, this worked!Full steps for the next person: select the table you want to copy and press Ctrl+C (or use the right-click menu). On the Tools menu, select SQL Query Analyzer. This brings up QA with the correct database selected and everything. Ctrl+V to paste the Create Table statement. Edit the statement to change the table name and the name of any constraints/keys/etc. Press the green arrow. Ta-da! (You might need to refresh the table list after you close QA and go back to EM.)
Martha
Vinko: Not me. So you explain yourself.
feihtthief
feihtthief: I was talking about some guy who downvoted your answer (without any valid reason, afaict), I wasn't accusing you of downvoting me or anybody else.
Vinko Vrsalovic
Vinko: My apologie. I misunderstood. I guess the answer was to brief for them.
feihtthief
A: 

If you want primary keys, etc., right-click on the table, select "Script table as", then "Create to", then "New Window". Change the name of the table to the name of the copy and run the script. Then you can select into as per the other answers if you'd like the data.

Malcolm
Not only the name has to be changed, if there are constraints they have to be renamed as well
Vinko Vrsalovic
Right-clicking does not give a "Script table as" option. Are you sure you're talking about Enterprise Manager?
Martha