简单的存储过程案例

创建存储过程示例
这是一个创建存储过程的模板代码  直接复制粘贴 修改表名就可用

--创建存储过程
create or replace produce getCount(typeno in varchar2,keycode in varchar2)
--声明变量
is
var_name varchar2(32);
var_dicname varchar2(128);
--声明一个游标
cursor key_content is
--给游标赋值 编写具有结果集的sql语句
select key_word_content from iqm_quality_keyword where key_word_type = keycode;
--定义一个游标变量 获取每一个游标的对象
first_content key_content%rowtype;

begin
    --给变量赋值
    var_name := 'helloworld';
    --将sql语句查询的结果返回给指定的变量
    select dic_name into var_dicname from iqm_base_code where dic_code = typeno and dic_type = 'busiType';
    --将变量输出显示
    dbms_output.put_line('直接赋值的变量为:'+var_name);

    dbms_output.put_line('通过sql赋值的变量为:'+var_dicname);

    --遍历游标获取结果
    for first_content in key_content loop
        --在这里获取游标的结果需要.字段名  不能直接输出变量
	dbms_output.put_line('通过游标赋值的变量为:'+first_content.key_word_content);
    end loop;
end;




帮助那些想尽快学习存储过程 实例:create PROCEDURE pagination @tblName varchar(255), -- 表名 @strGetFields varchar(1000) = '*', -- 需要返回的列 @fldName varchar(255)='', -- 排序的字段名 @PageSize int = 10, -- 页尺寸 @PageIndex int = 1, -- 页码 @pagecount int output, -- 返回页总数, 非 0 值则返回 --@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 @strWhere varchar(1500) = '1=1' -- 查询条件 (注意: 不要加 where) AS declare @sql nvarchar(2000) --获得表中所有记录的条数 declare @recordcount int declare @getcountsql nvarchar(2000) set @getcountsql = N'select @count = count(*) from ' + @tblName + N' where ' + @strWhere exec sp_executesql @getcountsql,N'@count int output',@count=@recordcount output if @recordcount=0 begin set @pagecount = 0 return end declare @lastcount int set @lastcount = @recordcount % @PageSize if @lastcount = 0 set @pagecount = @recordcount / @PageSize else set @pagecount = @recordcount / @PageSize + 1 if @lastcount = 0 or @pageindex < @pagecount begin set @sql = N'select ' + @strGetFields + N' from (select top ' + convert(nvarchar(4),@PageSize) + N' * from (select top ' + convert(varchar(10),@PageSize*@PageIndex) + N' * from ' + @tblName + N' where ' + @strWhere + N' order by ' +@fldName+ N') as t order by ' + @fldName + ' desc) as tt order by ' + @fldName end else begin if @lastcount != 0 and @pageindex = @pagecount begin set @sql = N'select ' + @strGetFields + N' from (select top ' + convert(nvarchar(4),@lastcount) + N' * from (select top ' + convert(varchar(10),@PageSize*@PageIndex) + N' * from ' + @tblName + N' where ' + @strWhere + N' order by ' +@fldName+ N') as t order by ' + @fldName + ' desc) as tt order by ' + @fldName end end print @sql exec sp_executesql @sql go select * from authors declare @pagecount int exec pagination 'authors','*','au_id',5,1, @pagecount output,'state=''CA''' print @pagecount
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值