在ms sql 2008中将记录从一个表插入另一个表 [英] Insert record from one table to another in ms sql 2008

查看:96
本文介绍了在ms sql 2008中将记录从一个表插入另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将一个表中的记录插入到另一个表中它给我的错误

违反PRIMARY KEY约束''Table1 $ PK_KndNr''。无法在对象''dbo.Table1''中插入重复键。





 插入 进入表1(KndNr,Anrede,Name,Vorname,AdrZusatz,Strasse,Ort,[Tel_Privat],[Tel_Geschäft], 
传真,Natel,电子邮件,Geburtsdatum,ZahlungsKondition)

选择 distinct Kundennummer,Anrede,Name,Vorname,Zusatz,Strasse,Ort,[Telefon 1 ],[Telefon 2 ],
传真,Natel,[电子邮件],Geburtstag, 1

来自 table2
WHERE 存在选择 * 来自表1
where Table1.KndNr = tabl e2.Kundennummer);





什么是正确的查询以避免源表中的重复记录。

解决方案

PK_KndNr''。无法在对象''dbo.Table1''中插入重复键。





 插入 进入表1(KndNr,Anrede,Name,Vorname,AdrZusatz,Strasse,Ort,[Tel_Privat],[Tel_Geschäft], 
传真,Natel,电子邮件,Geburtsdatum,ZahlungsKondition)

选择 distinct Kundennummer,Anrede,Name,Vorname,Zusatz,Strasse,Ort,[Telefon 1 ],[Telefon 2 ],
传真,Natel,[电子邮件],Geburtstag, 1

来自 table2
WHERE 存在选择 * 来自表1
where Table1.KndNr = tabl e2.Kundennummer);





什么是正确的查询以避免源表中的重复记录。


< blockquote>请参阅 文章 [ ^ ]


使用此...

  insert   into 表1 

选择 distinct Kundennummer,Anrede,Name ,Vorname,Zusatz,Strasse,Ort,[Telefon 1 ],[Telefon 2 ],
传真,Natel,[电子邮件],Geburtstag, 1
fr om table2
WHERE 存在选择 * 来自表1
where Table1.KndNr = table2.Kundennummer)





注意:当选择记录按顺序提及列时目的地表有

快乐编码!

:)


Hi
I want to insert records from one table to another one it give me error
Violation of PRIMARY KEY constraint ''Table1$PK_KndNr''. Cannot insert duplicate key in object ''dbo.Table1''.


insert into Table1(KndNr,Anrede, Name, Vorname, AdrZusatz, Strasse, Ort, [Tel_Privat], [Tel_Geschäft], 
Fax, Natel, Email, Geburtsdatum,ZahlungsKondition)

select distinct Kundennummer,Anrede,Name,Vorname,Zusatz,Strasse,Ort,[Telefon 1],[Telefon 2],
Fax,Natel,[E-Mail],Geburtstag,1

from table2
WHERE not exists (select * from Table1
                  where Table1.KndNr = table2.Kundennummer);



what will be correct query to avoid duplicate records in source table.

解决方案

PK_KndNr''. Cannot insert duplicate key in object ''dbo.Table1''.


insert into Table1(KndNr,Anrede, Name, Vorname, AdrZusatz, Strasse, Ort, [Tel_Privat], [Tel_Geschäft], 
Fax, Natel, Email, Geburtsdatum,ZahlungsKondition)

select distinct Kundennummer,Anrede,Name,Vorname,Zusatz,Strasse,Ort,[Telefon 1],[Telefon 2],
Fax,Natel,[E-Mail],Geburtstag,1

from table2
WHERE not exists (select * from Table1
                  where Table1.KndNr = table2.Kundennummer);



what will be correct query to avoid duplicate records in source table.


Refer this article[^]


use this...

insert into Table1

select distinct Kundennummer,Anrede,Name,Vorname,Zusatz,Strasse,Ort,[Telefon 1],[Telefon 2],
Fax,Natel,[E-Mail],Geburtstag,1
from table2
WHERE not exists (select * from Table1
                  where Table1.KndNr = table2.Kundennummer)



Note: when select records mention columns in order in which the destination table have
Happy Coding!
:)


这篇关于在ms sql 2008中将记录从一个表插入另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆