Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将数据从数据库插入到另一个数据库_Sql_Sql Server_Sql Server 2008_Sql Insert - Fatal编程技术网

Sql 将数据从数据库插入到另一个数据库

Sql 将数据从数据库插入到另一个数据库,sql,sql-server,sql-server-2008,sql-insert,Sql,Sql Server,Sql Server 2008,Sql Insert,我想将旧数据库表中的值转换为新数据库表 旧数据库结构: 表一:国家 乡下人 国名 新的数据库结构 表二:国家 身份证 名字 我使用了下面的插入查询 select 'insert into Countries (Id, Name) select ', countryid, countryname from Country 但我的结果是 insert into Countries (Id, Name) values (1, 'India') 插入国家(Id、名称)选择1印度 插入国家/

我想将旧数据库表中的值转换为新数据库表

旧数据库结构:

表一:
国家

  • 乡下人
  • 国名
新的数据库结构

表二:
国家

  • 身份证
  • 名字
我使用了下面的插入查询

select 'insert into Countries (Id, Name) select ', countryid, countryname from Country
但我的结果是

insert into Countries (Id, Name) values (1, 'India')
  • 插入国家(Id、名称)选择1印度
  • 插入国家/地区(Id、名称)选择2个任意国家/地区
像那样

但我需要这样的结果

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

要实现这一点,查询是什么?帮助我…

使用简单的INSERT语句(数据库名称。[架构名称].table)


如果两个数据库都在一台服务器上,您可以这样做:

insert into [db1name].[dbo].[Countries] (Id, Name)
select CountryId, CountryName
from [db2name].[dbo].[Countries]
where _your where clause_
SELECT 'INSERT INTO Countries (Id, Name) VALUES (' + CAST(countryid AS VARCHAR(50)) + ', ''' + countryname + ''')' from Country

希望这有助于

如果有大量数据要传输,并且有多个表,我建议使用SQL Server Management Studio提供的导入/导出向导

编辑: 但是,如果没有太多数据,并且两个系统没有连接,并且需要生成脚本来传输数据,则查询应如下所示:

insert into [db1name].[dbo].[Countries] (Id, Name)
select CountryId, CountryName
from [db2name].[dbo].[Countries]
where _your where clause_
SELECT 'INSERT INTO Countries (Id, Name) VALUES (' + CAST(countryid AS VARCHAR(50)) + ', ''' + countryname + ''')' from Country

老实说,我真的不明白你写的问题。 您是否试图从查询中生成字符串,然后再次传递到数据库

只需在一个查询中将值从一个数据库传递到另一个数据库:

/*
    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
或者,您可以使用临时表并在检索原始值后切换数据库连接

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

编辑:如前一张海报所述,要使其正常工作,您需要在同一台服务器上同时使用两个数据库,因为您没有提到这一点,我只是假设情况是这样的:哇,我以前从未尝试过导入/导出wizad。谢谢你,内纳季夫科维奇先生