if此查询的条件

时间:2015-11-04 20:30:07

标签: sql-server

如果此查询的条件如何...如果记录不存在,则从此选择

插入其他更新
 SELECT CAST(left(TerminalName, patindex('%[^0-9]%', TerminalName+'.') - 1) as int)
      ,'04944700'
      ,SUBSTRING(TerminalName , CHARINDEX('-' , TerminalName) + 1, LEN(TerminalName))
      ,TerminalName
      ,'Bulk'
      ,'0010'
      ,user
      ,GETDATE()
      ,user
      ,GETDATE()
FROM External_Blk_Itm_Contracts WHERE TerminalName NOT IN (SELECT MBFTERMINALNAME FROM budterminals)

1 个答案:

答案 0 :(得分:0)

您要使用的是MERGE声明。您的Select语句将是您的数据源,然后您将插入或更新,具体取决于是否存在与连接条件匹配的行。

这是我写的T-SQL的一个例子:

MERGE ODS.dbo.Mytable as [Target]
    USING [Source Query] as [Source]
    ON [Target].SubId = [Source].SubId And [Target].WorkflowId = [Source].WorkflowId
    WHEN NOT MATCHED BY TARGET THEN
        INSERT
        (
             [SubId]
            ,[WorkflowId]
            ,[ReadTime]
            ,[ReadTimeLocal]
        )
        VALUES
        (
             [SubId]
            ,[WorkflowId]
            ,[ReadTime]
            ,[ReadTimeLocal]
        )
    WHEN MATCHED THEN UPDATE Set
        [Target].ReadTime = [Source].ReadTime,
        [Target].ReadTimeLocal = [Source].ReadTimeLocal,
    ;