[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语句时,除了返回对应的结果集外,还会 ...
随机推荐
- org.json 使用
org.json 和 json-lib 使用上差不多. //JSONObject public static void main(String[] args) { String jsonObjectS ...
- CSS3 伸缩布局盒模型记
CSS3 伸缩布局盒模型 CSS3引入的布局模式Flexbox布局,主要思想是让容器有能力让其子项目能够改变其宽度,高度,以最佳方式填充可用空间.Flex容器使用Flex项目可以自动放大与收缩,用来填 ...
- Git CMD - reset: Reset current HEAD to the specified state
命令格式 git reset [-q] [<tree-ish>] [--] <paths>… git reset (--patch | -p) [<tree-ish&g ...
- 为Widget添加事件
原帖:http://bbs.51cto.com/thread-965565-1-1.html 在appWidget中,ImageButton和Button都是被支持的控件,其事件可分成三种类型: ...
- Git sparse checkout
Git的sparse checkout在clone项目仓库时只clone指定路径下的信息. 步骤如下: (1) mkdir yourdir(2) cd yourdir(3) git init(4) g ...
- RNN-theano代码解析
import theano import numpy import os import pdb from theano import tensor as T from collections impo ...
- Oracle数据库学习第一天
crud 增删改查 create read update delete1--oracle数据库的安装 系统默认创建两个用户 sys和system 密码自定义 超级管理员:sys 管理员:system ...
- 采用HttpModules来重写URLS
首先写一个处理URLs重写的类,并且这个类必须继承IHttpHandler接口,以博客园的程序为例: public class UrlReWriteModule : System.Web.IHttpM ...
- FileZilla ftp服务器安装
在官网下载server版本的FileZilla,我下载的是(The latest stable version of FileZilla Server is 0.9.41) 然后直接安装就可了,我由于 ...
- (hdu)1257 最少拦截系统
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1257 Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦 ...