Question about SQL DB

05/30/2013 15:32 eminemsjunge#1
Hello Guys i have a little Question.
I want to add a Table from one Database into an other DB can anyone help me with this ?
05/30/2013 16:47 Schickl#2
You can genereate a script to get the structure of the table and just run that again in the target db
The following code then copies the contents from one table to the other one
Code:
INSERT INTO <target db>.dbo.<target table>
SELECT * --you shouldn't use *, especially if you have an identity column and IDENTITY_INSERT is OFF, so specify columns
FROM <source db>.dbo.<source table>
[WHERE ...]
05/30/2013 18:45 eminemsjunge#3
Can you help me via TV ? :)
05/30/2013 18:54 Schickl#4
I'm pretty busy atm, so no, sorry

I found out about an easier way to do what you want(didn't know about that method)

Code:
CREATE TABLE <target db>.dbo.<table name>
AS ( SELECT *
       FROM <source db>.dbo.<table name>
       [WHERE ...]
     )
That makes it way easier
Just insert the names(the WHERE clause is optional of course)
05/30/2013 22:33 eminemsjunge#5
I will try it thanks :)

dont work for me :(
05/31/2013 00:48 royalblade#6
Delete them table from the target database.
Drop table TARGETDB.dbo.TABLENAME

Select * INTO TARGETDB.dbo.TABLENAME FROM OLDDB.dbo.TABLENAME
05/31/2013 09:07 Schickl#7
Quote:
Originally Posted by eminemsjunge View Post
I will try it thanks :)

dont work for me :(
how does your command look like exactly?

If the table already exists in the target db you will have to drop it(just like royalblade said)
The second command he posted won't work if you drop the table(because that's just a normal insert statement) so you have to use the create table I posted