具有多个过滤条件的SQL存储的proc更新

时间:2019-07-08 10:16:15

标签: sql sql-server

我正在使用ms sql服务器。我正在尝试为以下情况创建SP。 我每天都要用不同的条件从Temp映射表(被覆盖)中更新sourceDB,下面的代码会出错,所以请有人提出如何完成此操作。

/**Here is my code:**/

Create Proc SP_Source_DBMapping

AS

Create table Table_Mapping

(
[User] varchar(50),
[Call_Date] datetime,
[Final_status] varchar (255),
[Category] varchar (255),
[V_Date] datetime,
[V_Time] time,
[Comments] varchar(max),
[CB_Date] datetime,
[key] varchar(255) Primary key
)
bulk insert Table_Mapping
from 'D:\DB\Source.Csv'
with
(
firstrow =2,
fieldterminator =',',
rowterminator = '\n'
)

IF SourceTable.[Callable Status] <> 'Closed'
    or
    SourceTable.Final_Status not like 'call done%' and 
    SourceTable.[Callable Status] = 'Closed' and
    [dbo].[Table_Mapping].Final_Status like 'call done%'

Begin
update ST set [User] = TM.[User]
,[Call_Date] = TM.[Call_Date]
,Final_Status = TM.Final_Status
,[Category]=TM.[Category]
,[V_Date]=TM.[V_Date]
,[V_Time]=TM.[V_Time]
,[Comments]=TM.[Comments]
,[CB_Date]=TM.[CB_Date]
,[Attempt Date I]=
case when [Attempt Date I] = null Then TM.[Call_Date]
End
,[Attempt Date II]=
case when [Attempt Date I] <> TM.[Call_Date] and [Attempt Date II] = null Then TM.[Call_Date]
 end
,[Attempt Date III]=
case when [Attempt Date I] <> TM.[Call_Date] and [Attempt Date II] <> TM.[Call_Date] and [Attempt Date III] = null Then TM.[Call_Date]
 end

From DB1.SourceTable as ST
inner join Table_Mapping as TM
on ST.[Key]=TM.[Key]
--where exists (select [key] from DB1.SourceTable)
Drop Table Table_Mapping
End

0 个答案:

没有答案