【问题标题】:Insert data from db to another db将数据从数据库插入另一个数据库
【发布时间】:2023-04-10 11:38:01
【问题描述】:

我想将旧数据库表中的值提取到新数据库表中。

旧的数据库结构:

表一:Country

  • 国家标识
  • 国名

新的数据库结构

表二:Countries

  • 身份证
  • 姓名

我使用了以下插入查询,例如,

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

但我的结果是,

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

这样。

但我需要这样的结果,

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

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

【问题讨论】:

    标签: sql sql-server sql-server-2008 sql-insert


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 哇,我以前从未尝试过导入/导出向导。谢谢@NenadZivkovic 先生 ?
    【解决方案2】:

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

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

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      使用简单的 INSERT 语句 (database_name.[schema_name].table)

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

      【讨论】:

        【解决方案4】:

        说实话,我并没有真正理解你写的查询。 您是否尝试从查询中构建字符串,然后再次将其传递给数据库?

        您可以在一个查询中将值从一个数据库传递到另一个数据库:

        /*
            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
        

        编辑:正如之前的海报所提到的,为了使这工作你需要在同一台服务器上的两个数据库,因为你没有说任何关于我只是假设是这样的情况? :D

        【讨论】:

          猜你喜欢
          • 2014-11-21
          • 1970-01-01
          • 2015-03-31
          • 2015-02-04
          • 1970-01-01
          • 2019-12-02
          • 1970-01-01
          • 2023-04-03
          相关资源
          最近更新 更多