mysql 存储过程参考 虽然不建议用存储过程,一个例子 用于自己参考

BEGIN

DECLARE done INT;

DECLARE v_companyName VARCHAR(100);
DECLARE v_phone VARCHAR(30);
DECLARE v_contactName VARCHAR(30);
DECLARE v_companyAddress VARCHAR(256);
DECLARE v_mail VARCHAR(50);
DECLARE v_fixphone VARCHAR(30);
DECLARE v_id INT;
DECLARE isInsert INT DEFAULT 0;  
DECLARE isRepeat INT DEFAULT 0; 
DECLARE isSuccessFlag INT DEFAULT 0; 
DECLARE curCount INT DEFAULT 0; 

DECLARE v_masterUserid int DEFAULT 10005;

-- 定义游标
DECLARE rs_cursor CURSOR FOR
SELECT id, trim(companyName), trim(phone), trim(contactName), trim(companyAddress), SUBSTRING_INDEX(trim(companyMailbox),'---','1') as mail,trim(fixedPhone) from crawl_data where status = 0 order by companyName;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

OPEN rs_cursor; 
    cursor_loop:LOOP
    FETCH rs_cursor INTO v_id,v_companyName,v_phone, v_contactName,v_companyAddress,v_mail,v_fixphone;  -- 取数据
    IF done=1 THEN
    leave cursor_loop;
    END IF;
    -- 逻辑处理
    -- 更新表
    set isInsert = 0;
    set isRepeat = 0;
set v_masterUserid = 10005;

    if (v_companyName is not null and v_phone is not null)  THEN  -- 公司名和手机存在时
             -- 判断数据库里是否重复
            if  (select count(1) from intendeduser_market m where m.companyName = v_companyName and (m.mobile = v_phone or m.mobileTwo = v_phone)) <= 0 THEN
                 -- 新增操作
                 set isInsert = 1;
            else 
                 set isRepeat = 1;
            end if; 
    ELSEif (v_companyName is not null and v_fixphone is not null)  THEN   -- 公司和联系电话存在时
                if(select count(1) from intendeduser_market m where m.companyName = v_companyName and m.tel = v_fixphone) <= 0 THEN
                     set isInsert = 1;
                else
                     set isRepeat = 1;
                end if;
         
    end IF;


    if isInsert=1 then
            if  (LOCATE('北京', v_companyName) > 0  or LOCATE('天津', v_companyName) > 0 or LOCATE('河北', v_companyName) > 0
                    or LOCATE('石家庄', v_companyName) > 0 or LOCATE('黑龙江', v_companyName) > 0 or LOCATE('吉林', v_companyName) > 0 or LOCATE('辽宁', v_companyName) > 0 or LOCATE('山西', v_companyName) > 0) THEN  -- 公司名匹配城市 为北京 天津 河北 石家庄 黑龙家 吉林 辽宁  山西 等
                 set v_masterUserid = 10245;
      end if;

         insert into intendeduser_market  (companyName,mobile,username,companyAddress,email,createsysUserId,tel,servicelog,servicelogTime,createtime,modifytime, marketUserId,remark) 
         values (v_companyName, v_phone, v_contactName,v_companyAddress, v_mail,  10071, v_fixphone, '系统导入', NOW(), NOW(),NOW(), v_masterUserid,'猫头鹰爬取');
      
    end if;


    set isSuccessFlag = 1;
    if isRepeat = 1 THEN
        set isSuccessFlag = 2;
    end if;

    update crawl_data c set c.status = isSuccessFlag where c.id = v_id;

    END LOOP;
CLOSE rs_cursor;

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
帮助那些想尽快学习存储过程 实例:create PROCEDURE pagination @tblName varchar(255), -- 表名 @strGetFields varchar(1000) = '*', -- 需要返回的列 @fldName varchar(255)='', -- 排序的字段名 @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @pagecount int output, -- 返回页总数, 非 0 值则返回 --@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1500) = '1=1' -- 查询条件 (注意: 不要加 where) AS declare @sql nvarchar(2000) --获得表中所有记录的条数 declare @recordcount int declare @getcountsql nvarchar(2000) set @getcountsql = N'select @count = count(*) from ' + @tblName + N' where ' + @strWhere exec sp_executesql @getcountsql,N'@count int output',@count=@recordcount output if @recordcount=0 begin set @pagecount = 0 return end declare @lastcount int set @lastcount = @recordcount % @PageSize if @lastcount = 0 set @pagecount = @recordcount / @PageSize else set @pagecount = @recordcount / @PageSize + 1 if @lastcount = 0 or @pageindex < @pagecount begin set @sql = N'select ' + @strGetFields + N' from (select top ' + convert(nvarchar(4),@PageSize) + N' * from (select top ' + convert(varchar(10),@PageSize*@PageIndex) + N' * from ' + @tblName + N' where ' + @strWhere + N' order by ' +@fldName+ N') as t order by ' + @fldName + ' desc) as tt order by ' + @fldName end else begin if @lastcount != 0 and @pageindex = @pagecount begin set @sql = N'select ' + @strGetFields + N' from (select top ' + convert(nvarchar(4),@lastcount) + N' * from (select top ' + convert(varchar(10),@PageSize*@PageIndex) + N' * from ' + @tblName + N' where ' + @strWhere + N' order by ' +@fldName+ N') as t order by ' + @fldName + ' desc) as tt order by ' + @fldName end end print @sql exec sp_executesql @sql go select * from authors declare @pagecount int exec pagination 'authors','*','au_id',5,1, @pagecount output,'state=''CA''' print @pagecount
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值