触发器实现记录操作表的日志
这个功能我是琢磨了好久,本来我的sqlserver方面的知识深入了解的就不多,关键的难点是用变量代替字段名,然后获取字段在表中存的值,再赋给另外一个变量,我之所以这么做,因为后面好几处要用到这个字段的名称,我才用变量代替,便于修改,可就是 实现 花费
这个功能我是琢磨了好久,本来我的sqlserver方面的知识深入了解的就不多,关键的难点是用变量代替字段名,然后获取字段在表中存的值,再赋给另外一个变量,我之所以这么做,因为后面好几处要用到这个字段的名称,我才用变量代替,便于修改,可就是实现花费了很长时间,网上这么方面的资料又少,可终究还是找到了解决方案,希望大家以后遇到同样的问题不至于头大,把具体的实现分享给大家
CREATE trigger [dbo].[trg_new_course]
on [dbo].[course]
for insert,delete,update
as
begin
declare @tabname varchar(50),
@pkname varchar(20),
@pkvalue varchar(20),
@opttype int,
@optip varchar(20),
@optsql varchar(200),
@xmlstr nvarchar(500);
declare @optinfo nvarchar(500), @id_i int, @id_d int;
declare @min_id int, --最小的字段号
@total int, --记录总数
@row_count int, --循环变量
@temp_name varchar(100), --临时字段名
@temp_pre_name varchar(100), --带字段类型前缀的变量
@temp_type varchar(100), --临时字段类型
@temp_value varchar(100), --临时字段值
@xmlnode_value varchar(100), --xml的节点值
@sql_name varchar(100), --sql操作相关的字段
@sql_value varchar(100), --sql操作相关的字段值
@sql nvarchar(200), --存储动态
sql @pk_pre_name varchar(20) --带类型前缀的关键字段名
set @sql_name = '';
set @sql_value = '';
set @row_count = 1;
set @pkname = 'id'; --关键字名称
set @tabname = 'course'; --操作的表名
set @optinfo = '';
select @id_i=id from inserted; select @id_d=id from deleted;
select @temp_type = data_type from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tabname and column_name = @pkname;
if (@temp_type = 'int')
begin
set @pk_pre_name = 'i' + @pkname
end
else if(@temp_type = 'float')
begin
set @pk_pre_name = 'f' + @pkname
end
else if(@temp_type = 'decimal')
begin
set @pk_pre_name = 'd' + @pkname
end
else if(@temp_type = 'datetime')
begin
set @pk_pre_name = 'da' + @pkname
end
else
begin
set @pk_pre_name = 'c' + @pkname
end
if @id_i is null and @id_d is not null --删除操作
begin
set @pkvalue = @id_d;
set @opttype = 1;
--若变量的类型不是字符串型
set @pkvalue = convert(varchar(200),@pkvalue);
--生成执行删除操作的sql语句
set @optsql = 'delete from ' + @tabname + ' where ' + @pkname + '=' + @pkvalue;
--生成删除操作字段信息的xml表示
set @optinfo = @optinfo + '';
set @optinfo = @optinfo + @pkvalue;
set @optinfo = @optinfo + '' + @pkname +'>';
end
else
begin
set @pkvalue = @id_i;
select * into temps from inserted;--这句必须写动态sql中时找不到inerted这个逻辑表的
select @min_id = max(ordinal_position) from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tabname;
select @total = count(1) from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tabname;
while(@row_count
begin
select @temp_name = column_name,@temp_type = data_type from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tabname and ordinal_position = @min_id;
if(@temp_type = 'int')
begin
declare @temp_in int;
SET @sql = 'select @temp_in = ' + @temp_name + ' from temps;';
EXEC SP_EXECUTESQL @Sql, N'@temp_in int OUTPUT', @temp_in OUTPUT;
set @xmlnode_value = convert(varchar(100),@temp_in);
set @temp_value = @xmlnode_value;
set @temp_pre_name = 'i' + @temp_name;
end
else if(@temp_type = 'float')
begin
declare @temp_inf float;
SET @sql = 'select @temp_inf = ' + @temp_name + ' from temps;';
EXEC SP_EXECUTESQL @Sql, N'@temp_inf float OUTPUT', @temp_inf OUTPUT;
set @xmlnode_value = convert(varchar(100),@temp_inf);
set @temp_value = @xmlnode_value;
set @temp_pre_name = 'f' + @temp_name;
end
else if(@temp_type = 'decimal')
begin
declare @temp_ind float;
SET @sql = 'select @temp_ind = ' + @temp_name + ' from temps;';
EXEC SP_EXECUTESQL @Sql, N'@temp_ind decimal(18,0) OUTPUT', @temp_ind OUTPUT;
set @xmlnode_value = convert(varchar(100),@temp_ind);
set @temp_value = @xmlnode_value;
set @temp_pre_name = 'd' + @temp_name;
end
else
begin
declare @temp_inc varchar(200);
SET @sql = 'select @temp_inc = ' + @temp_name + ' from temps;';
EXEC SP_EXECUTESQL @Sql, N'@temp_inc varchar(200) OUTPUT', @temp_inc OUTPUT;
set @xmlnode_value = convert(varchar(100),@temp_inc);
set @temp_value = '''' + @xmlnode_value + '''';
set @temp_pre_name = 'c' + @temp_name;
end
--生成插入/修改操作相关数据信息的xml表示
set @optinfo = @optinfo + '';
set @optinfo = @optinfo + @xmlnode_value;
set @optinfo = @optinfo + '' + @temp_pre_name + '>';
if @id_i is not null and @id_d is null -- 插入操作
begin
--生成插入操作执行的sql语句
if(@temp_name @pkname)
begin
set @sql_name = @sql_name + ',' + @temp_name;
set @sql_value = @sql_value + ',' + @temp_value;
end
end
else if @id_i is not null and @id_d is not null --更新操作
begin
--生成修改操作执行的sql语句
if(@temp_name @pkname)
begin
set @sql_name = @sql_name + ',' + @temp_name + '=' + @temp_value;
end
end
select @min_id = ordinal_position from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'course' and ordinal_position
set @row_count = @row_count + 1;
end
if @id_i is not null and @id_d is null -- 插入操作
begin
--生成执行插入操作的sql语句
set @opttype = 0;
set @optsql = 'insert into ' + @tabname + '(' + substring(@sql_name,2,len(@sql_name)) + ')' + ' values(' + substring(@sql_value,2,len (@sql_value)) +')';
end
else if @id_i is not null and @id_d is not null --更新操作
begin
--生成执行修改操作的sql语句
set @opttype = 3;
set @optsql = 'update ' + @tabname + ' set ' + substring(@sql_name,2,len(@sql_name)) + ' where ' + @pkname + '=' + @pkvalue;
end
drop table temps;
end
set @xmlstr = '
set @xmlstr = @xmlstr + '
set @xmlstr = @xmlstr + '
set @xmlstr = @xmlstr + '
set @xmlstr = @xmlstr + ''; set @xmlstr = @xmlstr + '
set @xmlstr = @xmlstr + @optinfo;
set @xmlstr = @xmlstr + '';
set @xmlstr = @xmlstr + '';
select @optip=client_net_address from sys.dm_exec_connections where Session_id=@@spid;
if(@pkvalue is null)
begin
set @pkvalue = -1;
end
insert into optlog values(@tabname,@pkname,@pkvalue,@opttype,@optip,getdate(),@optsql,@xmlstr);
print '操作执行成功';
end
红色标注的部分我认识是实现的难点,就是用到了sqlserver的系统存储过程sp_executesql,具体的用法网上有的可以查下,这只是我的一家之言,或许大家还有很好的实现,欢迎大家提意见啊!我知道这里很多大牛,可能我的想法会比较拙劣,但希望能够帮助到大家,只此记录我的技术成长历程。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Both vivox100s and x100 mobile phones are representative models in vivo's mobile phone product line. They respectively represent vivo's high-end technology level in different time periods. Therefore, the two mobile phones have certain differences in design, performance and functions. This article will conduct a detailed comparison between these two mobile phones in terms of performance comparison and function analysis to help consumers better choose the mobile phone that suits them. First, let’s look at the performance comparison between vivox100s and x100. vivox100s is equipped with the latest

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

With the rapid development of the Internet, the concept of self-media has become deeply rooted in people's hearts. So, what exactly is self-media? What are its main features and functions? Next, we will explore these issues one by one. 1. What exactly is self-media? We-media, as the name suggests, means you are the media. It refers to an information carrier through which individuals or teams can independently create, edit, publish and disseminate content through the Internet platform. Different from traditional media, such as newspapers, television, radio, etc., self-media is more interactive and personalized, allowing everyone to become a producer and disseminator of information. 2. What are the main features and functions of self-media? 1. Low threshold: The rise of self-media has lowered the threshold for entering the media industry. Cumbersome equipment and professional teams are no longer needed.

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

As Xiaohongshu becomes popular among young people, more and more people are beginning to use this platform to share various aspects of their experiences and life insights. How to effectively manage multiple Xiaohongshu accounts has become a key issue. In this article, we will discuss some of the features of Xiaohongshu account management software and explore how to better manage your Xiaohongshu account. As social media grows, many people find themselves needing to manage multiple social accounts. This is also a challenge for Xiaohongshu users. Some Xiaohongshu account management software can help users manage multiple accounts more easily, including automatic content publishing, scheduled publishing, data analysis and other functions. Through these tools, users can manage their accounts more efficiently and increase their account exposure and attention. In addition, Xiaohongshu account management software has

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP is a server-side scripting language widely used in web development. Its main function is to generate dynamic web content. When combined with HTML, it can create rich and colorful web pages. PHP is powerful. It can perform various database operations, file operations, form processing and other tasks, providing powerful interactivity and functionality for websites. In the following articles, we will further explore the role and functions of PHP, with detailed code examples. First, let’s take a look at a common use of PHP: dynamic web page generation: P
