实现功能:新增特定类型的新闻时,自动追加特定的背景图片。

第一版(错误信息:不能在 '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

以上插入触发器代码虽然没有报错,也实现了效果,但是存在漏洞。

  1. ntext转nvarchar(max)是否会有数据丢失
  2. 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触发器编写过程的更多相关文章

  1. 记一次SQL Server insert触发器操作

    需求:在河道水情表(ST_RIVER_R )新增插入数据时,更新实时数据表(SS_data) 中关联字段的值. 需求概括下:当A表中新增数据时,同时更新B表中的某字段 代码如下: USE [DBCNB ...

  2. 记一次sql server 2005访问http接口,并解析json的过程

    记一次sql server 2005访问http接口,并解析json的过程  JSON解析官方网站:https://www.red-gate.com/simple-talk/sql/t-sql-pro ...

  3. SQL Server Insert操作中的锁

    原文:SQL Server Insert操作中的锁 这篇博文简单介绍一下在SQL Server中一条Insert语句中用到的锁. 准备数据 首先我们建立一张表Table_1,它有两列Id(bigint ...

  4. 【SQL SERVER】触发器(一)

    下面是个人对触发器知识的整理,触发器其实很简单,但想要编写发杂的触发器操作还是需要一定的SQL语句编写,触发器主要用于SQL SERVER约束.默认值和规则的完整性检查,还可以实现由主键和外键不能保证 ...

  5. SQL Server DDL触发器运用

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 基础知识(Rudimentary Knowledge) DDL运用场景(DDL Scene) ...

  6. SQL Server:触发器详解

    1. 概述 触发器是一种特殊的存储过程,它不能被显式地调用,而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活. 所以触发器可以用来实现对表实施复杂的完整性约束. 2. 触发器的分类 SQL S ...

  7. 数往知来SQL SERVER 视图 触发器 <九>

    SQL server学习_视图 1.视图 视图其实是一张虚拟表,他是一张表的部分数据或多张表的综合数据(视图就是把SQL语句封装起来) 可以看做是一个结果集,但是不是一个结果集 视图不具备存储数据的能 ...

  8. SQL SERVER TRIGGER 触发器

    1.触发器简介 触发器是一种特殊的存储过程,它的执行不是由程序调用,也不是手动执行,而是由事件来触发.触发器是当对某一个表进行操作.例如:update.insert.delete这些操作的时候,系统会 ...

  9. SQL Server 使用触发器(trigger)发送电子邮件

    sql 使用系统存储过程 sp_send_dbmail 发送电子邮件语法: sp_send_dbmail [ [ @profile_name = ] 'profile_name' ] [ , [ @r ...

随机推荐

  1. Opencv3.4:显示一张图片

    Github https://github.com/gongluck/Opencv3.4-study.git #include "opencv2/opencv.hpp" using ...

  2. 「PKUSC2018」最大前缀和(状压dp)

    前言 考试被\(hyj\)吊着打... Solution 考虑一下如果前缀和如果在某一个位置的后面的任意一个前缀和都<=0,肯定这就是最大的. 然后这样子就考虑左右两边的状压dp,然后就好了. ...

  3. OCP考试062题库出现大量新题-18

    choose two Examine this command executed on a client that is remote from the database server. SQL> ...

  4. pycharm光标变成黑框,恢复成竖线

    pycharm光标变成黑框,如下图所示 解决办法: 按外接键盘上的Insert键,即可恢复.

  5. SpringCloud之Eureka集群

    前面我们介绍了SpringCloud注册中心Eureka,但是存在一个单点故障的问题,一个注册中心远远不能满足实际的生产环境,现在我们介绍一下如何搭建一个Eureka集群. 一:集群环境搭建 我们先建 ...

  6. sudoer命令各参数含义及设置

    对于普通用户sudo加权的时限制可以执行的命令 https://segmentfault.com/a/1190000007394449 需要修改/etc/sudoers 1. 字段说明如下 授权用户/ ...

  7. pip指定网址安装

    用pip安装库的有时候很慢都动不了 ,访问速度很慢,不稳定等缺陷 所以呢为了解决这个问题只能指定网址源下载的话速度就很快了 pip安装默认访问的是https://pypi.Python.org/sim ...

  8. SQLAlchemy 快速入门、基础知识

    SQLAlchemy 是Python 编程语言下的一款开源软件.提供了SQL工具包及对象关系映射(ORM)工具. ORM, 全称Object Relational Mapping, 中文叫做对象关系映 ...

  9. flask信号使用

    flask信号: 安装: flask中的信号使用的是一个第三方插件,叫做blinker.通过pip list看一下,如果没有安装,通过以下命令即可安装blinker: pip install blin ...

  10. 用AOP思想改造一个服务器的数据存储

    背景是有一个游戏服务器一直以来都是写SQL的, 后来改过一段时间的redis, 用的是别的员工写的类orm方式将实体类型映射成各种key-value对进行写入, 但是仍有一个缺点就是需要在增\删\改的 ...