sql如何在合并语句后清空一个表

时间:2014-08-06 10:05:35

标签: sql sql-server sql-server-2008

这是我的存储过程

ALTER PROCEDURE dbo.sync_vmp

AS
BEGIN
    SET NOCOUNT ON;
    merge into FRM_DTA as Target
    using FRM_DTA_VMP as Source
    on Target.FRM_ID = Source.FRM_ID
    when matched then
    update set
    target.FRM_OWNER = Source.FRM_OWNER,
    target.FRM_OWNER_AR = Source.FRM_OWNER_AR,
    target.FRM_OWNER_EN = Source.FRM_OWNER_EN,
    target.FRM_AREA_ID = Source.FRM_AREA_ID,
    target.FRM_AREA = Source.FRM_AREA,
    target.FRM_AREA_AR = Source.FRM_AREA_AR,
    target.FRM_EMARA_ID = Source.FRM_EMARA_ID,
    target.FRM_EMARA = Source.FRM_EMARA,
    target.FRM_EMARA_AR = Source.FRM_EMARA_AR,
    target.FRM_REGION_ID = Source.FRM_REGION_ID,
    target.FRM_REGION = Source.FRM_REGION,
    target.FRM_REGION_AR = Source.FRM_REGION_AR,
    target.RecievingCentreID = Source.RecievingCentreID,
    target.RecievingCentreAr = Source.RecievingCentreAr,
    target.RecievingCentre = Source.RecievingCentre,
    target.Owner_Address = Source.Owner_Address,
    target.Owner_Address_Ar = Source.Owner_Address_Ar,
    target.FRM_TELEPHONE = Source.FRM_TELEPHONE,
    target.FRM_MOBILE = Source.FRM_MOBILE,
    target.Owner_BankName_ID = Source.Owner_BankName_ID,
    target.Owner_BankName = Source.Owner_BankName,
    target.Owner_BankBranch_ID = Source.Owner_BankBranch_ID,
    target.Owner_BankBranch = Source.Owner_BankBranch,
    target.BankAccountNo = Source.BankAccountNo,
    target.AccountActive = Source.AccountActive,
    target.PrefPaymentMethod = Source.PrefPaymentMethod,
    target.FRM_CAT = Source.FRM_CAT,
    target.farmNumber = Source.farmNumber,
    target.FRM_PASSPORT = Source.FRM_PASSPORT,
    target.FRM_Approve = Source.FRM_Approve,
    target.FRM_AUDIT_STATUS = Source.FRM_AUDIT_STATUS,
    target.FRM_AUDIT_REASON = Source.FRM_AUDIT_REASON,
    target.FRM_WATER_SAL = Source.FRM_WATER_SAL,
    target.FRM_SOL_SAL = Source.FRM_SOL_SAL,
    target.syn_status = Source.syn_status,
    target.FRM_status = Source.FRM_status,
    target.Call_status = Source.Call_status,
    target.ALZAKA_CH = Source.ALZAKA_CH
    when not matched then
    insert(FRM_ID,FRM_OWNER,FRM_OWNER_AR,FRM_AREA_ID,FRM_AREA,FRM_AREA_AR,FRM_EMARA_ID,FRM_EMARA,FRM_EMARA_AR,FRM_REGION_ID,FRM_REGION,FRM_REGION_AR,RecievingCentreID,RecievingCentreAr,RecievingCentre,Owner_Address,Owner_Address_Ar,FRM_TELEPHONE,FRM_MOBILE,Owner_BankName_ID,Owner_BankName,Owner_BankBranch_ID,Owner_BankBranch,BankAccountNo,AccountActive,PrefPaymentMethod,FRM_CAT,farmNumber,FRM_PASSPORT,FRM_Approve,FRM_AUDIT_STATUS,FRM_AUDIT_REASON,FRM_WATER_SAL,FRM_SOL_SAL,syn_status,FRM_status,Call_status,ALZAKA_CH)
    values (Source.FRM_ID,Source.FRM_OWNER, Source.FRM_OWNER_AR,Source.FRM_AREA_ID,Source.FRM_AREA,Source.FRM_AREA_AR,Source.FRM_EMARA_ID,Source.FRM_EMARA,Source.FRM_EMARA_AR,Source.FRM_REGION_ID,Source.FRM_REGION,Source.FRM_REGION_AR,Source.RecievingCentreID,Source.RecievingCentreAr,Source.RecievingCentre,Source.Owner_Address,Source.Owner_Address_Ar,Source.FRM_TELEPHONE,Source.FRM_MOBILE,Source.Owner_BankName_ID,Source.Owner_BankName,Source.Owner_BankBranch_ID,Source.Owner_BankBranch,Source.BankAccountNo,Source.AccountActive,Source.PrefPaymentMethod,Source.FRM_CAT,Source.farmNumber,Source.FRM_PASSPORT,Source.FRM_Approve,Source.FRM_AUDIT_STATUS,Source.FRM_AUDIT_REASON,Source.FRM_WATER_SAL,Source.FRM_SOL_SAL,Source.syn_status,Source.FRM_status,Source.Call_status,Source.ALZAKA_CH);

END

如您所见,我正在将dbo.FRM_DTA_VMP中的数据合并到dbo.FRM_DTA

我的问题是如何在执行此存储过程后清空dbo.FRM_DTA_VMP?

如果合并中没有发生异常,我想这样做

1 个答案:

答案 0 :(得分:1)

如人们所说,你可以在MERGE语句后立即截断sproc中的表。检查@@ERROR变量的错误,如下所示; 澄清:

CREATE PROCEDURE YourProcedure
BEGIN
--code goes here

 IF @@ERROR = 0 --no errors
   BEGIN
    TRUNCATE TABLE dbo.FRM_DTA_VMP
   END
END

MSDN:@ERROR