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

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

    效果图: 下载链接:创建wpf项目引用 如下:链接:链接: https://pan.baidu.com/s/1UCJmslLPSDph7VrYhXM9gw 密码: j3n4 链接: https://p ...

  2. C# 动态创建SQL数据库(二)

    使用Entity Framework  创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关系映射来创建数据库与表 一 新建项 ...

  3. .NET MVC中如何使用手机验证码注册登陆

    #region 手机验证码 /// <summary> /// /// </summary> /// <param name="Yonghushouji&quo ...

  4. dubbo-admin 出现警告(不影响使用)

    <dubbo:application name="pyg-sellergoods-s" />. <dubbo:application name="pyg ...

  5. WCF:初识

    结构: using System.ServiceModel; namespace MyServices { [ServiceContract] public interface IHomeServic ...

  6. requests请求例子

    实例一: class GetSalerInfo(View): def post(self, request): userid = request.POST/GET.get('userid',None) ...

  7. npm私服搭建

    本文是在 centos7 下利用 nexus 搭建 npm 私服的整理 一.安装 JDK 1.下载 JDK 2.安装 tar zxvf jdk-8u191-linux-x64.tar.gz .0_19 ...

  8. flask开发微信公众号

    1.进入微信公众号首页,进行注册登录 https://mp.weixin.qq.com/ 2.进入个人首页,进行公众号设置 可参照 公众号文档 进行开发 开发前 先阅读 接口权限列表 3.配置服务器 ...

  9. oracle 异常关闭操作 导致数据库无法正常关闭 也无法启动

    场景描述: 在关闭数据库的时候,命令没有打全,导致数据库没有正常关闭 解决办法: 重新建立个连接,然后切换到oracle用户 执行强制关闭数据库: OK 问题解决,不过生产环境 还是不推荐 shutd ...

  10. java8之lambda表达式(2)-方法引用

    方法引用使用的地方也是在函数式接口,使用方法引用可以使代码更加简单和便捷 在如下代码中 根据List中字符串长度排序的代码可以写成如下: public static void test1_() { L ...