记一次SQL Server Insert触发器编写过程
实现功能:新增特定类型的新闻时,自动追加特定的背景图片。
第一版(错误信息:不能在 'inserted' 表和 'deleted' 表中使用 text、ntext 或 image 列),代码如下:
--创建insert插入类型触发器
if (object_id('tgr_news_QA_insert', 'tr') is not null)
drop trigger tgr_news_QA_insert
go create trigger tgr_news_QA_insert
on news
for insert --插入触发
as
--定义变量
declare @id bigint, @content nvarchar(max),@newstype bigint; --在inserted表中查询已经插入记录信息
select @id = id,
@content = [content],
@newstype = newstype
from inserted --处理问题解答数据
if @newstype=101
begin
set @content = '<div style="background:url(''images/newsBackgroundImg.jpg'') no-repeat;width:700px;height:600px; "><div width=''100%'' style=''text-align:left;''>'+@content+'</div></div>'
update news set content=@content where id=@id
end go
于是改成instead of insert触发器,代码如下:
--创建insert插入类型触发器
if (object_id('tgr_news_QA_insert', 'tr') is not null)
drop trigger tgr_news_QA_insert
go create trigger tgr_news_QA_insert
on news
instead of insert --插入触发
as
--定义变量
declare @id bigint, @content nvarchar(max),@newstype bigint; --在inserted表中查询已经插入记录信息
select @id = id,
@content = [content],
@newstype = newstype
from inserted --处理对应类型的数据
if @newstype=101
begin
set @content = '<div style="background:url(''images/newsBackgroundImg.jpg'') no-repeat;width:700px;height:600px; "><div width=''100%'' style=''text-align:left;''>'+@content+'</div></div>'
insert into news
select id, newstype, title, @content content,
from inserted
end
else
insert into news
select * from inserted
以上插入触发器代码虽然没有报错,也实现了效果,但是存在漏洞。
- ntext转nvarchar(max)是否会有数据丢失
- inserted 数据不一定只有一条
最终版:
--创建insert插入类型触发器
if (object_id('tgr_news_QA_insert', 'tr') is not null)
drop trigger tgr_news_QA_insert
go create trigger tgr_news_QA_insert
on news
instead of insert --插入触发
as
insert into news
select id,
newstype,
title,
'<div style="background:url(''images/newsBackgroundImg.jpg'') no-repeat;width:700px;height:600px; "><div width=''100%'' style=''text-align:left;''>'+convert(nvarchar(max),content)+'</div></div>',
from inserted
where newstype=101 insert into news
select id,
newstype,
title,
[content],
from inserted
where newstype<>101
关于SQL Server 2005中NTEXT与NVARCHAR(MAX)参考:http://www.cnblogs.com/dudu/archive/2009/10/17/1585152.html
记一次SQL Server Insert触发器编写过程的更多相关文章
- 记一次SQL Server insert触发器操作
需求:在河道水情表(ST_RIVER_R )新增插入数据时,更新实时数据表(SS_data) 中关联字段的值. 需求概括下:当A表中新增数据时,同时更新B表中的某字段 代码如下: USE [DBCNB ...
- 记一次sql server 2005访问http接口,并解析json的过程
记一次sql server 2005访问http接口,并解析json的过程 JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...
- SQL Server Insert操作中的锁
原文:SQL Server Insert操作中的锁 这篇博文简单介绍一下在SQL Server中一条Insert语句中用到的锁. 准备数据 首先我们建立一张表Table_1,它有两列Id(bigint ...
- 【SQL SERVER】触发器(一)
下面是个人对触发器知识的整理,触发器其实很简单,但想要编写发杂的触发器操作还是需要一定的SQL语句编写,触发器主要用于SQL SERVER约束.默认值和规则的完整性检查,还可以实现由主键和外键不能保证 ...
- SQL Server DDL触发器运用
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) DDL运用场景(DDL Scene) ...
- SQL Server:触发器详解
1. 概述 触发器是一种特殊的存储过程,它不能被显式地调用,而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活. 所以触发器可以用来实现对表实施复杂的完整性约束. 2. 触发器的分类 SQL S ...
- 数往知来SQL SERVER 视图 触发器 <九>
SQL server学习_视图 1.视图 视图其实是一张虚拟表,他是一张表的部分数据或多张表的综合数据(视图就是把SQL语句封装起来) 可以看做是一个结果集,但是不是一个结果集 视图不具备存储数据的能 ...
- SQL SERVER TRIGGER 触发器
1.触发器简介 触发器是一种特殊的存储过程,它的执行不是由程序调用,也不是手动执行,而是由事件来触发.触发器是当对某一个表进行操作.例如:update.insert.delete这些操作的时候,系统会 ...
- SQL Server 使用触发器(trigger)发送电子邮件
sql 使用系统存储过程 sp_send_dbmail 发送电子邮件语法: sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @r ...
随机推荐
- [NOI2017]蔬菜(贪心)
神仙题啊! 早上开了两个多小时,终于肝出来了,真香 我们考虑从第 \(10^5\) 天开始递推,先生成 \(p=10^5\) 的解,然后逐步推出 \(p-1,...,2,1\) 的解. 那怎么推出 \ ...
- 简单记录常用git 命令
声明:仅作笔记用 拉取远程代码 1.git pull 2.如果需要,输入账户名密码 将本地代码推送到远程 1.git push 2.如果需要,输入账户名密码 同步远程分支 1.git fetch 2. ...
- python学习笔记12-深浅拷贝
以上为浅拷贝. .copy()函数 赋值:数据完全共享(=赋值是在内存中指向同一个对象,如果是可变(mutable)类型,比如列表,修改其中一个,另一个必定改变 如果是不可变类型(immutable) ...
- Python网络练习题
练习题 什么是C/S架构? C/S架构客户端.服务端架构,C/S端软件主要有网络游戏,QQ等 互联网协议是什么?分别介绍五层协议中每一层的功能? 互联网协议:计算机之间的通信标准 物理层:主要是基于电 ...
- Linux发邮件
一.mail命令 1.配置 vim /etc/mail.rc 文件尾增加以下内容 set from=1968089885@qq.com smtp="smtp.qq.com"set ...
- (转)每天一个linux命令(21):find命令之xargs
原文:http://www.cnblogs.com/peida/archive/2012/11/15/2770888.html https://blog.csdn.net/ly1358152944/a ...
- c++三维静态数组的定义与作为函数的传递
在c++中,我们可以定义三维数组,并且可以将之作为参数直接传递. 定义: #include <iostream> #include <windows.h> using name ...
- [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associated with an element type "id".
用DOM解析XML时出现了如下错误: [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associa ...
- SQL Server性能优化(7)理解数据库文件组织
一.基本单位"页" SQL Server是用8KB的页来存储数据.物理I/O操作也是在页级执行.页的种类有很多,具体参考(MSDN).我们关注更多的是数据页的结构,包括三部 ...
- 杂谈:HTTP1.1 与 HTTP2.0 知多少?
HTTP是应用层协议,是基于TCP底层协议而来. TCP的机制限定,每建立一个连接需要3次握手,断开连接则需要4次挥手. HTTP协议采用"请求-应答"模式,HTTP1.0下,HT ...