SQL Server, How to remove updates elements from User-Defined Table Type?(SQL Server,如何从用户定义的表类型中删除更新元素?)
问题描述
我有一个用户定义的表类型,比如说 @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,如何从用户定义的表类型中删除更新元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQL Server,如何从用户定义的表类型中删除更新元
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
