码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2015-02-11 12:26:45      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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

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);

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

标签:

原文地址:http://www.cnblogs.com/liuyuanhao/p/4285640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!