日期:2014-05-18  浏览次数:20503 次

从一张表导入另一张表的问题!
有两张表:
临时表:table1
正式表:table2
table1,table2表结构相同,有以下几列:
{
  a1 varchar(50),主键
  a2 varchar(50),主键
  a3 varchar(50),主键
  a4 bigint(8),
  a5 varchar(50)

}
以前表中a1,a2两列是主键,现在又增加了一列a3作为主键

问题:把数据从table1 导入到table2中,
  要求:如果table1中的a1,a2,a3三列和table2中的a1,a2,a3三列不全相同,做Insert操作,否则:Update a4,a5二列值。
现在我做转档的时候,提示在table2中不能插入重复主键???不知道什么原因?????

急!!!!!!

------解决方案--------------------
應該用遊標吧.
------解决方案--------------------
按你的要求,貌似不能直接导数据吧,
可以用存储过程,游标历遍全表进行插入和更新
------解决方案--------------------
引用楼主 zwjxxm 的帖子:
有两张表:
临时表:table1
正式表:table2
table1,table2表结构相同,有以下几列:
{
a1 varchar(50),主键
a2 varchar(50),主键
a3 varchar(50),主键
a4 bigint(8),
a5 varchar(50)

}
以前表中a1,a2两列是主键,现在又增加了一列a3作为主键

问题:把数据从table1 导入到table2中,
要求:如果table1中的a1,a2,a3三列和table2中的a1,a2,a3三列不全相同,做Insert操作,否则:Upd…

------解决方案--------------------
如果是2008,用MERGE,参考如下:

SQL code
解读SQL Server2008的新语句MERGE

作者:Jonathan Allen  2007-07-24 

    SQL Server 2008将包含用于合并两个行集(rowset)数据的新句法。根据一个源数据表对另一个数据表进行确定性的插入、更新和删除这样复杂的操作,运用新的MERGE语句,开发者用一条命令就可以完成。 

    对两个表进行信息同步时,有三步操作要进行。首先要处理任何需要插入目标数据表的新行。其次是处理需要更新的已存在的行。最后要删除不再使用的旧行。这个过程中需要维护大量重复的逻辑,并可能导致微妙的错误。 

    Bob Beauchemin讨论了MERGE语句,这个语句将上述的多个操作步骤合并成单一语句。他给出了如下的例子: 

merge [target] t
using [source] s on t.id = s.id
when matched then update t.name = s.name, t.age = s.age -- use "rowset1"
when not matched then insert values(id,name,age) -- use "rowset2"
when source not matched then delete; -- use "rowset3" 

    如你所见,具体操作是根据后面的联合(join)的解析结果来确定的。在这个例子中,如果目标和源数据表有匹配的行,就实行更新操作。如果没有,就实行插入或者删除操作来使目标数据表和源数据表保持一致。 

    这个新句法的一个美妙之处是它在处理更新时的确定性。在使用标准的UPDATE句法和联合时,可能有超过一个源行跟目标行匹配。在这种情况下,无法预料更新操作会采用哪个源行的数据。 

    而当使用MERGE句法时,如果存在多处匹配,它会抛出一个错误。这就提醒了开发者,要达到预想的目标,当前的联合条件还不够明确。 



SQL Server 2008 MERGE
ZDNet 软件频道 更新时间:2007-11-19 作者:David.Portas 来源:David Portas’ Blog
本文关键词:MERGE SQL Server 2008 SQL Server 数据库 
MERGE is a new DML statement in SQL Server 2008. Microsoft have implemented the ISO SQL 2003 and 2007 standard MERGE statement (as seen in Oracle and DB2) and added some extensions of their own.

In a nutshell, MERGE allows you to perform simultaneous UPDATE, INSERT and/or DELETE operations on one table. There are new physical operators that combine these operations so that they can be performed in a single scan rather than multiple scans.

MERGE has loads of possible applications. For the first time you can assign the contents of one table or query to another in a single operation. The following example requires SQL Server 2008 CTP4. Given this schema and data:

CREATE TABLE a
 (keycol INT PRIMARY KEY,
  col1 INT NOT NULL,
  col2 INT NOT NULL,
  col3 INT NOT NULL);

CREATE TABLE b
 (keycol INT PRIMARY KEY,
  col1 INT NOT NULL,
  col2 INT NOT NULL,
  col3 INT NOT NULL);

INSERT INTO a VALUES (1,0,0,0),(2,0,0,0);
INSERT INTO b VALUES (1,1,1,1),(3,3,3,3);

The following MERGE will populate table a with the same data as table b:

MERGE INTO a
USING b
 ON a.keycol = b.keycol
WHEN MATCHED THEN
 UPDATE SET
  col1 = b.col1,
  col2 = b.col2,
  col3 = b.col3
WHEN NOT MATCHED THEN
 INSERT (keycol, col1, col2, col3)
 VALUES (b.keycol, b.col1, b.col2, b.col3)
WHEN SOURCE NOT MATCHED THEN
 DELETE;

In the relational world this is the operation known as Relational Assignment ie:

 a := b

Unfortunately the SQL syntax is less pretty and requires just a little more typing!

MERGE also makes a good "upsert" for application CRUD stored procedures, removing the need for constructs like:

IF NOT EXISTS ...
  INSERT ...

Here's an examp