日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

存储过程,触发器,分页练习

發(fā)布時間:2024/4/11 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 存储过程,触发器,分页练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

-------------------------------------游標和循環(huán)--------------------------------

DECLARE CATEGORY_CUR SCROLL CURSOR For ?SELECT*FROM dbo.Categories ? FOR UPDATE OF CategoryName ?OPEN CATEGORY_CUR ?GO

?UPDATE Categories ?SET CategoryName='家電' ?where CURRENT OF CATEGORY_CUR ? ? ?SELECT*FROM Categories ? DECLARE @VAR INT SET @VAR=0 WHILE @VAR<100000 BEGIN INSERT INTO dbo.Categories VALUES('測試數(shù)據(jù)') SET @VAR=@VAR+1 END ? ? ? ?SELECT Count(*)FROM dbo.Products ? ?delete from dbo.Categories ? exec sp_helpdb

------------------------------------存儲過程以及分頁----------------------------

create procedure prSelctCategories AS BEGIN SELECT*FROM Categories END

EXECUTE prSelctCategories

Create Procedure prSelct @id int as select*from Categories where CategoryID=@id

execute prSelct 2763993

create procedure pr @id int, @name varchar(20) as Select*from Categories where CategoryID=@id and CategoryName=@name

execute pr 2763993,'測試數(shù)據(jù)'

GO

create procedure rp @id int, @content varchar(20) output as select @content= CategoryName from Categories where CategoryID=@id

GO

DECLARE @content varchar(20) execute rp 2763993,@content output PRINT @content

DECLARE @PRODEUCTNAME VARCHAR(20),@CATEGORYID INT,@PRUDUCTNUM INT,@VAR INT SET @VAR=0 SET @CATEGORYID=2763985 WHILE @VAR<10000 BEGIN INSERT INTO Products VALUES('洗衣粉',@CATEGORYID,@VAR,DEFAULT,DEFAULT) SET @VAR=@VAR+1 END

select*from Products delete from Products GO

create procedure sp_proPage @pageindex int=1, @pagesize? int=10 as select top(@pagesize)*from products where productID not in( select top ((@pageindex-1)*@pagesize) productID from Products order by ProductID)

GO execute sp_proPage 3,5000

Go

Create procedure proc_Page @pageIndex int,???--當前頁 @pageSize int,???--每頁多少條數(shù)據(jù) @pageCount int output?--總共多少頁 as declare @firstIndex int?--firstIndex開始 declare @lastIndex?int?--lastIndex結(jié)束 declare @total int set @firstIndex=(@pageIndex-1)*@pageSize+1 set @lastIndex=@pageSize*@pageIndex select @total=COUNT(*)from Products if(@total%@pageSize<>0) ?begin set @pageCount=@total/@pageSize+1 ?end else ?begin ?set @pageCount=@total/@pageSize ?end --先對主鍵進行排序,然后用ROW_NUMBER進行編號? select*from (select ROW_NUMBER()over(order by ProductID)as PID,* from? Products)as PTB where PID between @firstIndex and @lastIndex

declare @pageCount int execute proc_Page 1,10,@pageCount output print @pageCount

--------------------------------------事務的使用--------------------------------------

create table Bank ( ?? ID INT NOT NULL PRIMARY KEY, ?? MONERYS MONEY ) ?INSERT INTO Bank VALUES(1,'2000') ?INSERT INTO Bank VALUES(2,'5000') ?GO ?CREATE PROCEDURE PROC_MON ?@TOID int, ?@FROMID INT, ?@MONEYS MONEY ?AS SET XACT_ABORT on BEGIN TRAN ?DECLARE @Counterror int=0 UPDATE Bank SET MONERYS=MONERYS-@MONEYS WHERE ID=@FROMID --轉(zhuǎn)賬賬戶減去金額 ?set @Counterror=@Counterror+@@ERROR ?UPDATE Bank SET MONERYS=MONERYS+@MONEYS WHERE ID=@TOID?? --轉(zhuǎn)賬賬戶加上金額 ?set @Counterror=@Counterror+@@ERROR IF? @Counterror<>0? --判斷是否出現(xiàn)錯誤 ?begin rollback tran? --如果有錯開始回滾 ?RETURN 0 ?END ?else?? ????--如果沒有錯誤 ?begin commit tran --執(zhí)行事務 ?RETURN 1 END GO ?execute PROC_MON 1,2,2000 ?select*from bank ?

?-----------------------------------------------------觸發(fā)器操作-------------------------------------------- ----

DML觸發(fā)器:DML事件即指在表或視圖中修改數(shù)據(jù)的insert、update、delete語句。 ----DDL觸發(fā)器:?DDL事件即指在表或索引中的create、alter、drop語句也。 GO create trigger BankUpdateTrigger on Bank for update as print'the table was updated!'

Update Bank set MONERYS=5 where ID=1

select*from Bank

execute sp_helptext BankUpdateTrigger???? --查看觸發(fā)器內(nèi)容 select * from sysobjects where xtype='TR' --產(chǎn)看數(shù)據(jù)庫多少觸發(fā)器 disable trigger BankUpdateTrigger on Bank? --禁用觸發(fā)器 enable? trigger BankUpdateTrigger on Bank?--開啟觸發(fā)器 GO Alter trigger BankUpdateTrigger on Bank for update as if exists(select*from Bank where monerys=5) rollback?????????--觸發(fā)器回滾,保持數(shù)據(jù)完整性

----------------------------------操作日志表----------------------------------------------------------

create table reapterLog ( ?LogID INT IDENTITY(1,1)PRIMARY KEY NOT NULL, )

轉(zhuǎn)載于:https://www.cnblogs.com/jasonyang/archive/2013/05/22/3093233.html

總結(jié)

以上是生活随笔為你收集整理的存储过程,触发器,分页练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。