Home Database Mysql Tutorial sql server触发器实现插入时操作另一张表

sql server触发器实现插入时操作另一张表

Jun 07, 2016 pm 02:50 PM
server sql accomplish operate trigger

以前都是也得mysql,现在写sqlserver的触发器,感觉改动还是蛮大的 1.定义变量 #在mysql中变量直接这么定义就可以了SET @VALUE = 111;#在sql server中declare @count int;#并赋值set @count =0;#如果是查询,必须这么些select @count = count(*) from WQ_MNI

以前都是也得mysql,现在写sqlserver的触发器,感觉改动还是蛮大的

1.定义变量

#在mysql中变量直接这么定义就可以了
SET @VALUE = "111";

#在sql server中
declare @count int;
#并赋值
set @count =0;
#如果是查询,必须这么些
select @count = count(*) from WQ_MNINF_D_REAL where STCD=@stcd;
Copy after login

2.判断

在mysql中,if判断的格式
if 条件 then 语句 end if;

而在sql server中,if判断的格式

if(条件) begin 语句 end;


例子

#mysql
IF @VALUE4=1 THEN
   INSERT INTO t_sca_history_data (METER_CODE,PARAM_CODE,DATA_VALUE,V_VALUE,DATE_TIME) VALUES
                        (NEW.METER_CODE,NEW.PARAM_CODE,NEW.DATA_VALUE,NEW.V_VALUE,NEW.DATE_TIME);
END IF;

sql server
if(@count=0)
begin
   insert into WQ_MNINF_D_REAL select STCD,TYPE,UPPERVALUE,LOWERVALUE,TM,NT,@smid,@stnm,@prjcd,@pipcd from inserted;
end 
Copy after login

3.触发器的new

在mysql中,用new.NAME 可以得到触发器触发插入的值,而sql server不是这样的,sql server是把处罚的数据放在一个临时表中,所以它的操作是这样的

#inserted代表插入数据的那张临时表,同时还有deleted 这张用作删除数据的临时表
select STCD from inserted
#若只是把插入的数据插入另一张表,语句如下
insert into WQ_WWFINF_D_REAL select PRJCD,TM,INFL,SWWL,CWWL,OTPS,OTF,QOEC,PSPPS,NT FROM inserted;
#如果还有加点别的数据,可以这么做
insert into WQ_MNINF_D_REAL select STCD,TYPE,UPPERVALUE,LOWERVALUE,TM,NT,@smid,@stnm,@prjcd,@pipcd from inserted;
Copy after login

4.我做的触发器的代码,改触发器的作用是把原始数据表的数据插入到实时数据表中,如果实时表没有该数据,就插入,如果有,就删除再插入

BEGIN
  declare @stcd varchar(30);
  declare @count int;
  declare @smid int;
  declare @stnm varchar(30);
  declare @prjcd varchar(30);
  declare @pipcd varchar(30);
  select @stcd = STCD from inserted;
  select @count = count(*) from WQ_MNINF_D_REAL where STCD=@stcd;
  select @smid = SMID from WQ_WQSINF_B where STCD=@stcd;
  select @stnm = STNM from WQ_WQSINF_B where STCD=@stcd;
  select @prjcd = PRJCD from WQ_WQSINF_B where STCD=@stcd;
  select @pipcd = PIPCD from WQ_WQSINF_B where STCD=@stcd;
  if(@count=0)
    begin
      insert into WQ_MNINF_D_REAL select STCD,TYPE,UPPERVALUE,LOWERVALUE,TM,NT,@smid,@stnm,@prjcd,@pipcd from inserted;
    end 
  else
    begin
      delete WQ_MNINF_D_REAL where STCD=@stcd;
      insert into WQ_MNINF_D_REAL select STCD,TYPE,UPPERVALUE,LOWERVALUE,TM,NT,@smid,@stnm,@prjcd,@pipcd from inserted;
    end
END
Copy after login

这篇博文给我作用挺大的,一并贴出

http://blog.csdn.net/chenbin520/article/details/6026686

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

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

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

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

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

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.

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

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.

Analysis of the impact of MySQL connection number on database performance Analysis of the impact of MySQL connection number on database performance Mar 16, 2024 am 10:09 AM

Analysis of the Impact of MySQL Connection Number on Database Performance With the continuous development of Internet applications, databases have become an important data storage and management tool to support application systems. In the database system, the number of connections is an important concept, which is directly related to the performance and stability of the database system. This article will start from the perspective of MySQL database, explore the impact of the number of connections on database performance, and analyze it through specific code examples. 1. What is the number of connections? The number of connections refers to the number of client connections supported by the database system at the same time. It can also be managed

See all articles