简体   繁体   English

仅存在表的第一个数据库中的第二个数据库中的sql更新数据

[英]sql update data in 2nd db from 1st db only where table exists

I'm trying to move some tables from a live db to another db, which will then be made live after the data has been transferred. 我正在尝试将一些表从实时数据库移动到另一个数据库,然后在传输数据后将其生效。 The 2nd db exists on a server with 2008, and the migration is from 2012. which has caused a few problems 第二个数据库存在于2008年的服务器上,迁移是从2012年开始的,这导致了一些问题

Originally I did a full DB export with Scripts. 最初我用Scripts做了一个完整的数据库导出。 because the export tool wouldnt go to previous versions. 因为导出工具不会转到以前的版本。 - after the full export I deleted the superfluous tables on db2. - 完全导出后,我删除了db2上多余的表。 as I am only moving 40 or so out of about 200. 因为我只在约200个左右移动40个左右。

Now the DB structure is set up, all is working. 现在DB结构已经建立,一切正常。 with out of date-data. 与过时的数据。 and everything is ready to be switched once the data is made up to date. 一旦数据更新,一切都准备好了。 So i'd ideally like a script that checks to see if a table in db1 exists in db2, then if it does to copy across all rows. 所以我理想地喜欢一个脚本,它检查db2中是否存在db1中的表,然后是否复制所有行。 Is this possible? 这可能吗?

If creating a linked server is not an option then you can try using sql server import data wizard to transfer data from one db to another. 如果创建链接服务器不是一个选项,那么您可以尝试使用sql server导入数据向导将数据从一个数据库传输到另一个数据库。 Use the following link for how-to: Import data easily in sql server 使用以下链接获取操作方法: 在sql server中轻松导入数据

You'll need to create a procedure on DB2 that returns a result based on the table you're checking: 您需要在DB2上创建一个过程,该过程根据您正在检查的表返回结果:

USE [DB2]

CREATE PROCEDURE DBO.CHECKDB2TABLE

@TABLENAME NVARCHAR(50)

AS

IF EXISTS (
    SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE (TABLE_NAME = @TABLENAME)
)
    BEGIN
        SELECT 'TRUE'
    ELSE 
        SELECT 'FALSE'
    END

Then use the procedure to check DB2 and create a conditional statement based on that output to copy across the table. 然后使用该过程检查DB2并根据该输出创建条件语句以在表中进行复制。

EXEC DB2.dbo.CheckDB2Table @TABLENAME='Tablename'

Thanks. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果第 2 个表中存在第 1 个表的值,则用第 1 个和第 2 个表的总和更新第 2 个表的值,否则将第一个表数据插入到第 2 个表 - If value of 1 table exists in the 2nd table, update the 2nd table value with sum of 1st and 2nd tables, else insert 1st table data to 2nd table 从第一张表中选择数据是否在第二张表中存在但有条件 - SELECT data from 1st table whether exists or not in 2nd table but with conditions SQL 仅选择第二个表中的行,包含从第一个表收到的确切结果 - SQL selecting only rows from 2nd table, containing exact results received from 1st table 根据第一和第二表进行更新 - Update based of 1st and 2nd table 将数据从第三张表链接到第一张表和第二张表 - Link data from third table to a 1st and a 2nd table 仅当第 2 个表中不存在记录时,才将第 1 个表中的记录插入到第 2 个表中 - Insert records from 1st table to 2nd table only when the record is not present in the 2nd table 如何在 SQL 服务器中从第一个和第二个表中获取匹配记录,并且仅从第一个表中获取不匹配记录,该服务器已加入 1 个字段 - How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field 根据第一个表更新第二个表列 - update 2nd table column based on 1st table 如何从第一个表中返回所有列,而从第二个表中只返回一列 - How to return all columns from 1st table and only 1 column from 2nd table 双倍增量,其中第二增量在遇到数据时在SQL中反映第一 - Double increment where 2nd increment reflects 1st in sql for encounter data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM