使用合并在插入语句中选择 [英] Select in Insert statement using Merge

查看:55
本文介绍了使用合并在插入语句中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,ALl,
我想在Sql Server 2008的Merge语句的Insert部分中使用select语句.

示例查询如下所示"

Hi ALl,
I want to use select statement in Insert part of Merge Statement in Sql server 2008.

Example query is like below"

MERGE StudentTotalMarks AS stm
USING (SELECT StudentID,StudentName FROM StudentDetails) AS sd
ON stm.StudentID = sd.StudentID
WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
WHEN NOT MATCHED THEN
INSERT(StudentID,StudentMarks)
VALUES(sd.StudentID,select marks from xyz where studendid=<pre>sd.StudentID);</pre>



因此,在值的最后一行中,第二列有select语句,因此,我想确认是否可以运行,否则请问如何才能达到相似的结果.



so in the last line of values part there is select statement for second column, so i want to confirm whether this will work or not, if not then how can i achieve similar result.

推荐答案

尝试一下
MERGE StudentTotalMarks AS stm
USING (SELECT StudentID,StudentName FROM StudentDetails) AS sd
ON stm.StudentID = sd.StudentID
WHEN MATCHED AND stm.StudentMarks > 250 THEN DELETE
WHEN MATCHED THEN UPDATE SET stm.StudentMarks = stm.StudentMarks + 25
WHEN NOT MATCHED THEN
INSERT select sd.StudentID,marks as StudentMarks from xyz where studendid=sd.StudentID


祝您编码愉快!
:)


Happy Coding!
:)


这篇关于使用合并在插入语句中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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