sql存储过程⼏个简单例⼦(⼀)
导读:sql存储是数据库操作过程中⽐较重要的⼀个环节,对于⼀些初学者来说也是⽐较抽象难理解的,本⽂我将通过⼏个实例来解析数据库中的sql存储过程,这样就将抽象的事物形象化,⽐较容易理解。
例1:
create proc proc_stu
nationwide
@sname varchar(20),
@pwd varchar(20)
as
lect * from ren where sname=@sname and pwd=@pwd
go
查看结果:proc_stu 'admin','admin'
例2:
下⾯的存储过程实现⽤户验证的功能,如果不成功,返回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
在程序中调⽤该存储过程,并根据@LEGAL参数的值判断⽤户是否合法。
例3:⼀个⾼效的数据分页的存储过程可以轻松应付百万数据
CREATE PROCEDURE pageTest --⽤于翻页的测试
英语在线--需要把排序字段放在***列
(
@FirstID nvarchar(20)=null, --当前页⾯⾥的***条记录的排序字段的值
@LastID nvarchar(20)=null, --当前页⾯⾥的***⼀条记录的排序字段的值
@isNext bit=null, --true 1 :下⼀页;fal 0:上⼀页
@allCount int output, --返回总记录数
@pageSize int output, --返回⼀页的记录数
@CurPage int --页号(第⼏页)0:***页;-1***⼀页。
)
AS
if @CurPage=0--表⽰***页
mehjdfbegin
-
-统计总记录数
lect @allCount=count(ProductId) from Product_test
t @pageSize=10
--返回***页的数据
克什米尔问题
lect top 10
ProductId,
ProductName,
Introduction
from Product_test order by ProductId
end
el if @CurPage=-1--表⽰***⼀页
doggybag
lect * from
(lect top 10 ProductId,
ProductName,
Introductionbroadcaster
from Product_test order by ProductId desc ) as aa
soworder by ProductId
el
begin
if @isNext=1
--翻到下⼀页
lect top 10 ProductId,
ProductName,
Introduction
from Product_test where ProductId > @LastID order by ProductId
el
--翻到上⼀页
lect * from
(lect top 10 ProductId,
ProductName,新东方优能中学教育
Introduction
from Product_test where ProductId < @FirstID order by ProductId desc) as bb order by ProductId
end
违约金英语上⽂中讲到的这三个例⼦都是sql存储过程⽐较典型的例⼦,希望⼤家好好学习,都能够学到⼤家各⾃需要的东西。