[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语句时,除了返回对应的结果集外,还会 ...
随机推荐
- 关于Eclipse插件之IWorkbench IWorkbenchWindow IWorkbenchPage |WorkbenchPart......等的总结
1..IWorkbench: workbench是eclipse用户界面中最底层的对象,它建立在Display之上,包含一个或多个IWorkbenchWindow,用于向终端用户呈现信息 当你的wor ...
- The message queue
今晚来整理一下消息队列,消息队列是一条由消息连接而成的链表,存在内核里面,通过消息对了的引用标识符来访问,每个消息队列都有一个msqid_ds结构与之对应,这个结构保存了消息队列的当前状态参数,这个结 ...
- 纯CSS3制作进度条源代码
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- JMS - ExceptionListener
If a JMS provider detects a problem with a connection, it will inform the connection’s ExceptionList ...
- HTTP - 持久连接
Web 客户端经常会打开到同一个站点的连接.比如,一个 Web 页面上的大部分内嵌图片通常都是来自同一个 Web 站点,而且相当一部分指向其他对象的超链接通常都指向同一个站点.因此,初始化了对某服务器 ...
- CSS之显示天气
这个可以有,自从有了这个,以后查询天气就方便多了,哈哈. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...
- 第三十篇、iOS开发中常用的宏
//字符串是否为空 #define kStringIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str leng ...
- phpmyadmin安装教程及配置设置
phpmyadmin安装教程及配置设置 | 浏览:20304 | 更新:2013-11-07 09:50 1.一般网上下载到的phpmyadmin是一个压缩包,我们将其释放到htdocs目录中,例如h ...
- jqGrid Tree
CSS: <!--jqGrid--><link rel="stylesheet" href="plugins/jqgird/css/ui.jqgrid. ...
- Json对象序列化与反序列化
如果后台的参数数对象,需要在前台传入: JS代码: //创建JS对象 var CUTTING_TABLET_MO = new Object(); CUTTING_TABLET_MO.CUTTING_T ...