Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create table from another table in different database in sql server 2005

I have a database "temp" with table "A". I created new database "temp2". I want to copy table "A" from "temp" to a new table in "temp2" . I tried this statement but it says I have incorrect syntax, here is the statement:

CREATE TABLE B IN 'temp2'
  AS (SELECT * FROM A IN 'temp');

Here is the error:

Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'IN'. Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'IN'.

Anyone knows whats the problem?

Thanks in advance,

Greg

like image 847
Greg Avatar asked Jun 06 '10 07:06

Greg


People also ask

How can create table from another database in SQL Server?

If you would like to create a new table based on the structure and data from another table, you can use the SELECT INTO clause. First, write a SELECT clause followed by a list of columns (in our example: id , name , and price ) from the existing table (in our example: product ).

How can you create a new table with existing data from another table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.

Can we create a table from another table in SQL?

A copy of an existing table can also be created using CREATE TABLE . The new table gets the same column definitions. All columns or specific columns can be selected. If you create a new table using an existing table, the new table will be filled with the existing values from the old table.

How do I copy data from one table of one database to another table of another database in SQL?

Using SQL Server Management StudioOpen the table with columns you want to copy and the one you want to copy into by right-clicking the tables, and then clicking Design. Click the tab for the table with the columns you want to copy and select those columns.


1 Answers

I've not seen that syntax before. This is what I normally use.

SELECT * 
INTO temp2.dbo.B
FROM temp.dbo.A
like image 178
Martin Smith Avatar answered Dec 05 '22 22:12

Martin Smith