更新地区号存储过程

USE [**]
GO
/****** Object:  StoredProcedure [***].[***]    Script Date: 06/03/2016 09:01:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [***].[llrrGXKH_GJZ]
    @lsbm char(20),
    @dqbh char(20),
    @serror    varchar(255) output
AS
BEGIN
   declare @vssql varchar(500)

--1.1 根据上游供应商绑定匹配,解决不同客户以同一客户名从不同供应商进货的问题.
           set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khsdzd_sjkhbh,yjlx_dqbh=khsdzd_dqbh,yjlx_xykh= ‘‘1‘‘,yjlx_ppkhmc=khsdzd_sjkhmc ‘
        set @vssql += ‘ From khsdzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khsdzd_khmc)) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and (khsdzd_js = ‘‘1‘‘ or khsdzd_js = ‘‘2‘‘ or khsdzd_js = ‘‘3‘‘)‘
        set @vssql +=‘  and khsdzd_gysbh = yjlx_gysbh‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.5 根据上游绑定表(khsdzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
--1.2 根据上游供应商绑定匹配
           set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khsdzd_sjkhbh,yjlx_dqbh=khsdzd_dqbh,yjlx_xykh= ‘‘0‘‘,yjlx_ppkhmc=khsdzd_sjkhmc ‘
        set @vssql += ‘ From khsdzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khsdzd_khmc)) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and (khsdzd_js = ‘‘4‘‘ or khsdzd_js = ‘‘5‘‘ or khsdzd_js = ‘‘6‘‘)‘
        set @vssql +=‘  and khsdzd_gysbh = yjlx_gysbh‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.5 根据上游绑定表(khsdzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end    

--1.3 根据客户名称来匹配,并更新 协议 客户标志为‘‘1‘‘,客户名称==匹配客户名称

        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khppzd_khmc)) ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1 根据客户名称来匹配,并更新 协议 客户标志为‘‘1‘‘,客户名称==匹配客户名称‘
            raiserror(@sError,16,1)
            return -1
            end        

--1.5 根据客户别名表(khbmzd)来更新匹配

        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khbmzd_khbh,yjlx_dqbh=khbmzd_dqbh,yjlx_xykh= ‘‘1‘‘,yjlx_ppkhmc=khbmzd_khmc ‘
        set @vssql += ‘ From khbmzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khbmzd_bm)) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and (khbmzd_js = ‘‘1‘‘ or khbmzd_js = ‘‘2‘‘ or khbmzd_js = ‘‘3‘‘)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.5 根据客户别名表(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end    

        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khbmzd_khbh,yjlx_dqbh=khbmzd_dqbh,yjlx_xykh= ‘‘0‘‘,yjlx_ppkhmc=khbmzd_khmc ‘
        set @vssql += ‘ From khbmzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khbmzd_bm)) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and (khbmzd_js = ‘‘4‘‘ or khbmzd_js = ‘‘5‘‘ or khbmzd_js = ‘‘6‘‘)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.5 根据客户别名表(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
--1.6 根据客户匹配三个关键字字典(khppzd2)来更新匹配
        --市内更新协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),4)=left(ltrim(rtrim(khppzd_dqbh)),4)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end    

        --省内更新协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),2)=left(ltrim(rtrim(khppzd_dqbh)),2)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
        --省外更新协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end        

        --市内更新非协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),4)=left(ltrim(rtrim(khppzd_dqbh)),4)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
        --省内更新非协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),2)=left(ltrim(rtrim(khppzd_dqbh)),2)‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
        --省外更新非协议客户
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd2 ‘
        set @vssql += ‘ where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz3))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and   (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘1.6 根据khppzd2(khbmzd)来更新匹配‘
            raiserror(@sError,16,1)
            return -1
            end
-- 2 更新非协议客户的客户名称  客户名称==匹配客户名称
        --市内
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khppzd_khmc)) ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),4)=left(ltrim(rtrim(khppzd_dqbh)),4) ‘

        exec(@vssql)
        if @@error <> 0
            begin
            select @serror = ‘ 2 更新非协议客户的客户名称  客户名称==匹配客户名称,--市内‘
            raiserror(@sError,16,1)
            return -1
            end
        --省内
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khppzd_khmc)) ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null )    ‘
        set @vssql += ‘ and left(ltrim(rtrim(yjlx_gysdqbh)),2)=left(ltrim(rtrim(khppzd_dqbh)),2) ‘
        exec(@vssql)
        if @@error <> 0
            begin

            select @serror = ‘2 更新非协议客户的客户名称  客户名称==匹配客户名称,-省内‘
            raiserror(@sError,16,1)
            return -1
        end        

        --省外
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ Where ltrim(rtrim(yjlx_khmc))=ltrim(rtrim(khppzd_khmc)) ‘
        set @vssql += ‘ and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null )    ‘

        exec(@vssql)
        if @@error <> 0
            begin

            select @serror = ‘2 更新非协议客户的客户名称  客户名称==匹配客户名称,--省外‘
            raiserror(@sError,16,1)
            return -1
        end        

--3 根据客户关键字 更新协议客户编号\地区编号
    --先在本地区匹配
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ Where ‘
        set @vssql += ‘   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘ ‘
        set @vssql += ‘ and  yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘ ) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘ and left(ltrim(yjlx_gysdqbh),2)=left(ltrim(khppzd_dqbh),2) ‘

        exec(@vssql)
        if @@error <> 0
            begin

            select @serror = ‘-3 根据客户关键字 更新协议客户编号\地区编号,本地区匹配‘
            raiserror(@sError,16,1)
            return -1
        end
--根据地区字典匹配
    --3.1 市内匹配,客户名称包含有地区名称关键字(zwdqzd_bz=‘0/3‘匹配)
   set @vssql= ‘UPDATE ‘+@lsbm + ‘ set  yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
   set  @vssql += ‘ From lrdqzd ‘
   set  @vssql += ‘Where yjlx_dqbh=‘‘‘‘ ‘
   set  @vssql += ‘ and left(yjlx_gysdqbh,4)=left(zwdqzd_dqbh,4) ‘
   set  @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+zwdqzd_dqmc+‘‘%‘‘ ‘
   set  @vssql += ‘ and (zwdqzd_bz=‘‘0‘‘ or zwdqzd_bz=‘‘3‘‘) ‘
   exec(@vssql)
   if @@error <> 0
    begin
    rollback
    select @serror = ‘1 市内匹配,客户名称包含有地区名称关键字‘
    raiserror(@sError,16,1)
    return -1
   end

   --3.2 市内匹配,客户名称包含(地区名称-1)的关键字(zwdqzd_bz=‘0/3‘匹配)
    set @vssql= ‘ UPdate ‘+ @lsbm
    set @vssql += ‘ set yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
    set @vssql += ‘ From lrdqzd ‘
    set @vssql += ‘ Where yjlx_dqbh=‘‘‘‘  ‘
    set @vssql += ‘ and left(yjlx_gysdqbh,4)=left(zwdqzd_dqbh,4)  ‘
    set @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+left(zwdqzd_dqmc,len(zwdqzd_dqmc)-1)+‘‘%‘‘‘
    set @vssql += ‘ and (zwdqzd_bz=‘‘0‘‘ or zwdqzd_bz=‘‘3‘‘) ‘
    exec(@vssql)
   if @@error <> 0
    begin
    rollback
    select @serror = ‘2 市内匹配,客户名称包含(地区名称-1)的关键字(zwdqzd_bz=匹配)‘
    raiserror(@sError,16,1)
    return -1
   end    

    --3.3 //市内,客户名称包含有地区名称全称(zwdqzd_bz=‘1‘匹配)

    set    @vssql = ‘ UPdate ‘+ @lsbm
    set    @vssql += ‘ set yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
    set    @vssql += ‘ From lrdqzd ‘
    set    @vssql += ‘ Where yjlx_dqbh=‘‘‘‘  ‘
    set    @vssql += ‘ and left(yjlx_gysdqbh,4)=left(zwdqzd_dqbh,4)  ‘
    set    @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+zwdqzd_dqmc +‘‘%‘‘ ‘
    set    @vssql += ‘ and zwdqzd_bz=‘‘1‘‘ ‘
    exec(@vssql)

   if @@error <> 0
    begin
    rollback
    select @serror = ‘3 //市内,客户名称包含有地区名称全称(zwdqzd_bz=匹配)‘
    raiserror(@sError,16,1)
    return -1
   end
       --3.4//省内,客户名称包含有地区名称关键字(zwdqzd_bz=‘0‘匹配)
    set @vssql = ‘ UPdate ‘+ @lsbm
    set @vssql += ‘ set yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
    set @vssql += ‘ From lrdqzd ‘
    set @vssql += ‘ Where yjlx_dqbh=‘‘‘‘ ‘
    set @vssql += ‘ and left(yjlx_gysdqbh,2)=left(zwdqzd_dqbh,2)  ‘
    set @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+left(zwdqzd_dqmc,len(zwdqzd_dqmc) - 1) +‘‘%‘‘ ‘
    set @vssql += ‘ and (zwdqzd_bz=‘‘0‘‘ or zwdqzd_bz=‘‘3‘‘) ‘
    exec(@vssql)
   if @@error <> 0
    begin
    rollback
    select @serror = ‘4//省内,客户名称包含有地区名称关键字(zwdqzd_bz=匹配)‘
    raiserror(@sError,16,1)
    return -1
   end    

    ----3.5//省内,客户名称包含有地区名称全称(zwdqzd_bz=‘‘1‘‘匹配)
    set @vssql = ‘ UPdate ‘+ @lsbm
    set @vssql += ‘ set yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
    set @vssql += ‘ From lrdqzd ‘
    set @vssql += ‘ Where yjlx_dqbh=‘‘‘‘  ‘
    set @vssql += ‘ and left(yjlx_gysdqbh,2)=left(zwdqzd_dqbh,2)  ‘
    set @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+zwdqzd_dqmc+‘‘%‘‘ ‘
    set @vssql += ‘ and zwdqzd_bz=‘‘1‘‘ ‘
    exec(@vssql)
   if @@error <> 0
    begin
    rollback
    select @serror = ‘5//省内,客户名称包含有地区名称全称(zwdqzd_bz=‘‘1‘‘匹配)‘
    raiserror(@sError,16,1)
    return -1
   end    

    --3.6    //省内级数为1 ,客户名称前2位=地区名称前2位
    set @vssql = ‘ UPdate ‘+ @lsbm
    set @vssql += ‘ set yjlx_dqbh=zwdqzd_dqbh,yjlx_tzry=zwdqzd_ywy,yjlx_tzrymc=zwdqzd_ywymc ‘
    set @vssql += ‘ From lrdqzd ‘
    set @vssql += ‘ Where yjlx_dqbh=‘‘‘‘  ‘
    set @vssql += ‘ and zwdqzd_js=‘‘1‘‘ and left(yjlx_khmc,2)=left(zwdqzd_dqmc,2) ‘
    set @vssql += ‘ and zwdqzd_bz =‘‘1‘‘‘
    set @vssql += ‘ and left(zwdqzd_dqbh,2)=‘‘‘
    set @vssql += @dqbh
    set @vssql += ‘‘‘‘
    exec(@vssql)
   if @@error <> 0
    begin
    rollback
    select @serror = ‘6    //省内级数为1 ,客户名称前2位=地区名称前2位‘
    raiserror(@sError,16,1)
    return -1
   end
   --3.4 / 乡镇地区字典匹配 XZQHZD  ERP_DQBH------2013-04-08--*/
    set @vssql = ‘ UPdate ‘+ @lsbm
    set @vssql += ‘ set yjlx_dqbh=ERP_DQBH ‘
    set @vssql += ‘ From XZQHZD ‘
    set @vssql += ‘ Where yjlx_dqbh=‘‘‘‘  ‘
    set @vssql += ‘ and left(yjlx_gysdqbh,4)=left(erp_dqbh,4)  ‘
    set @vssql += ‘ and yjlx_khmc like ‘‘%‘‘+left(NAME_XIANG,len(NAME_XIANG) - 1) +‘‘%‘‘ ‘
    exec(@vssql)    

   if @@error <> 0
    begin
    rollback
    select @serror = ‘7 / 乡镇地区字典匹配 XZQHZD  ERP_DQBH------2013-04-08--*‘
    raiserror(@sError,16,1)
    return -1
   end    

        --后在全国匹配
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘ Set yjlx_khbh=khppzd_khbh,yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘1‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ Where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘ ‘
        set @vssql += ‘ and   yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘ and (khppzd_js=‘‘2‘‘ or khppzd_js=‘‘1‘‘ or khppzd_js=‘‘3‘‘ ) ‘
        set @vssql += ‘ and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘

        exec(@vssql)
        if @@error <> 0
            begin

            select @serror = ‘-3 根据客户关键字 更新协议客户编号\地区编号,全国匹配‘
            raiserror(@sError,16,1)
            return -1
        end        

--4 根据客户关键字 更新非协议客户编号\地区编号
    --先在本地区匹配
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘  set yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ Where   ‘
        set @vssql += ‘    yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘‘
        set @vssql += ‘  and  yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘  and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘  and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘
        set @vssql += ‘  and left(ltrim(yjlx_gysdqbh),2)=left(ltrim(khppzd_dqbh),2) ‘
        exec(@vssql)
        if @@error <> 0
            begin

            select @serror = ‘4 根据客户关键字 更新非协议客户编号\地区编号,本地区匹配‘
            raiserror(@sError,16,1)
            return -1
        end

        --后在全国匹配
        set @vssql = ‘ Update ‘ +@lsbm
        set @vssql += ‘  set yjlx_dqbh=khppzd_dqbh,yjlx_xykh=‘‘0‘‘,yjlx_ppkhmc=khppzd_khmc ‘
        set @vssql += ‘ From khppzd ‘
        set @vssql += ‘ Where yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz1))+‘‘%‘‘ ‘
        set @vssql += ‘  and  yjlx_khmc like ‘‘%‘‘+ltrim(rtrim(khppzd_gjz2))+‘‘%‘‘ ‘
        set @vssql += ‘  and (khppzd_js=‘‘4‘‘ or khppzd_js=‘‘5‘‘ or khppzd_js=‘‘6‘‘) ‘
        set @vssql += ‘  and (ltrim(rtrim(yjlx_khbh))=‘‘‘‘ or yjlx_khbh is null ) ‘

        exec(@vssql)

        if @@error <> 0
            begin

            select @serror = ‘4 根据客户关键字 更新非协议客户编号\地区编号,在全国匹配‘
            raiserror(@sError,16,1)
            return -1
        end

END
时间: 2024-10-05 09:09:07

更新地区号存储过程的相关文章

中国移动归属地区号大全

北京市 010 上海市 021 天津市 022 重庆市 023 香港 852 澳门 853 河北省 邯郸市 0310 石家庄 0311 保定市 0312 张家口 0313 承德市 0314 唐山市 0315 廊坊市 0316 沧州市 0317 衡水市 0318 邢台市 0319 秦皇岛 0335 浙江省 衢州市 0570 杭州市 0571 湖州市 0572 嘉兴市 0573 宁波市 0574 绍兴市 0575 台州市 0576 温州市 0577 丽水市 0578 金华市 0579 舟山市 058

系统视图,系统表,系统存储过程的使用

获取数据库中用户表信息 1.获取特定库中所有用户表信息 select * from sys.tables select * from sys.objects  where type='U'  --用户表 第二条语句中当type='S'时是系统表 2.获取表的字段信息 select * from sys.columns where object_id=object_id('表名') select * from syscolumns where id=OBJECT_ID('表名' ) 3.获取当前库

mysql存储过程及(java调用)

create procedure DeleteDataByPdfDocId (    IN pdfDocId varchar(100) ) BEGIN #根据pdfDocId删除数据 DELETE FROM pdf_formula WHERE PDF_DOCID = pdfDocId ; DELETE FROM pdf_image WHERE PDF_DOCID = pdfDocId ; DELETE FROM pdf_table WHERE PDF_DOCID = pdfDocId ; DEL

微信公众号退款开发

博主是小菜鸟,这篇文章仅是自己开发的随笔记录,不足博友可以指出来,一起进步 1.[微信支付]公众号支付开发者文档链接地址 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_4 调用微信退款接口,需要发送特定的xml格式字符串到到微信退款接口: 2.微信申请退款需要双向证书, JAVA只需要使用apiclient_cert.p12即可,证书从 https://pay.weixin.qq.com/index.php/core/hom

Java调用SQL Server的存储过程详解

转载自Microsoft的官方文档 http://msdn2.microsoft.com/zh-cn/library/ms378995.aspx收录于 www.enjoyjava.net/f25 本文较长,包含了如下几部分 使用不带参数的存储过程 使用带有输入参数的存储过程 使用带有输出参数的存储过程 使用带有返回状态的存储过程 使用带有更新计数的存储过程 1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序列.不带参数的 call 转义

Entity Framework 6 Recipes 2nd Edition(10-7)译 -&gt; TPH继承模型中使用存储过程

10-7. TPH继承模型中使用存储过程 问题 用一个存储过程来填充TPH继承模型的实体 解决方案 假设已有如Figure 10-7所示模型. 我们有两个派生实体: Instructor(教员)和Student(学生). 这个模型使用TPH继承方式,所以数据库中只有一个表. Person(人员)表有一个鉴别列,用来把表的记录映射到不同的派生实体上. 我们想用一个存储过程来填充实体. Figure 10-7. A model for instructors and students 用下面的步骤,

IBatis.Net学习笔记十三:在IBatis.Net中调用存储过程

其实调用方式比较简单,主要也就是两种类型的存储过程:1.更新类型的存储过程2.查询类型的存储过程下面就来看看具体的调用方式:1.更新类型的存储过程sp_InsertAccount: CREATE PROCEDURE [dbo].[sp_InsertAccount]    -- Add the parameters for the stored procedure here   @Account_ID int,   @Account_FirstName varchar(32),   @Accoun

【转】几个常用的Oracle存储过程

http://blog.bossma.cn/database/some-oracle-storing-process/ 几个常用的Oracle存储过程 发布时间:2008年1月6日 / 分类:DataBase / 7,268 次浏览 / 评论 初学Oracle,了解了一下Oracle的存储过程,运行了几个例子,做个备忘,方便以后使用:1.插入数据:向一张表里插入若干条数据 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

MVC5 Entity Framework学习之异步和存储过程

在之前的文章中,你已经学习了如何使用同步编程模型来读取和更新数据,在本节中你将学习如何实现异步编程模型.异步可以使应用程序执行更有效率,因为它可以更有效的使用服务器资源. 同样在本节中你还将学习如何针对实体的insert, update, 和delete操作使用存储过程. 最后将应用程序部署到 Windows Azure. 下面是完成后的页面 为什么要使用异步代码 一个web服务器的可用线程是有限的,在高负载情况下,所有的可用线程可能都在被使用.当出现这种情况时,服务器将无法处理新的请求,直到有