[SQL SERVER系列]存储过程,游标和触发器实例[原创]
自己写的存储过程与游标结合使用的实例,与大家分享,也供自己查阅,仅供参考:
--使用游标循环处理,删除重复的记录
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系列]存储过程,游标和触发器实例[原创]的更多相关文章
- Sql Server系列:游标
1. 游标简介 游标是一种处理数据的方法,主要用于存储过程.触发器和Transact-SQL脚本中.SELECT语句返回的是一个结果集,游标能够从包含多条数据记录的结果集中每次提取一条记录. 游标的特 ...
- Sql Server系列:触发器
触发器的一些常见用途: ◊ 强制参照完整性 ◊ 常见审计跟踪(Audit Trails):这意味着写出的记录不仅跟踪大多数当前的数据,还包括对每个记录进行实际修改的历史数据. ◊ 创建与CHECK约束 ...
- 查看SQL SERVER 加密存储过程,函数,触发器,视图
原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...
- VFP获取 SQL Server 的数据表、触发器、存储过程、视图等脚本
本文代码转载自红雨先生 *-----------------------------------------------* SqlServer 相关函数*----------------------- ...
- Sql Server系列:存储过程
1 存储过程简介 存储过程是使用T-SQL代码编写的代码段.在存储过程中,可以声明变量.执行条件判断语句等其他编程功能.在MS SQL Server 2012中存储过程主要分三类:系统存储过程.自定义 ...
- sql server系统存储过程大全
关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...
- SQL Server基础之游标
查询语句可能返回多条记录,如果数据量非常大,需要使用游标来逐条读取查询结果集中的记录.应用程序可以根据需要滚动或浏览其中的数据.本篇介绍游标的概念.分类.以及基本操作等内容. 一:认识游标 游标是 ...
- 在易语言中调用MS SQL SERVER数据库存储过程方法总结
Microsoft SQL SERVER 数据库存储过程,根据其输入输出数据,笼统的可以分为以下几种情况或其组合:无输入,有一个或多个输入参数,无输出,直接返回(return)一个值,通过output ...
- SQL Server 优化存储过程的七种方法
原文:SQL Server 优化存储过程的七种方法 优化存储过程有很多种方法,下面介绍最常用的7种. 1.使用SET NOCOUNT ON选项 我们使用SELECT语句时,除了返回对应的结果集外,还会 ...
随机推荐
- 关于使用vss版本管理工具中的sln,suo文件作用
Visual Studio.NET采用两种文件类型(.sln和.suo)来存储特定于解决方案的设置,它们总称为解决方案文件.为解决方案资源管理器提供显示管理文件的图形接口所需的信息 从而在每次继续开发 ...
- The message queue
今晚来整理一下消息队列,消息队列是一条由消息连接而成的链表,存在内核里面,通过消息对了的引用标识符来访问,每个消息队列都有一个msqid_ds结构与之对应,这个结构保存了消息队列的当前状态参数,这个结 ...
- Linux下is not in the sudoers file解决方法
最近在学习linux,在某个用户(xxx)下使用sudo的时候,提示以下错误:xxx is not in the sudoers file. This incident will be reporte ...
- java url中文 编译和解码
js到servlet: js端 var minename='中文'; minename=encodeURI(encodeURI(minename)); java servlet 端 String na ...
- web前端常用小函数汇总
//去掉html标签 function delHtmlTag(str) { var title = str.replace(/<[^>]+>/g, "");// ...
- 利用google api生成二维码名片例子
二维条码/二维码可以分为堆叠式/行排式二维条码和矩阵式二维条码.堆叠式/行排式二维条码形态上是由多行短截的一维条码堆叠而成:矩阵式二维条码以矩阵的形式组成,在矩阵相应元素位置上用“点”表示二进制“1” ...
- Xcode7中添加3DTouch
首先是插件SBShortcutMenuSimulator的安装 1.git clone https://github.com/DeskConnect/SBShortcutMenuSimulator.g ...
- iOS预处理指令
预处理过程扫描源代码,对其进行初步的转换,产生新的源代码提供给编译器.可见预处理过程先于编译器对源代码进行处理. 预处理指令是以#开头的代码行,#后是指令关键字,在关键字和#号之间允许存在任意个数的空 ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
- javascript笔记——正则表达式学习笔记
indexof 查找 substring 获取字符串 [) charAt 获取某个字符 split 分割字符串,获得数组 \s:空格 \S:非空格 \d:数字 \D:非数字 \w:字符 \W:非字符 ...