记一次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 ...
随机推荐
- Mac下替代Total Commander的工具推荐
[推荐]:Nimble Commander 轻量小巧,免费版与收费版区别不大,比较稳定,支持sftp等其他网络存储,支持自定义热键,预览等. http://magnumbytes.com/ [其他]: ...
- Django:全文检索功能可参考博客
https://blog.csdn.net/AC_hell/article/details/52875927 https://www.zmrenwu.com/courses/django-blog-t ...
- Asp.Net Mvc异步上传文件的方式
今天试了下mvc自带的ajax,发现上传文件时后端action接收不到文件, Request.Files和HttpPostedFileBase都接收不到.....后来搜索了下才知道mvc自带的Ajax ...
- 过滤器中获取form表单或url请求数据
var httpFormData = filterContext.HttpContext.Request.Form; var logContent = string.Empty; //获取url的 l ...
- 《基于MVC的JavaScript Web富应用开发》学习笔记
第1章 MVC和类 1. 什么是MVC? MVC是一种设计模式, 它将应用划分为3个部分: 数据(模型, Model), 展现层(视图, View) 和用户交互层(控制器, Controller). ...
- 01_python_初始python
一.初始python python是一门解释型语言,弱类型语言 / python解释器最为常用的是cpython(官方) 弱类型语言: a = 1 a = 'alex' #说明变量a既可以是整 ...
- Smart/400开发上手4: 调试Cobol代码 (DEBUG with QBATCH)
Step1:Compile Cobol source CB TIM07 using *SRCDBG option例如:CB MEMBER(TIM07) OPTION(*SRCDBG) WORKU(TI ...
- Liferay开发实战(2):Service Builder生成持久化层,及开发服务层
本文Liferay适用版本:v6.2.ce-ga6版 Liferay的插件体系是:模型-视图-控制器的portlet MVC框架.MVC是一个伟大的用于Web应用程序的设计模式,在实际应用中还应处理持 ...
- TakePhoto实现拍照得到图片和从相册得到图片
在学郭霖大神的第一行代码的时候,学到利用相机拍照和从本地相册取照片的那一小节的时候,代码写出来但是出了很多问题,APP老是崩溃,一番百度最终还是没有找到解决办法 无奈只能用别人现成的轮子了,然后就发现 ...
- How to resize or create a thumbnail image from file stream on UWP
最近在搞Ocr相关的windows universal app, 用到了一些图像处理相关的知识. 涉及到了BitmapDecoder/BitmapEncoder/IRandomAccessStream ...