sql: TRIGGER

--Common Table Expressions(CTE)
WITH HighSample (SampleId,SampleTitle,SampleContent) AS
(
    SELECT SampleId,SampleTitle,SampleContent From CompetitionSample
    WHERE SampleId>1
)
SELECT * From HighSample

--游标
DECLARE curSample INSENSITIVE CURSOR
FOR SELECT TOP 10 SampleId,SampleTitle,SampleContent FROM CompetitionSample
DECLARE @SampleId int
DECLARE @SampleTitle nvarchar(540)
Declare @SampleContent nvarchar(1000)
Open curSample
FETCH NEXT FROM curSample INTO @SampleId,@SampleTitle,@SampleContent
WHILE @@Fetch_Status=0
  BEGIN
    PRINT @SampleTitle+‘,‘+@SampleContent
    Fetch NEXT FROM curSample Into @SampleId,@SampleTitle,@SampleContent
  END
CLOSE curSample
DEALLOCATE curSample

--N到M条记录(要有主索引ID)
--Select Top M-N * From 表 Where ID in (Select Top M ID From 表) Order by ID   Desc
select * from ShoppingCouponsList
Select Top (15-10) * From ShoppingCouponsList Where ShoppingCouponsID in (Select Top 15 ShoppingCouponsID From ShoppingCouponsList) Order by ShoppingCouponsID   Desc


--触发器(添加,修改)
--text,ntext 无法创建约束,可以用此方法
drop TRIGGER Sample_insert

CREATE TRIGGER DuSample_insert
ON CompetitionSample
FOR INSERT,UPDATE AS --常见的触发器有三种:分别应用于Insert , Update , Delete 事件
declare @s nvarchar(500)
set @s=‘err,geovindu‘
IF EXISTS (SELECT * FROM  CompetitionSample  where charindex(‘</title>‘,SampleContent)=1)
begin
ROLLBACK TRANSACTION
RAISERROR(‘有非法字符‘,16,1,@s)
end


--修改不了
update CompetitionSample set SampleContent=‘</title>‘ where SampleId=‘1‘
--添加不了
insert into CompetitionSample(SampleTitle,SampleBrandId,SamplePlatformId,SampleSmallPic,SampleContent,SampleAuthor,SampleIs)
 values(‘d2‘,1,1,‘gif‘,‘</title>2‘,‘g2‘,0)


select * from CompetitionSample where charindex(‘</title>‘,SampleContent)=1

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。