Home Database Mysql Tutorial SQL SERVER触发器游标小记

SQL SERVER触发器游标小记

Jun 07, 2016 pm 05:38 PM
server cursor trigger

SQL SERVER触发器游标小记 今天接到个需求用触发器来实现通过条件对其他表的更新。好久没摸SQL SERVER,电脑里也没SQL SERVER安装包,同事遂发来个安装包,一看吓一跳,3.6G!!!!经过漫长等待后,开始作业。需求如下 1、 当a字段更新为2或者3,并且b字段更

SQL SERVER触发器游标小记

     今天接到个需求用触发器来实现通过条件对其他表的更新。好久没摸SQL SERVER,电脑里也没SQL SERVER安装包,同事遂发来个安装包,一看吓一跳,3.6G!!!!经过漫长等待后,开始作业。需求如下

    1、  当a字段更新为2或者3,并且b字段更新为y的时候在新表Exchange插入该id、Q

    2、  当a字段更新为3,且b字段更新为n的时候,在新表插入该表的id,a

代码如下

create trigger updateExange on [dbo].[EXAM_MASTER] after update as if(exists(select inserted.result_status,inserted.consultation_status from inserted where (inserted.result_status='2' or inserted.result_status='3') and consultation_status='y')) begin declare id_cursor1 cursor for select inserted.exam_id from inserted open id_cursor1 declare @exam_id int --@exam_id要与游标中的字段名相同 fetch next from id_cursor1 into @exam_id while @@FETCH_STATUS=0 begin insert into [dbo].[Exchange] (id,examid,mark) values(NEWID(),@exam_id,'Q') fetch next from id_cursor1 into @exam_id end close id_cursor1 deallocate id_cursor1 end if (exists(select inserted.result_status,inserted.consultation_status from inserted where inserted.result_status='3' and consultation_status='n')) begin declare id_cursor2 cursor for select inserted.exam_id from inserted open id_cursor2 fetch next from id_cursor2 into @exam_id while @@FETCH_STATUS=0 begin insert into [dbo].[Exchange] (id,examid,mark) values(NEWID(),@exam_id,'A') fetch next from id_cursor2 into @exam_id end close id_cursor2 deallocate id_cursor2 end

 虽然不是最佳办法,,但也算完成了任务。

posted on

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to hide text until clicked in Powerpoint How to hide text until clicked in Powerpoint Apr 14, 2023 pm 04:40 PM

How to hide text before any click in PowerPoint If you want text to appear when you click anywhere on a PowerPoint slide, setting it up is quick and easy. To hide text before clicking any button in PowerPoint: Open your PowerPoint document and click the Insert menu. Click on New Slide. Choose Blank or one of the other presets. Still in the Insert menu, click Text Box. Drag a text box onto the slide. Click the text box and enter your

How to install, uninstall, and reset Windows server backup How to install, uninstall, and reset Windows server backup Mar 06, 2024 am 10:37 AM

WindowsServerBackup is a function that comes with the WindowsServer operating system, designed to help users protect important data and system configurations, and provide complete backup and recovery solutions for small, medium and enterprise-level enterprises. Only users running Server2022 and higher can use this feature. In this article, we will explain how to install, uninstall or reset WindowsServerBackup. How to Reset Windows Server Backup If you are experiencing problems with your server backup, the backup is taking too long, or you are unable to access stored files, then you may consider resetting your Windows Server backup settings. To reset Windows

Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Windows Server 2025 preview version welcomes update, Microsoft improves Insiders testing experience Feb 19, 2024 pm 02:36 PM

On the occasion of releasing the build 26040 version of Windows Server, Microsoft announced the official name of the product: Windows Server 2025. Also launched is the Windows11WindowsInsiderCanaryChannel version build26040. Some friends may still remember that many years ago someone successfully converted Windows NT from workstation mode to server mode, showing the commonalities between various versions of Microsoft operating systems. Although there are clear differences between Microsoft's current version of the server operating system and Windows 11, those who pay attention to the details may be curious: why Windows Server updated the brand,

Recommend the best Windows 11 mouse cursor solution Recommend the best Windows 11 mouse cursor solution Apr 23, 2023 am 09:52 AM

Windows 11 has a seemingly unlimited amount of customization options, from default settings to every third-party app on the Internet. There are even apps that can change the appearance of your mouse cursor. Modifying the cursor is a great way to give your computer a unique look. You don't have to stick the same boring black and white pointer on every computer. But even so, you don't have to download software to change the look of your cursor. How to change the appearance of the cursor? Windows 11 offers a small amount of customization for the cursor. You can change the cursor by going into Control Panel and selecting Mouse Options there. A new window called "Mouse Properties" will appear. In the mouse properties you can change the color scheme, size and design. Your computer will naturally

How to add trigger in oracle How to add trigger in oracle Dec 12, 2023 am 10:17 AM

In Oracle database, you can use the CREATE TRIGGER statement to add triggers. A trigger is a database object that can define one or more events on a database table and automatically perform corresponding actions when the event occurs.

How to use MySQL triggers to implement automatic archiving of data How to use MySQL triggers to implement automatic archiving of data Aug 02, 2023 am 10:37 AM

How to use MySQL triggers to implement automatic archiving of data Introduction: In the field of modern data management, automatic archiving and cleaning of data is an important and common requirement. As the amount of data increases, retaining complete historical data will occupy excessive storage resources and reduce query performance. MySQL triggers provide an effective way to achieve this requirement. This article will introduce how to use MySQL triggers to achieve automatic archiving of data. 1. What is a MySQL trigger? A MySQL trigger is a special kind of memory.

What level is mysql trigger? What level is mysql trigger? Mar 30, 2023 pm 08:05 PM

MySQL triggers are row-level. According to SQL standards, triggers can be divided into two types: 1. Row-level triggers, which will be activated once for each row of data modified. If a statement inserts 100 rows of data, the trigger will be called 100 times; 2. Statement-level triggers The trigger is activated once for each statement. A statement that inserts 100 rows of data will only call the trigger once. MySQL only supports row-level triggers, not prepared statement-level triggers.

How to write custom stored procedures, triggers and functions in MySQL using C# How to write custom stored procedures, triggers and functions in MySQL using C# Sep 20, 2023 pm 12:04 PM

How to write custom stored procedures, triggers and functions in MySQL using C# MySQL is a widely used open source relational database management system, and C# is a powerful programming language for development tasks that require interaction with the database. Say, MySQL and C# are good choices. In MySQL, we can use C# to write custom stored procedures, triggers and functions to achieve more flexible and powerful database operations. This article will guide you in using C# to write and execute

See all articles