Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Importing Data from a table in another database 1

Status
Not open for further replies.
Sep 29, 2002
524
US

How can I do this? Can I do:

Code:
INSERT INTO [database1].[dbo].[table1]
       ([Column1], [Column2], [Column3])
Select [database2].[dbo].[Column1],
       [database2].[dbo].[Column2], 
       [database2].[dbo].[Column3]
FROM   [database2].[dbo].[table2]

Is that the exact syntax? Do I include the brakets? The reason I am asking is because I am getting the following error for each of the fields of the second database:

The multi-part identifier "databse2.dbo.field1" could not be bound.

Thanks in advanced,


Gladys Rodriguez
GlobalStrata Solutions
Computer Repair, Website Design and Computer Consultant
Small Business Resources
 
Your missing the table names

Code:
INSERT INTO [database1].[dbo].[table1]
       ([Column1], [Column2], [Column3])
Select [database2].[dbo].[!][table2][/!].[Column1],
       [database2].[dbo].[!][table2][/!].[Column2], 
       [database2].[dbo].[!][table2][/!].[Column3]
FROM   [database2].[dbo].[table2]

Usually, people will use aliases to make the syntax easier to read, like this...

Code:
INSERT INTO [database1].[dbo].[table1]
       ([Column1], [Column2], [Column3])
Select TableFrom.[Column1],
       TableFrom.[Column2], 
       TableFrom.[Column3]
FROM   [database2].[dbo].[table2] As TableFrom

And technically, since the select part is only looking at one table, there wouldn't be any ambiguity regarding the fields (which table they belong to) so you can omit the alias all together, like this...

Code:
INSERT INTO [database1].[dbo].[table1]
       ([Column1], [Column2], [Column3])
Select [Column1],
       [Column2], 
       [Column3]
FROM   [database2].[dbo].[table2]

The square brackets exist to allow you to use names that contain spaces or are reserved words. For the database/table/columns you show here, they wouldn't be necessary. However, they don't hurt to have them, either.

Hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top