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

第一版(错误信息:不能在 '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. JMeter----正则表达式&JSON Path Extractor

    最近在用JMerter给公司一个项目做性能测试,期间遇到要提取上一个接口返回的数据作为下个接口的请求.这里做下记录 如图所示,需要将“扫描二维码”接口请求的返回值中的data部分,作为“处理提交码值” ...

  2. Hive Bug修复:ORC表中array数据类型长度超过1024报异常

    目前HVIE里查询如下语句报错: select * from dw.ticket_user_mtime limit 10; 错误如下: 17/07/06 16:45:38 [main]: DEBUG ...

  3. 输出的数据格式是如何决定的-------Asp.net WebAPI学习笔记(二)

    在上一篇文章<路由其实也可以很简单>,我们解决了路由问题,这篇文章,我们来研究剩下的另一个问题,为何我们的方法返回的是一个列表,输出到客户端的时候,变成json呢,大家应该还记得我们上一篇 ...

  4. WPF 嵌入Winform GDI 、 开启AllowsTransparenc问题

    此文章可以解决2至少2个问题: 1.开启AllowsTransparenc造成的GDI+组件不显示问题 2.WPF 组件无法覆盖嵌入WPF窗口的任何第三方GDI+组件上层 方案1:自制双层 原理:用一 ...

  5. WPF自定义TabControl样式

    WPF自定义TabControl,TabControl美化 XAML代码: <TabControl x:Class="SunCreate.Common.Controls.TabCont ...

  6. c#中的gcAllowVeryLargeObjects和OutOfMemoryException

    什么是gcAllowVeryLargeObjects 在.net4.5中新增一个配置项 “gcAllowVeryLargeObjects” ,msdn解释如下: 在64位平台上,可以允许总共大于2千兆 ...

  7. Linux例行工作与系统管理(13)

    Linux 系统的任务是由cron(crond)这个系统服务来控制的,Linux系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外,由于使用者自己也可以设置计划任务,所以Linux ...

  8. 讲讲我当年是怎么拿到AI研发公司offer的

    前言 很多的老铁私信问我,当年我是怎么拿到公司offer的,我记得我毕业是2015年,那时人工智能这个行业还没热起来,能提供的岗位很少但是面试的人更少,我又是本专业毕业的,所以当初找工作还算顺利,去面 ...

  9. file_put_contents 换行

    file_put_contents('test.text', json_encode($result) . PHP_EOL, FILE_APPEND);

  10. Vue2.5开发去哪儿网App 搜索功能完成

    效果展示: Search.vue: <div class="search-content" ref="search" v-show="keywo ...