SQL Server Functions and triggers

Source: Internet
Author: User

Use of triggers in SQL Server

-- Check whether the specified expert record exists from the temporary table
Create Function f_expert_exist_in_temp (
@ Expertid numeric (9 ),
@ Sqltype numeric (9 ),
@ Trs_flag numeric (9)

)
Returns int
As
Begin
Declare @ RET int
Select @ ret = 0
Select @ ret = count (*) from [86_xiehe_time $ _ temp]
Where expertid = @ expertid and SQL _type = @ sqltype and trs_flag = @ trs_flag
Return @ RET
End

 

-- work experience trigger (only experts have work experience)
-- add
Create trigger [workhistory_ I _trig] on [xwcmworkhistory] For insert as
begin
-- determine whether a temporary table already exists.
declare @ nisexist int
select @ nisexist = DBO. f_expert_exist_in_temp (objid, 1, 0) from inserted
If (@ nisexist> 0)
return
-- add the statement to the temporary table if it does not exist
insert into [86_xiehe_time $ _ temp] (expertid, SQL _type, trs_flag)
select objid, 1, 0 from inserted
end

go
-- modify
Create trigger [workhistory_u_trig] on [xwcmworkhistory] for update as
begin
-- determine operation table records
declare @ nisexist int
select @ nisexist = DBO. f_expert_exist_in_temp (objid, 3, 0) from deleted
If (@ nisexist = 0)
begin
-- add to the temporary table if it does not exist
insert into [86_xiehe_time $ _ temp] (expertid, SQL _type, trs_flag)
select objid, 3, 0 from deleted
end

-- Determine whether to add data to the updated table
Select @ nisexist = DBO. f_expert_exist_in_temp (objid, 1, 0) from inserted
If (@ nisexist = 0)
Begin
Insert into [86_xiehe_time $ _ temp] (expertid, SQL _type, trs_flag)
Select objid, 1, 0 from inserted
End
End

Go
 
-- Delete
Create trigger [workhistory_d_trig] on [xwcmworkhistory] For Delete
Begin
-- Determine whether the temporary table already exists.
Declare @ nisexist int
Select @ nisexist = DBO. f_expert_exist_in_temp (objid, 3, 0) from deleted
If (@ nisexist> 0)
Return

-- If it does not exist, add it to the temporary table.
Insert into [86_xiehe_time $ _ temp] (expertid, SQL _type, trs_flag)
Select objid, 3, 0 from deleted
End

Go

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.