2013-03-27 16 views
7

Desidero prelevare valori dalle mie vecchie tabelle di database a nuove tabelle di database.Inserimento di dati da db a altro db

vecchia struttura db:

Tabella I: Country

  • countryID
  • CountryName

Nuova struttura db

Tabella II: Countries

  • Id
  • Nome

Ho usato la seguente query di inserimento come,

select 'insert into Countries (Id, Name) select ', countryid, countryname from Country 

Ma io ho il risultato come,

  • insert into Countries(Id,Name) select 1 India
  • insert into Countries(Id,Name) select 2 Any Country

simile.

ma ho bisogno del risultato simile,

insert into Countries (Id, Name) values (1, 'India') 

per raggiungere questo obiettivo, qual è la domanda? aiutami ...

risposta

10

Se sono presenti molti dati da trasferire e più tabelle, suggerirei di utilizzare la procedura guidata Importa/Esporta fornita da SQL Server Management Studio.

http://www.mssqltips.com/sqlservertutorial/203/simple-way-to-import-data-into-sql-server/

Edit: Tuttavia, se non v'è grande quantità di dati e di due sistemi non sono collegati - e avete bisogno di generare script per il trasferimento dei dati, la query dovrebbe essere simile a questo:

SELECT 'INSERT INTO Countries (Id, Name) VALUES (' + CAST(countryid AS VARCHAR(50)) + ', ''' + countryname + ''')' from Country 
+1

ottimo! funziona ... grazie – PoliDev

+0

Wow, non ho mai provato prima il wizard Import/Export. Grazie signore @NenadZivkovic –

5

Utilizzo semplice INSERT (nome_database [schema_name] .table.)

INSERT [NewDB].[your_schema].[Countries](Id,Name) 
SELECT CountryId, CountryName 
FROM [OldDB].[your_schema].[Country] 
4

Se entrambi i database sono su un server, si può solo fare in questo modo:

insert into [db1name].[dbo].[Countries] (Id, Name) 
select CountryId, CountryName 
from [db2name].[dbo].[Countries] 
where _your where clause_ 

Spero che questo aiuti

3

Ad essere sincero non ho davvero le domande che hai scritto. Stai cercando di costruire stringhe dalle tue query che poi passerai nuovamente al tuo database?

Si può solo passare i valori da un database all'altro in una query:

/* 
    maybe you need to switch off identity on your target table 
    to get your original id values into the target table like this: 
    (without comment ofc ;)) 
*/ 
--SET IDENTITY_INSERT TargetDatabase.dbo.Countries ON 

INSERT INTO TargetDatabase.dbo.Countries (Id, Name) 
    SELECT 
      CountryId, CountryName 
     FROM SourceDatabase.dbo.Country 

--SET IDENTITY_INSERT TargetDatabase.dbo.Countries OFF 

Oppure si può utilizzare una tabella temporanea e passare la connessione al database dopo recupero dei valori originali.

USE SourceDatabase 

DECLARE @TempTable TABLE (CountryId INT PRIMARY KEY, CountryName NVARCHAR(MAX)) 

INSERT INTO @TempTable (CountryId, CountryName) 
    SELECT 
      CountryId, CountryName 
     FROM Country 

USE TargetDatabase 

/* 
    maybe you need to switch off identity on your target table 
    to get your original id values into the target table like this: 
    (without comment ofc ;)) 
*/ 
--SET IDENTITY_INSERT Countries ON 

INSERT INTO Countries (Id, Name) 
    SELECT 
      CountryId, CountryName 
     FROM @TempTable 

--SET IDENTITY_INSERT Countries OFF 

EDIT: come un poster precedente detto, per far funzionare tutto questo è necessario entrambi i database sullo stesso server, dal momento che non hai detto nulla ho appena pensato che questo era il caso? : D

Problemi correlati