SQL Server >> Insert Into Select From Databases on 2 Servers

by mickbw » Tue, 14 Sep 2004 20:48:17 GMT

Please excuse the bizarre title.

I am trying to do what should be a simple task in Query Analyzer.

Insert into [Server1 Database]
Select * from [Server2 Database] where trandate = '2004-09-13'

This type of statement works if I do it on a database on the same
server but it will not accept the IP address of the servers in the
statement.

What am I missing?


SQL Server >> Insert Into Select From Databases on 2 Servers

by Andrew J. Kelly » Tue, 14 Sep 2004 21:08:30 GMT


If your not on the same server you must use a Linked Server or something
like OpenQuery(). See both in BOL for more details.

--
Andrew J. Kelly SQL MVP









SQL Server >> Insert Into Select From Databases on 2 Servers

by Adam Machanic » Tue, 14 Sep 2004 21:12:30 GMT

You need add a linked server for Server1 on Server2, or a linked server for
Server2 on Server1... Then, you'll be able to do this either on Server1 or
Server2 respectively, depending on which option you chose.

The easiest way to add a linked server is via EM; Expand your server, then
Security, then Linked Servers, and click the New icon...

You can also use sp_addlinkedserver and the related system stored procedures
if you prefer to do it via Query Analyzer.

Once you have the linked server set up, you can use 4-part naming:

INSERT INTO Server1.Database.TableOwer.TableName
SELECT * FROM TableOwner.TableName --Assuming you're on Server2 in the right
database








Insert Into Select From Databases on 2 Servers

by Michael Brennan-White » Tue, 14 Sep 2004 23:22:51 GMT

Thanks for the replies.

If I am restricted to a few views on the Select part of the statement,
should I be able to create a linked server?

Mick



Insert Into Select From Databases on 2 Servers

by Adam Machanic » Tue, 14 Sep 2004 23:27:32 GMT

Can you clarify your question? I'm not sure what you mean when you say
you're restricted to a few views. Do you mean that your access to the
remote server only allows you to query certain views?








Insert Into Select From Databases on 2 Servers

by Michael Brennan-White » Tue, 14 Sep 2004 23:47:12 GMT

I will and I'm sorry for the vagueness (is that a word) of my reply.

We are accessing the data from a remote data warehouse server. As you
surmised I can only query certain views. The connection is problematic
and very slow so I want to extract only the new transactions we require
to our server on a regular basis.

Thanks again



Insert Into Select From Databases on 2 Servers

by Adam Machanic » Tue, 14 Sep 2004 23:51:46 GMT

That shouldn't pose a problem with creating the linked server; the
permissions of whatever login you use for the link will be used, so you'll
still only have access to the same views, etc.








Insert Into Select From Databases on 2 Servers

by mickbw » Wed, 15 Sep 2004 22:29:34 GMT

Adam,

Thanks a lot. I figured out what the problem was.

My problem was that I thought the Linked Server text box when creating
a new Linked server could be an alias name for the Linked Server.
Nope, it had to be the IP address for the server.

Thanks very much,

Michael






Similar Threads

1. problem w/CREATE RULE...ON INSERT...INSERT INTO...SELECT...FROM...WHERE w/multiple inserts - PostgreSQL

2. Best way to select and insert a huge table in the same database

insert A (...) select .... from B inner join C on .....
will create huge transact log. Any better way?

3. Cross-database - select/inserts - SQL Server

4. selecting insert into another database

Hi,

I am able to create an additional table by selecting fields from another 
table, thing is I need to create table # 2 in a different database on the 
same server, this is my sql:

Select userid, TEMPFleischutID

into ZZFLEISCHUTEMPIDS

from users

This will create the table on the same database, I need to create it in 
database called "FAA", is this possible ?

Aleks 


5. Help with Insert statemnt selecting from Access database - SQL Server

6. Exclusively lock database during multiple SELECT, UPDATE and INSERT operations

Hi,

My application needs to execute several read and write operations to one or 
more tables in a Sql Server database and for the database to be exclusively 
locked across all these operations.

I cannot use lock hints because, as I understand it, they are part of the 
INSERT or UPDATE statements.  If I am executing an INSERT statement followed 
by an UPDATE statement within a stored procedure I need to prevent other 
users getting access to any records in the interval between the INSERT 
portion finishing and the UPDATE starting.

But it's worse - some of the INSERT/UPDATE bits are determined based on data 
that's been read in and cached in its memory by my application.  So what I 
need is to be able to do something like:

- Lock database for all operations other than mine
- Do the reads I need
- Do the writes I need
- Release the database so other users can either read it or execute similar 
lock - process - unlock operations

Is this possible, and if so, what's the best / easiest way to achieve it? 
(Bearing in mind that the operations that need to be executed while the 
database is locked are not all in a single stored procedure etc.)

If I can't do it 'properly' I will have to resort to creating a file on a 
network location and trying to open it exclusively in order to start the 
READ-INSERT-UPDATE stuff I described above, but I'm hoping to find the 
elegant solution - i.e. within SQL Server.

Thanks!

Pete 

7. INSERT INTO TABLE SELECT FROM ANOTHER DATABASE WITH JOINS - SQL Server

8. INSERT SELECT with multiple databases

Hello,

I want to do an insert select, where data from one table in one
database is copied into another table in another database. The
databases are on the same server. The tables have the same structure.

I have tried:  INSERT INTO db1.table SELECT * FROM db2.table
but I keep getting this error: Incorrect table name

Is it possible to do an INSERT SELECT with different databases on the
same server?