Home Page
Articoli
Tips & Tricks
News
Forum
Archivio Forum
Blogs
Sondaggi
Rss
Video
Utenti
Chi Siamo
Contattaci
Username:
Password:
Login
Registrati ora!
Recupera Password
Home Page
Stanze Forum
SQL Server 2000/2005/2008, Express, Access, MySQL, Oracle
Linked server & SET IDENTITY_INSERT
lunedì 12 dicembre 2005 - 13.02
Elenco Threads
Stanze Forum
Aggiungi ai Preferiti
Cerca nel forum
d@dora
Profilo
| Senior Member
338
messaggi | Data Invio:
lun 12 dic 2005 - 13:02
Ciao a tutti, ho un MSSQl 2k al quale collego un'altro SQL 2k con i linked server. Sul server linkato, cancello tabelle e inserisco records correttamente, ma su una tabella che ha una colonna identity mi da errore!
Non risco ad utilizzare il SET IDENTITY_INSERT perche' non accetta tra i parametri il 'nomeserver.db.user.table, ma solo usr.table. Qual'e' la sintassi corretta ???? O come posso passare anche il server linkato ????
Ciao e grazie
lbenaglia
Profilo
| Guru
5.625
messaggi | Data Invio:
lun 12 dic 2005 - 14:21
> Non risco ad utilizzare il SET IDENTITY_INSERT perche' non accetta tra i parametri il
> 'nomeserver.db.user.table, ma solo usr.table.
> Qual'e' la sintassi corretta ???? O come posso passare anche il server linkato ????
Ciao d@dora,
Non proprio, la sintassi corretta del comando SET IDENTITY_INSERT è:
SET IDENTITY_INSERT [ database. [ owner. ] ] { table } { ON | OFF }
Come vedi accetta solo il three-part name, ovvero database.owner.oggetto.
Se non ti interessa trasferire i valori IDENTITY sul linked server, puoi semplicemente escludere la colonna IDENTITY nell'operazione di INSERT;
/********************************
*** Linked Server ***
********************************/
USE tempdb;
GO
/* Definisco la tabella dbo.Students */
CREATE TABLE dbo.Students(
StudentID int NOT NULL IDENTITY(10, 10) CONSTRAINT PK_Students PRIMARY KEY,
FirstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL
);
GO
/********************************
*** Server locale ***
********************************/
USE tempdb;
GO
/* Definisco la tabella dbo.Students */
CREATE TABLE dbo.Students(
StudentID int NOT NULL IDENTITY CONSTRAINT PK_Students PRIMARY KEY,
FirstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL
);
GO
/* La popolo */
INSERT dbo.Students VALUES('Lorenzo', 'Benaglia');
INSERT dbo.Students VALUES('Luca', 'Bianchi');
INSERT dbo.Students VALUES('Andrea', 'Montanari');
GO
/* Interrogo la tabella locale */
SELECT *
FROM dbo.Students;
GO
/* Output:
StudentID FirstName LastName
----------- ---------- ----------
1 Lorenzo Benaglia
2 Luca Bianchi
3 Andrea Montanari
(3 row(s) affected)
*/
SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
GO
/* Copio i dati sul server remoto */
INSERT rrestelli.tempdb.dbo.Students(FirstName, LastName)
SELECT FirstName, LastName
FROM dbo.Students;
GO
/* Interrogo la tabella remota */
SELECT *
FROM rrestelli.tempdb.dbo.Students;
GO
/* Output:
StudentID FirstName LastName
----------- ---------- ----------
10 Lorenzo Benaglia
20 Luca Bianchi
30 Andrea Montanari
(3 row(s) affected)
*/
/* Pulizia */
DROP TABLE dbo.Students;
/********************************
*** Linked Server ***
********************************/
/* Pulizia */
DROP TABLE dbo.Students;
< Continuo in un nuovo post... >
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
lbenaglia
Profilo
| Guru
5.625
messaggi | Data Invio:
lun 12 dic 2005 - 14:22
< parte 2>
Diversamente se intendi trasferire anche i valori della colonna IDENTITY, sei costretto ad invertire il ruolo dei server, ovvero il server dove effettuerai l'INSERT dovrà linkare quello contenente i dati da importare.
A questo punto l'operazione andrà a buon fine:
/********************************
*** Linked Server ***
********************************/
USE tempdb;
GO
/* Definisco la tabella dbo.Students */
CREATE TABLE dbo.Students(
StudentID int NOT NULL IDENTITY CONSTRAINT PK_Students PRIMARY KEY,
FirstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL
);
GO
/* La popolo */
INSERT dbo.Students VALUES('Lorenzo', 'Benaglia');
INSERT dbo.Students VALUES('Luca', 'Bianchi');
INSERT dbo.Students VALUES('Andrea', 'Montanari');
GO
/* Interrogo la tabella locale */
SELECT *
FROM dbo.Students;
GO
/* Output:
StudentID FirstName LastName
----------- ---------- ----------
1 Lorenzo Benaglia
2 Luca Bianchi
3 Andrea Montanari
(3 row(s) affected)
*/
/********************************
*** Server locale ***
********************************/
USE tempdb;
GO
/* Definisco la tabella dbo.Students */
CREATE TABLE dbo.Students(
StudentID int NOT NULL IDENTITY(10, 10) CONSTRAINT PK_Students PRIMARY KEY,
FirstName varchar(10) NOT NULL,
LastName varchar(10) NOT NULL
);
GO
/* Abilito l'IDENTITY INSERT */
SET IDENTITY_INSERT dbo.Students ON
GO
SET ANSI_NULLS ON
SET ANSI_WARNINGS ON
GO
/* Copio i dati dal server remoto */
INSERT dbo.Students(StudentID, FirstName, LastName)
SELECT StudentID, FirstName, LastName
FROM rrestelli.tempdb.dbo.Students;
GO
/* Interrogo la tabella dbo.Students */
SELECT *
FROM dbo.Students;
GO
/* Output:
StudentID FirstName LastName
----------- ---------- ----------
1 Lorenzo Benaglia
2 Luca Bianchi
3 Andrea Montanari
(3 row(s) affected)
*/
/* Disabilito l'IDENTITY INSERT */
SET IDENTITY_INSERT dbo.Students OFF
GO
/* Inserisco un nuovo studente */
INSERT dbo.Students VALUES('Gianluca', 'Hotz');
GO
/* Interrogo la tabella dbo.Students */
SELECT *
FROM dbo.Students;
GO
/* Output:
StudentID FirstName LastName
----------- ---------- ----------
1 Lorenzo Benaglia
2 Luca Bianchi
3 Andrea Montanari
20 Gianluca Hotz
(4 row(s) affected)
*/
/* Pulizia */
DROP TABLE dbo.Students;
/********************************
*** Linked Server ***
********************************/
/* Pulizia */
DROP TABLE dbo.Students;
> Ciao e grazie
Prego.
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
d@dora
Profilo
| Senior Member
338
messaggi | Data Invio:
lun 12 dic 2005 - 14:55
ho letto il tuo post, ma io volevo inserire i recs su db remoto, da locale a remoto. Il set identity_insert non lo posso usare. Sto cercando di gestirmi una 'replica' a mano e mi pare strano di poter deletare una tabella, ma non poter inserire un record da un'altra tabella .
Ciao e grazie
lbenaglia
Profilo
| Guru
5.625
messaggi | Data Invio:
lun 12 dic 2005 - 15:00
>ho letto il tuo post, ma io volevo inserire i recs su db remoto,
>da locale a remoto. Il set identity_insert non lo posso usare.
>Sto cercando di gestirmi una 'replica' a mano e mi pare strano
>di poter deletare una tabella, ma non poter inserire un record
> da un'altra tabella .
Infatti li puoi inserire tranquillamente, a meno dei valori della colonna IDENTITY.
Se vuoi inserire anche i valori IDENTITY puoi fare come ti ho suggerito nell'ultimo post, ovvero invertendo i ruoli dei due server.
>Ciao e grazie
Prego.
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
d@dora
Profilo
| Senior Member
338
messaggi | Data Invio:
lun 12 dic 2005 - 15:40
saro' particolarmente tonto, ma non capisco qualche cosa:
-- creo il linked server
exec sp_addlinkedserver @server = 'dado',
@srvproduct = '',
@provider = 'MSDASQL',
@catalog='ideabiella',
@provstr = 'DRIVER={SQL Server};SERVER=192.168.1.125;UID=sa;PWD=;
-- login
EXEC sp_addlinkedsrvlogin 'dado', FALSE, NULL, 'sa', ''
--elimino la tabella sul server remoto
DELETE FROM Dado.ideabiella.dbo.anart
?????? che metto per identity ??????
INSERT Dado.ideabiella.dbo.anart SELECT * FROM locale.dbo.anart;
Ciao e grazie
lbenaglia
Profilo
| Guru
5.625
messaggi | Data Invio:
lun 12 dic 2005 - 15:57
>saro' particolarmente tonto, ma non capisco qualche cosa:
>-- creo il linked server
>exec sp_addlinkedserver @server = 'dado',
> @srvproduct = '',
> @provider = 'MSDASQL',
> @catalog='ideabiella',
>
>-- login
>EXEC sp_addlinkedsrvlogin 'dado', FALSE, NULL, 'sa', ''
>
>--elimino la tabella sul server remoto
>DELETE FROM Dado.ideabiella.dbo.anart
Qua non stai eliminando la tabella ma stai semplicemente eliminando tutte le righe della tabella remota Dado.ideabiella.dbo.anart
>?????? che metto per identity ??????
>
>
>INSERT Dado.ideabiella.dbo.anart SELECT * FROM
> locale.dbo.anart;
Ti ho già detto 3 volte che questa sintassi non può funzionare nel caso in cui la tabella remota Dado.ideabiella.dbo.anart abbia una colonna IDENTITY.
Come ti ho indicato nel mio primo post, se ti accontenti di fare autogenerare il valore della colonna IDENTITY nella tabella remota, è sufficiente escludere quella colonna nell'operazione di INSERT.
Per un esempio, leggi il mio primo post.
>Ciao e grazie
Prego.
Ciao!
--
Lorenzo Benaglia
Microsoft MVP - SQL Server
http://blogs.dotnethell.it/lorenzo/
http://italy.mvps.org
d@dora
Profilo
| Senior Member
338
messaggi | Data Invio:
lun 12 dic 2005 - 16:03
>
>Ti ho già detto 3 volte che questa sintassi non può funzionare
>nel caso in cui la tabella remota Dado.ideabiella.dbo.anart
>abbia una colonna IDENTITY.
>
>Come ti ho indicato nel mio primo post, se ti accontenti di fare
>autogenerare il valore della colonna IDENTITY nella tabella
>remota, è sufficiente escludere quella colonna nell'operazione
> di INSERT.
probabilmente era quello che NON volevo vedere ??? :-)))
ciao e grazie
Torna su
Stanze Forum
Elenco Threads
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?
Dopo esserti registrato potrai chiedere
aiuto sul nostro
Forum
oppure aiutare gli altri
Consulta le
Stanze
disponibili.
Registrati ora !