7

I want to take values from my old database tables to new database tables.

Old db structure:

Table I: Country

  • CountryId
  • CountryName

New db structure

Table II: Countries

  • Id
  • Name

I used the following insert query like,

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

But I have the result like,

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

like that.

but I need the result like,

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

To achieve this, what is the query? help me...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
PoliDev
  • 1,408
  • 9
  • 24
  • 45

4 Answers4

11

If there is a lot of data to transfer and multiple tables, I would suggest using Import/Export wizard provided by SQL Server Management Studio.

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

Edit: However, if there is not lot of data and the two systems are not connected - and you need to generate script to transfer data, your query should look like this:

SELECT 'INSERT INTO Countries (Id, Name) VALUES (' + CAST(countryid AS VARCHAR(50)) + ', ''' + countryname + ''')' from Country
Nenad Zivkovic
  • 18,221
  • 6
  • 42
  • 55
6

If both databases are on one server, you can just do like this:

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

Hope this helps

serejja
  • 22,901
  • 6
  • 64
  • 72
5

Use simple INSERT statement (database_name.[schema_name].table)

INSERT [NewDB].[your_schema].[Countries](Id,Name)
SELECT CountryId, CountryName
FROM [OldDB].[your_schema].[Country]
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
3

To be honest I do not really get the queries that you wrote. Are you trying to build strings from your queries that you then pass again to your database?

You can just pass your values from one database to the other in one 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

Or you can use a temporary table and switch the database connection after retrieving your original values.

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: as a previous poster mentioned, for this to work you need both databases on the same server, since you did not say anything about that i just assumed that that was the case? :D

DrCopyPaste
  • 4,023
  • 1
  • 22
  • 57