存储过程示例

存储过程示例存储过程示例 例 1 创建不带参数的存储过程 输出系统日期 create or replace procedure output date is begin dbms output put line sysdate end output date 例 2 带参数 in 和 out 的存储过程 create or replace procedure get username v id in

存储过程示例:

例1:创建不带参数的存储过程
输出系统日期
create or replace procedure output_date is
begin
dbms_output.put_line(sysdate);
end output_date;

例2 带参数in和out的存储过程
create or replace procedure get_username(v_id in number, v_username out varchar2)
as
begin
select username into v_username from tab_user where id=v_id; –变量赋值
exception
when no_data_found then
raise_application_error(-2001,’ID不存在!’);
end get_username;

例3:一个高效的数据分页的存储过程、
create procedure pageTest
(
@FirstId nvarchar(20)=null, –当前页面里的第一条记录的排序字段的值
@LastID nvarchar(20)=null, –当前页面里的最后一条记录的排序字段的值
@isNext bit=null, –true 1: 下一页; false 0:上一页;
@allCount int output, –返回总记录书
@pageSize int output, –返回一夜的记录数
@CurPage int –也好(第几页) 0:第一页;-1最后一页
)
AS
if @CurPage = 0 –第一页;
begin –统计总记录数
select @allCount=count(ProductId) from Product_test

set @pageSize = 10
–返回第一页的数据
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,
Intorduction
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 ProductId
end
)

编程小号
上一篇 2025-02-22 16:27
下一篇 2025-03-14 09:46

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/128708.html