使用merge-using语句初始化数据库

不会覆盖或删除数据库已经存在的数据,将数据作为一张表更新到另一张表中。

merge into Course as Target
using (values
       (1,‘Economics‘,3),
       (2,‘Literrature‘,3),
       (3,‘Chemistry‘,4)

)
as Source (CourseID,Title,Credits)
on Target.CourseID =Source.CourseID
when not matched by target then
Insert(Title,Credits)
values(Title,Credits);

merge into Student as Target
using(values
     (1,‘Tibbetts‘,‘Donnie‘,‘2013-09-01‘),
  (2,‘Guzman‘,‘Liza‘,‘2012-01-13‘),
  (3,‘Catlett‘,‘Phil‘,‘2011-09-03‘)
)
as Source (StudentID,LastName,FirstName,EnrollmentDate)
on Target.StudentID=Source.StudentID
when not matched by Target then
Insert (LastName,FirstName,EnrollmentDate)
values (LastName,FirstName,EnrollmetnDate);

Merge into Enrollment as Target
using (values
       (1,2.00,1,1),
    (2,3.50,2,2),
    (3,4.00,2,3),
    (4,1.80,2,1),
    (5,3.20,3,1),
    (6,4.00,3,2)
)
as Source (EnrollmentID,Grade,CourseID,StudentID)
on Target.EnrollmentID =Source.EnrollmentID
when not matched by Target then
Insert (Grade,CourseID,StudentID)
values (Grade,CourseID,StudentID);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。