A self-written trigger

Source: Internet
Author: User


Alter trigger [inserttopic] -- check whether the gold coin is sufficient before posting. If it is sufficient, post and reward points.
On [DBO]. [bbstopic]
Instead of insert
As
Begin
Declare @ topictype nvarchar (50)
, @ Integralsum int
, @ Goldsum numeric (18, 2)
, @ Code nvarchar (50)
, @ Title nvarchar (max)
, @ Tcontent nvarchar (max)
, @ Uid int
, @ Ucode nvarchar (50)
, @ CCode nvarchar (50)
, @ Replycount int
, @ Clickcount int
, @ Tstate int
, @ TTime datetime
, @ Isrecommand bit
, @ Isessential bit
, @ Customsort int
, @ Belongto int
, @ Ttcode nvarchar (50)
, @ Jiericode nvarchar (50)
, @ Xuanshangjinbi int
, @ Zhidingtime datetime
, @ Ispingbi bit
, @ Edittime datetime
, @ Inserttopicidentity int
, @ Insertgoldidentity int
, @ Insertintegralidentity int;
Select @ topictype = ttcode from inserted
Select @ uid = uid from inserted
Select @ integralsum = sum (bbsintegral) from bbsintegral where [email protected] -- total user points
Select @ goldsum = sum (bbsgold) from bbsgold where [email protected] -- total user gold coins
Select @ code = [Code]
, @ Title = [title]
, @ Tcontent = [tcontent]
, @ Uid = [uid]
, @ Ucode = [ucode]
, @ CCode = [cCode]
, @ Replycount = [replycount]
, @ Clickcount = [clickcount]
, @ Tstate = [tstate]
, @ TTime = [tTime]
, @ Isrecommand = [isrecommand]
, @ Isessential = [isessential]
, @ Customsort = [customsort]
, @ Belongto = [belongto]
, @ Ttcode = [ttcode]
, @ Jiericode = [jiericode]
, @ Xuanshangjinbi = [xuanshangjinbi]
, @ Zhidingtime = [zhidingtime]
, @ Ispingbi = [ispingbi]
, @ Edittime = [edittime]
From inserted
If (@ topictype = 'ordinary title') -- publish a topic post and make a payment-8 gold coins + 12 points
Begin

If (@ goldsum> = 8) -- if the gold coin is 8 enough, you can post it.
Begin
Insert into bbstopic values (@ code
, @ Title
, @ Tcontent
, @ Uid
, @ Ucode
, @ CCode
, @ Replycount
, @ Clickcount
, @ Tstate
, @ TTime
, @ Isrecommand
, @ Isessential
, @ Customsort
, @ Belongto
, @ Ttcode
, @ Jiericode
, @ Xuanshangjinbi
, @ Zhidingtime
, @ Ispingbi
, @ Edittime) Select @ [email protected] @ identity

Insert into [wts_community]. [DBO]. [bbsgold]
([Uid]
, [Bbsgold]
, [Datetime]
, [Itemtype])
Values
(@ Uid
,-8
, Getdate ()
, 'Post daily ') Select @ [email protected] @ identity
Insert into [wts_community]. [DBO]. [bbsintegral]
([Uid]
, [Bbsintegral]
, [Datetime]
, [Itemtype])
Values
(@ Uid
, 12
, Getdate ()
, 'Post daily ') Select @ [email protected] @ identity

If (@ inserttopicidentity! = 0 and @ insertgoldidentity! = 0 and @ insertgoldidentity! = 0)
Begin
Print 'you have successfully published a regular post, rewarded with 12 points, and deducted 8 gold coins'
End
Else
Begin
Print 'failed to publish common posts! '
End
End
Else
Begin

Print 'Insufficient coins'
-- Raiserror ('Insufficient coins ', 16, 1 );
-- Rollback Tran;

end
else if (@ topictype = 'topic title ') -- publish a regular post with a payment-10 gold coins + 10 Points
begin
If (@ goldsum> = 10) -- if the gold coin is sufficient for 10, then you can post
begin
insert into bbstopic values (@ Code
, @ title
, @ tcontent
, @ uid
, @ ucode
, @ cCode
, @ replycount
, @ clickcount
, @ tstate
, @ tTime
, @ isrecommand
, @ isessential
, @ customsort
, @ belongto
, @ ttcode
, @ jiericode
, @ xuanshangjinbi
, @ zhidingtime
, @ ispingbi
, @ edittime) Select @ [email protected] @ identity

insert into [wts_community]. [DBO]. [bbsgold]
([uid]
, [bbsgold]
, [datetime]
, [itemtype])
values
(@ uid
,-10
, getdate ()
, 'Post every day ') select @ [email protected] @ identity
insert into [wts_community]. [DBO]. [bbsintegral]
([uid]
, [bbsintegral]
, [datetime]
, [itemtype])
values
(@ uid
, 10
, getdate ()
, 'Post every day') Select @ [email protected] @ ident Ity
If (@ inserttopicidentity! = 0 and @ insertgoldidentity! = 0 and @ insertintegralidentity! = 0)
begin
Print 'your topic post has been published successfully! Reward 10 points, deduct 10 gold coins '
end
else
begin
Print' your topic post failed! '
end
else
begin

Print 'Insufficient coins '
-- raiserror ('Insufficient coins', 16, 1);
-- rollback Tran;
end
else if (@ topictype = 'Post question ') -- publish a question post: payment-12 gold coins + 12 points
begin
If (@ goldsum> = 12) -- if the gold coin is 12 enough, then you can post
begin
insert into bbstopic values (@ Code
, @ title
, @ tcontent
, @ uid
, @ ucode
, @ cCode
, @ replycount
, @ clickcount
, @ tstate
, @ tTime
, @ isrecommand
, @ isessential
, @ customsort
, @ belongto
, @ ttcode
, @ jiericode
, @ xuanshangjinbi
, @ zhidingtime
, @ ispingbi
, @ edittime) Select @ [email protected] @ identity

Insert into [wts_community]. [DBO]. [bbsgold]
([Uid]
, [Bbsgold]
, [Datetime]
, [Itemtype])
Values
(@ Uid
,-12
, Getdate ()
, 'Post daily ') Select @ [email protected] @ identity
Insert into [wts_community]. [DBO]. [bbsintegral]
([Uid]
, [Bbsintegral]
, [Datetime]
, [Itemtype])
Values
(@ Uid
, 12
, Getdate ()
, 'Post daily ') Select @ [email protected] @ identity
If (@ inserttopicidentity! = 0 and @ insertgoldidentity! = 0 and @ insertintegralidentity! = 0)
Begin
Print 'your post has been published! Reward 12 points, deduct 12 gold coins! '
End
Else
Begin
Print 'the post you posted failed! '
End
End
Else
Begin

Print 'Insufficient coins'
-- Raiserror ('Insufficient coins ', 16, 1 );
-- Rollback Tran; The transaction ends in the trigger. The batch processing has been aborted.
End
End


End

-- Test

Insert into [wts_community]. [DBO]. [bbstopic]
([Code]
, [Title]
, [Tcontent]
, [Uid]
, [Ucode]
, [CCode]
, [Replycount]
, [Clickcount]
, [Tstate]
, [TTime]
, [Isrecommand]
, [Isessential]
, [Customsort]
, [Belongto]
, [Ttcode]
, [Jiericode]
, [Xuanshangjinbi]
, [Zhidingtime]
, [Ispingbi]
, [Edittime])
Values
('Chunadao'
, 'Test'
, 'Test'
, 125
,''
, 'Chunadao'
, 0
, 0
, 0
, '2017/9 00:00:00'
, 0
, 0
, 0
, 0
, 'Question requests'
,''
, 10
, '2017/9 00:00:00'
, 0
, '2017/9 00:00:00 ')

 

A self-written trigger

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.