我想将值列表表示为特定表的一行。目前我的代码看起来像是upsert的一部分:
MERGE INTO dbo.MyTable mt
USING (VALUES(1234, 'Val1', GETDATE()))
AS v(ID, Value, DateUpdated)
ON v.ID = mt.ID
WHEN NOT MATCHED THEN
INSERT VALUES(v.ID, v.Value, v.DateUpdated)
WHEN MATCHED THEN
UPDATE SET mt.Value = v.Value;
理想情况下,我的代码看起来更像是这样:
MERGE INTO dbo.MyTable mt
USING (VALUES(1234, 'Val1', GETDATE()))
AS v(typeof(dbo.MyTable)) --TABLE DEFINITION, NO DUPLICATE TYPING
ON v.ID = mt.ID
WHEN NOT MATCHED THEN
INSERT VALUES(v.ID, v.Value, v.DateUpdated)
WHEN MATCHED THEN
UPDATE SET mt.Value = v.Value;
其中" typeof"允许我的表的定义用于值类型转换和名称转换。
答案 0 :(得分:1)
不知道你为什么要寻找"表定义,没有重复的打字"
而且我不知道我是否正确理解了您的要求。
试试这个,
MERGE INTO dbo.MyTable mt
USING (select ID, Value, DateUpdated from dbo.MyTable%ROWTYPE where id=-1
union all
select 1234,'Val1',GETDATE()
) AS v
ON v.ID = mt.ID
WHEN NOT MATCHED THEN
INSERT VALUES(v.ID, v.Value, v.DateUpdated)
WHEN MATCHED THEN
UPDATE SET mt.Value = v.Value;