存储过程,触发器,分页练习
-------------------------------------游標和循環--------------------------------
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('測試數據') 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,'測試數據'
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,???--每頁多少條數據 @pageCount int output?--總共多少頁 as declare @firstIndex int?--firstIndex開始 declare @lastIndex?int?--lastIndex結束 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 --轉賬賬戶減去金額 ?set @Counterror=@Counterror+@@ERROR ?UPDATE Bank SET MONERYS=MONERYS+@MONEYS WHERE ID=@TOID?? --轉賬賬戶加上金額 ?set @Counterror=@Counterror+@@ERROR IF? @Counterror<>0? --判斷是否出現錯誤 ?begin rollback tran? --如果有錯開始回滾 ?RETURN 0 ?END ?else?? ????--如果沒有錯誤 ?begin commit tran --執行事務 ?RETURN 1 END GO ?execute PROC_MON 1,2,2000 ?select*from bank ?
?-----------------------------------------------------觸發器操作-------------------------------------------- ----
DML觸發器:DML事件即指在表或視圖中修改數據的insert、update、delete語句。 ----DDL觸發器:?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???? --查看觸發器內容 select * from sysobjects where xtype='TR' --產看數據庫多少觸發器 disable trigger BankUpdateTrigger on Bank? --禁用觸發器 enable? trigger BankUpdateTrigger on Bank?--開啟觸發器 GO Alter trigger BankUpdateTrigger on Bank for update as if exists(select*from Bank where monerys=5) rollback?????????--觸發器回滾,保持數據完整性
----------------------------------操作日志表----------------------------------------------------------
create table reapterLog ( ?LogID INT IDENTITY(1,1)PRIMARY KEY NOT NULL, )
轉載于:https://www.cnblogs.com/jasonyang/archive/2013/05/22/3093233.html
總結
以上是生活随笔為你收集整理的存储过程,触发器,分页练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [翻译] WindowsPhone-Ga
- 下一篇: sicp第一章部分习题解答