欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

sql存儲過程幾個簡單例子

 更新時間:2016年02月23日 14:01:09   投稿:mdxy-dxy  
存儲過程是一組為了完成特定功能的SQL語句集,是利用SQL Server所提供的Transact-SQL語言所編寫的程序。經(jīng)編譯后存儲在數(shù)據(jù)庫中。存儲過程是數(shù)據(jù)庫中一個重要的對象

sql存儲是數(shù)據(jù)庫操作過程中比較重要的一個環(huán)節(jié),對于一些初學者來說也是比較抽象難理解的,本文我將通過幾個實例來解析數(shù)據(jù)庫中的sql存儲過程,這樣就將抽象的事物形象化,比較容易理解。

例1:

create proc proc_stu 
@sname varchar(20), 
@pwd varchar(20) 
as 
select * from ren where sname=@sname and pwd=@pwd 
go

查看結(jié)果:proc_stu 'admin','admin'

例2:

下面的存儲過程實現(xiàn)用戶驗證的功能,如果不成功,返回0,成功則返回1.

CREATE PROCEDURE VALIDATE @USERNAME CHAR(20),@PASSWORD CHAR(20),@LEGAL BIT OUTPUT
AS

IF EXISTS(SELECT * FROM REN WHERE SNAME = @USERNAME AND PWD = @PASSWORD) 
SELECT @LEGAL = 1 
ELSE 
SELECT @LEGAL = 0

在程序中調(diào)用該存儲過程,并根據(jù)@LEGAL參數(shù)的值判斷用戶是否合法。

例3:一個高效的數(shù)據(jù)分頁的存儲過程 可以輕松應(yīng)付百萬數(shù)據(jù)

CREATE PROCEDURE pageTest --用于翻頁的測試
--需要把排序字段放在第一列

(
@FirstID nvarchar(20)=null, --當前頁面里的第一條記錄的排序字段的值
@LastID nvarchar(20)=null, --當前頁面里的最后一條記錄的排序字段的值
@isNext bit=null, --true 1 :下一頁;false 0:上一頁
@allCount int output, --返回總記錄數(shù)
@pageSize int output, --返回一頁的記錄數(shù)
@CurPage int --頁號(第幾頁)0:第一頁;-1最后一頁。
)

AS

if @CurPage=0--表示第一頁
begin
--統(tǒng)計總記錄數(shù)
select @allCount=count(ProductId) from Product_test 

set @pageSize=10
--返回第一頁的數(shù)據(jù)
select top 10 
ProductId,
ProductName,
Introduction 
from Product_test order by ProductId 
end

else if @CurPage=-1--表示最后一頁

select * from 
(select top 10 ProductId,
ProductName,
Introduction

from Product_test order by ProductId desc ) as aa 
order by ProductId
else

begin 
if @isNext=1
--翻到下一頁
select top 10 ProductId,
ProductName,
Introduction
from Product_test where ProductId > @LastID order by ProductId 
else
--翻到上一頁
select * from
(select top 10 ProductId,
ProductName,
Introduction
from Product_test where ProductId < @FirstID order by ProductId desc) as bb order by ProductId
end

上文中講到的這三個例子都是sql存儲過程比較典型的例子,希望大家好好學習,都能夠?qū)W到大家各自需要的東西。

相關(guān)文章

最新評論