views:

41

answers:

2

Hey all, I am trying to find out how to copy data from one table to another database table. I have two connections to two different databases. Ones called comp-DEV1 and the other SQLTEST. I am currently unable to copy data from my sorce table (SQLTEST) to my destination table (comp-DEV1).

This is the error:

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '-'.

Query:

 INSERT INTO comp-DEV1.EMSSQL.dbo.tblCL
 SELECT *
 FROM SQLTEST.EMSSQL.dbo.tblCL
 WHERE NOT EXISTS(SELECT * 
             FROM comp-DEV1.EMSSQL.dbo.tblCL 
             WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID = comp-DEV1.EMSSQL.dbo.tblCL.CID)
             )

Any help would be great :o)

David

+2  A: 

Try wrapping your database names in brackets, such as:

INSERT INTO [comp-DEV1].EMSSQL.dbo.tblCL 
 SELECT * 
 FROM SQLTEST.EMSSQL.dbo.tblCL 
 WHERE NOT EXISTS(SELECT *  
             FROM [comp-DEV1].EMSSQL.dbo.tblCL  
             WHERE (SQLTEST.EMSSQL.dbo.tblCL.CID = 
                [comp-DEV1].EMSSQL.dbo.tblCL.CID) 
             ) 
LittleBobbyTables
Ok, did that and i now have a new error: Msg 7202, Level 11, State 2, Line 2Could not find server 'SQLTEST' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
StealthRT
@StealthRT: You should only need to use the four name notation to access the Linked Server instance - you can omit it when referencing tables on the current instance: `SELECT * FROM EMSSQL.dbo.tblCL WHERE ...`
OMG Ponies
@OMG Ponies (haha). You are correct but i still get the error since, im guessing, the databases are not linked together?
StealthRT
A: 

Run the following statement first to check that you can read the source from the destination server:

SELECT * FROM [comp-DEV1].EMSSQL.dbo.tblCL

Get that working first then you should be on your way...

richardps
Yes, it reads those records.
StealthRT