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

Tsql Values子句作为Table Value Constructor

时间:2015-09-18 20:32:27      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

Values子句不仅能为Insert into子句提供插入的数据源,而且还能作为From子句和Merge中的Using的数据源。

 

1,Specifying multiple values as a derived table in a FROM clause.

SELECT a, b 
FROM (VALUES (1, 2), (3, 4), (5, 6), (7, 8), (9, 10) ) AS MyTable(a, b);
GO

-- Used in an inner join to specify values to return.
SELECT ProductID, a.Name, Color
FROM Production.Product AS a
INNER JOIN (VALUES (Blade), (Crown Race), (AWC Logo Cap)) AS b(Name) 
ON a.Name = b.Name;


2,Specifying multiple values as a derived source table in a MERGE statement

MERGE INTO Sales.SalesReason AS Target
USING (VALUES (Recommendation,Other), (Review, Marketing), (Internet, Promotion))
       AS Source (NewName, NewReasonType)
ON Target.Name = Source.NewName
WHEN MATCHED THEN
    UPDATE SET ReasonType = Source.NewReasonType
WHEN NOT MATCHED BY TARGET THEN
    INSERT (Name, ReasonType) VALUES (NewName, NewReasonType)
WHEN NOT MATCHED BY SOURCE THEN
    DELETE
OUTPUT $action;

 

参照MSDN

Table Value Constructor

https://msdn.microsoft.com/en-us/library/dd776382%28v=sql.110%29.aspx?f=255&MSPPError=-2147217396

Tsql Values子句作为Table Value Constructor

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/4820151.html

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