salve Massimo,
massimo74rn wrote:
Ciao,
Ho due database, ognuno su un server diverso.
vorrei Creare una tabella sul db1 importandola dal db2, una cosa del genere:
CREATE TABLE miaTabella AS SELECT * FROM db2.tabelle("VecchiaTabella") oppure potrei usare un dataset????
se mi postate un esempio per favore in vb.net grazie.
puoi usare un linked server.. ad esempio, sulla macchina B
SET NOCOUNT ON;
USE master;
CREATE DATABASE remoteDB;
GO
USE remoteDB;
CREATE TABLE dbo.RemoteTb (
ID int NOT NULL PRIMARY KEY ,
IdRef int NOT NULL ,
Description varchar(20) NOT NULL
);
GO
INSERT INTO dbo.RemoteTb VALUES ( 1 , 1 , 'some value');
INSERT INTO dbo.RemoteTb VALUES ( 2 , 1 , 'some value2');
INSERT INTO dbo.RemoteTb VALUES ( 3 , 1 , 'some value3');
INSERT INTO dbo.RemoteTb VALUES ( 4 , 2 , 'some value');
INSERT INTO dbo.RemoteTb VALUES ( 5 , 2 , 'some value2');
GO
SELECT * FROM dbo.RemoteTb;
wait here for execution on local db, then clean-up
USE master;
GO
DROP DATABASE remoteDB;
--<-----------------------
sulla macchina A, invece,
SET NOCOUNT ON;
USE master;
CREATE DATABASE localDB;
GO
USE localDB;
CREATE TABLE dbo.LocalTb (
ID int NOT NULL PRIMARY KEY ,
Description varchar(20) NOT NULL
);
GO
INSERT INTO dbo.LocalTb VALUES ( 1 , 'some header');
INSERT INTO dbo.LocalTb VALUES ( 2 , 'some header2');
INSERT INTO dbo.LocalTb VALUES ( 3 , 'some header3');
INSERT INTO dbo.LocalTb VALUES ( 4 , 'some header4');
INSERT INTO dbo.LocalTb VALUES ( 5 , 'some header5');
GO
--EXEC master..sp_addlinkedserver @server='S_Remote', @srvproduct='',
@provider='SQLOLEDB',
@datasrc='NomeMacchinaB';
both registration succeded, but now use IP as example
EXEC master..sp_addlinkedsrvlogin 'S_Remote', 'false', NULL, 'sa', 'remote_sa_pwd'
GO
PRINT 'querying databases'
SELECT * FROM S_Remote.remoteDB.dbo.RemoteTb;
PRINT 'join them together'
SELECT l.Description ,
r.Id, r.Description
FROM dbo.LocalTb l JOIN S_Remote.remoteDB.dbo.RemoteTb r
ON r.IdRef = l.Id;
GO
EXEC sp_droplinkedsrvlogin 'S_Remote', NULL;
EXEC sp_dropserver 'S_Remote', 'droplogins';
GO
cleanup
USE master;
GO
DROP DATABASE localDB;
saluti