扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:IT专家网 来源:IT专家网 2008年1月29日
数据库引擎现在支持一种新的参数类型来引用用户自定义表类型(参考 5)。表值参数可以发送更多的 SQL Server 数据。
下面的示例展示了如何使用表值参数。
USE AdventureWorks; GO /**//* Create a table type. */ CREATE TYPE LocationTableType AS TABLE ( LocationName VARCHAR(50) , CostRate INT ); GO /**//* Create a procedure to receive data for the table-valued parameter. */ CREATE PROCEDURE usp_InsertProductionLocation @TVP LocationTableType READONLY AS SET NOCOUNT ON INSERT INTO [AdventureWorks].[Production].[Location] ([Name] ,[CostRate] ,[Availability] ,[ModifiedDate]) SELECT *, 0, GETDATE() FROM @TVP; GO /**//* Declare a variable that references the type. */ DECLARE @LocationTVP AS LocationTableType; /**//* Add data to the table variable. */ INSERT INTO @LocationTVP (LocationName, CostRate) SELECT [Name], 0.00 FROM [AdventureWorks].[Person].[StateProvince]; /**//* Pass the table variable data to a stored procedure. */ EXEC usp_InsertProductionLocation @LocationTVP; GO |
这个新增的 Transaction SQL 语句在一个基于源数据连接结果集的目标表上执行 INSERT、UPDATE 和 DELETE 操作。该语法允许您将一个数据源连接到目标表或视图上。然后在连接后的结果集上执行多种操作。
MERGE 的语法为:
[ WITH <common_table_expression> [,...n] ] MERGE [ TOP ( expression ) [ PERCENT ] ] [ INTO ] target_table [ [ AS ] table_alias ] [ WITH ( <merge_hint> ) ] USING <table_source> ON <search_condition> [ WHEN MATCHED [ AND <search_condition> ] THEN <merge_matched> ] [ WHEN [TARGET] NOT MATCHED [ AND <search_condition> ] THEN <merge_not_matched> ] [ WHEN SOURCE NOT MATCHED [ AND <search_condition> ] THEN <merge_ matched> ] <output_clause> [ OPTION ( <query_hint> [ ,...n ] ) ] ; <merge_hint>::= { [ <table_hint_limited> [ ,...n ] ] [ [ , ] INDEX ( index_val [ ,...n ] ) ] } <table_source> ::= { table_or_view_name [ [ AS ] table_alias ] [ <tablesample_clause> ] [ WITH ( table_hint [ [ , ]...n ] ) ] | rowset_function [ [ AS ] table_alias ] [ ( bulk_column_alias [ ,...n ] ) ] | user_defined_function [ [ AS ] table_alias ] | OPENXML <openxml_clause> | derived_table [ AS ] table_alias [ ( column_alias [ ,...n ] ) ] | <joined_table> | <pivoted_table> | <unpivoted_table> } <merge_matched>::= { UPDATE SET <set_clause> | DELETE } <set_clause>::= { column_name = { expression | DEFAULT | NULL } | { udt_column_name. { { property_name = expression | field_name = expression } | method_name ( argument [ ,...n ] ) } } | column_name { .WRITE ( expression , @Offset , @Length ) } | @variable = expression } [ ,...n ] <merge_not_matched>::= INSERT [ ( <column_list> ) ] { VALUES ( <values_list> ) | DEFAULT VALUES } <output_clause>::= { [ OUTPUT <dml_select_list> INTO { @table_variable | output_table } [ (column_list) ] ] [ OUTPUT <dml_select_list> ] } <dml_select_list>::= { <column_name> | scalar_expression } [ [AS] column_alias_identifier ] [ ,...n ] <column_name> ::= { DELETED | INSERTED | from_table_name } . { * | column_name } | $ACTION |
USE AdventureWorks; GO MERGE Production.ProductInventory AS pi USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail sod JOIN Sales.SalesOrderHeader soh ON sod.SalesOrderID = soh.SalesOrderID AND soh.OrderDate = GETDATE() GROUP BY ProductID) AS src (ProductID, OrderQty) ON (pi.ProductID = src.ProductID) WHEN MATCHED AND pi.Quantity - src.OrderQty <> 0 THEN UPDATE SET pi.Quantity = pi.Quantity - src.OrderQty WHEN MATCHED AND pi.Quantity - src.OrderQty = 0 THEN DELETE; |
我们看到,利用 MERGE 语句可以将复杂的 SQL 语句简化。它比起 IF、CASE 等更加灵活和强大。
结论
Microsoft SQL Server 2008 对事务性 SQL 语言做了一些增强,提高了查询效率。使得 SQl Server 成为大中型企业数据库的首先产品。SQL Server 2008 将伴随 Visual Studio 2008 一起发布,开发人员提前了解这些信息有助于在 SQL Server 的新版本发布后快速建立基于该版本的企业级应用程序。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者