自己写的存储过程与游标结合使用的实例,与大家分享,也供自己查阅,仅供参考:

--使用游标循环处理,删除重复的记录
declare @UserID int
declare @UserName varchar(32)
declare @RealName varchar(32)
declare @UnitFlag int
declare @Email2 varchar(64)
declare @Mobile varchar(64)
declare @Start int
declare @End int
declare @Type varchar(16)
declare @IsSubscribe bit
declare curEmailTotalLib cursor
for (select UserID,UserName,RealName,UnitFlag,Email,Mobile,IsSubscribe from Task_IntermediateData)
open curEmailTotalLib --打开游标
fetch next from curEmailTotalLib into @UserID, @UserName, @RealName, @UnitFlag, @Email2, @Mobile, @IsSubscribe
while @@fetch_status = 0 --获取成功
begin
--在邮件系统总库中不存在此用户ID,不存在此邮箱,并且用户订阅过
if not exists(select * from Task_EmailTotalLib where UserID = @UserID)
and not exists(select * from Task_EmailTotalLib where Email = @Email2)
and @IsSubscribe = 1
begin
set @Start = charindex('@', @Email2, 0)
set @End = charindex('.', @Email2, @Start)
if @Start != 0 and @End != 0
begin
--不是垃圾邮件
if @Email2 is not null and ltrim(rtrim(@Email2)) <> ''
begin
if not exists(select * from Task_JunkEmail where Email = @Email2)
begin
begin try
set @Type = substring(@Email2, @Start + 1, @End - @Start - 1)
if @Type != 'qq' and @Type!='' and @Type != '' and @Type!='sina'
and @Type !='sohu' and @Type != 'gmail' and @Type!='hotmail' and
@Type != 'yahoo' and @Type != '' and @Type != '' and
@Type !='yeah' and @Type != 'cnki'
begin
set @Type = 'extra'
end
insert into Task_EmailTotalLib(UserID, UserName, RealName, Email, Mobile,
Priority, MailType, LibType, FpIsSend, CpIsSend, UpIsSend, VpIsSend, WpIsSend,
XpIsSend, YpIsSend, ZpIsSend, SendCount, SucCount, FailCount, CreditRate, IsJunkEmail,
IsSubscribe, IsUsed, Memo) values(@UserID, @UserName, @RealName, @Email2, @Mobile,
2, @Type, @UnitFlag, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, @IsSubscribe, 0, '')
end try
begin catch
print '@Email2:'+@Email2+'charindex(''@'', @Email2, 0)'+charindex('@', @Email2, 0)+' @Start'+@Start+' @End'+@End+' @End - @Start - 1:'+@End - @Start - 1
end catch
end
end
end
end
fetch next from curEmailTotalLib into @UserID, @UserName, @RealName, @UnitFlag, @Email2, @Mobile, @IsSubscribe
end
close curEmailTotalLib --关闭游标
deallocate curEmailTotalLib --释放游标

  触发器实例:插入数据时,触发器获取这条数据ID,自动修改,比程序处理更方便。

  Create trigger tg_url_update  on [dbo].UrlTotal for insert as
    declare @getid int; 
    declare @url varchar(128);
    set @getid=(select id from inserted);
    set @url='Test.aspx?id='+cast(@getid as varchar(50)) 
    update UrlTotal set url=@url where id=@getid

  

谢谢阅读~~

[SQL SERVER系列]存储过程,游标和触发器实例[原创]的更多相关文章

  1. Sql Server系列:游标

    1. 游标简介 游标是一种处理数据的方法,主要用于存储过程.触发器和Transact-SQL脚本中.SELECT语句返回的是一个结果集,游标能够从包含多条数据记录的结果集中每次提取一条记录. 游标的特 ...

  2. Sql Server系列:触发器

    触发器的一些常见用途: ◊ 强制参照完整性 ◊ 常见审计跟踪(Audit Trails):这意味着写出的记录不仅跟踪大多数当前的数据,还包括对每个记录进行实际修改的历史数据. ◊ 创建与CHECK约束 ...

  3. 查看SQL SERVER 加密存储过程,函数,触发器,视图

    原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create  PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...

  4. VFP获取 SQL Server 的数据表、触发器、存储过程、视图等脚本

    本文代码转载自红雨先生 *-----------------------------------------------* SqlServer 相关函数*----------------------- ...

  5. Sql Server系列:存储过程

    1 存储过程简介 存储过程是使用T-SQL代码编写的代码段.在存储过程中,可以声明变量.执行条件判断语句等其他编程功能.在MS SQL Server 2012中存储过程主要分三类:系统存储过程.自定义 ...

  6. sql server系统存储过程大全

    关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...

  7. SQL Server基础之游标

    查询语句可能返回多条记录,如果数据量非常大,需要使用游标来逐条读取查询结果集中的记录.应用程序可以根据需要滚动或浏览其中的数据.本篇介绍游标的概念.分类.以及基本操作等内容. 一:认识游标   游标是 ...

  8. 在易语言中调用MS SQL SERVER数据库存储过程方法总结

    Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接返回(return)一个值,通过output ...

  9. SQL Server 优化存储过程的七种方法

    原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...

随机推荐

  1. 【贪心+一点小思路】Zoj - 3829 Known Notation

    借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...

  2. 【补】【FZU月赛】【20150515】【待续】

    A FZU-2054 水题,比较A,B双方的最大值即可. B FZU-2055 string,截取‘.’之前和之后然后和给出的文件夹名和拓展名比较就好了啊,不明白为什么那么多人错. 代码: #incl ...

  3. TCP基础知识

    TCP/IP网络协议栈分为应用层(Application).传输层(Transport).网络层(Network)和链路层(Link)四层.如下图所示 两台计算机通过TCP/IP协议通讯的过程如下所示 ...

  4. Delphi IP 控件源码

    interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,ComCtrls, Co ...

  5. 第六十三篇、runtime实现归解档

    #import <objc/runtime.h> #import <objc/message.h> @implementation HDFArchiveModel - (voi ...

  6. 第五十六篇、OC打开本地和网络上的word、ppt、excel、text等文件

    iOS打开本地和网络上的word.ppt.excel.text等文件 iOS开发过程中可能需要预览一些文件,这些文件的格式可能有word.ppt.excel等文件格式.那么系统提供两个类去预览这些文件 ...

  7. 第三十九篇、NavBar动态隐藏、设置透明、毛玻璃效果

    1.动态隐藏 - (void)viewDidLoad { [super viewDidLoad]; if ([self respondsToSelector:@selector(automatical ...

  8. OC1_类方法的内存管理

    // // Dog.h // OC1_类方法的内存管理 // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangxu ...

  9. 代码优化—From <effective C++>

    1.尽可能的延后变量定义式的出现时间 不止应该延后变量的定义,直到非得使用该变量的前一刻为止,甚至应该尝试延后这份定义直到能够给它初值实参为止. 如果这样不仅能够避免构造和析构非必要对象,还可以避免无 ...

  10. Poj 2840 Big Clock

    1.Link: http://poj.org/problem?id=2840 2.Content: Big Clock Time Limit: 1000MS   Memory Limit: 13107 ...