問題描述
我有一個用戶定義的表類型,比如說 @TT dbo.IntType readonly,
IntType 是一批 int
,作為 Primary-Key
I have a User-Defined Table Type, lets say @TT dbo.IntType readonly,
IntType is batch of int
, as Primary-Key
CREATE TYPE [dbo].[IntType] AS TABLE(
[T] [int] NOT NULL,
PRIMARY KEY CLUSTERED
我喜歡根據 IntType
中的所有鍵 (int
) 和 INSERT
新行來做 UPDATE
對于數據庫中不存在的鍵(int
)(更新插入)
I like to do UPDATE
based on all key(int
) in IntType
, and INSERT
new rows for the key(int
) not exist in the database (an upsert)
我想知道是否有一種簡單的方法可以刪除UPDATED"鍵(int
)?然后我可以插入其余的.(或者如果您有 SQL Server 2010 的超級單行更新插入!!!)
I wonder if theres a easy way to remove the "UPDATED" key(int
)? Then I can insert the rest.
(or if you have a super one-line upsert for SQL Server 2010!!!)
推薦答案
使用 MERGE
語句.
http://technet.microsoft.com/en-us/library/bb510625.aspx
讓我看看我能不能把一些東西放在一起.
Let me see if I can put something together.
示例
MERGE INTO [TargetTable] AS T
USING (SELECT l.T AS 'id' FROM @list l ) AS S
ON (T.[id] = S.[id])
WHEN MATCHED THEN
UPDATE SET
T.updated = 1
WHEN NOT MATCHED THEN
INSERT VALUES ([insert value into each column of target table]);
編輯 2:參數 @list
是傳入的列表,您填充了 ids
EDIT 2: The parameter @list
is the list that is passed in that you populated with the ids
這篇關于SQL Server,如何從用戶定義的表類型中刪除更新元素?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!