存储过程和事务2

上一篇 / 下一篇  2014-06-09 16:21:39 / 个人分类:数据

    dropprocproc_find_stu
go
createprocproc_find_stu(@startIdint, @endIdint)
as
   
select*fromstudentwhereidbetween@startIdand@endId
go

execproc_find_stu 2, 4;

 

   5带通配符参数存储过程

--带通配符参数存储过程
if(object_id('proc_findStudentByName','P')isnotnull)
   
dropprocproc_findStudentByName
go
createprocproc_findStudentByName(@namevarchar(20) ='%j%', @nextNamevarchar(20) ='%')
as
   
select*fromstudentwherenamelike@nameandnamelike@nextName;
go

execproc_findStudentByName;
execproc_findStudentByName'%o%','t%';

 

   6带输出参数存储过程

if(object_id('proc_getStudentRecord','P')isnotnull)
   
dropprocproc_getStudentRecord
go
createprocproc_getStudentRecord(
    @id
int, --默认输入参数
    @name
varchar(20)out, --输出参数
    @age
varchar(20)output--输入输出参数
)
as
   
select@name = name, @age = age fromstudentwhereid = @idandsex = @age;
go

--
declare@idint,
        @name
varchar(20),
        @temp
varchar(20);
set@id = 7;
set@temp = 1;
execproc_getStudentRecord @id, @nameout, @tempoutput;
select@name, @temp;
print@name +'#'+ @temp;

 

 

   7不缓存存储过程

--WITHRECOMPILE不缓存
if(object_id('proc_temp','P')isnotnull)
   
dropprocproc_temp
go
createprocproc_temp
withrecompile
as
   
select*fromstudent;
go

execproc_temp;

 

   8加密存储过程

--加密WITH ENCRYPTION
if(object_id('proc_temp_encryption','P')isnotnull)
   
dropprocproc_temp_encryption
go
createprocproc_temp_encryption
withencryption
as
   
select*fromstudent;
go

execproc_temp_encryption;
execsp_helptext'proc_temp';
execsp_helptext'proc_temp_encryption';

 

   9带游标参数存储过程

if(object_id('proc_cursor','P')isnotnull)
   
dropprocproc_cursor
go
createprocproc_cursor
    @cur
cursorvaryingoutput
as
   
set@cur =cursorforward_onlystaticfor
   
selectid, name, agefromstudent;
   
open@cur;
go
--
调用
declare@exec_curcursor;
declare@idint,
        @name
varchar(20),
        @age
int;
execproc_cursor @cur = @exec_curoutput;--调用存储过程
fetchnextfrom@exec_curinto@id, @name, @age;
while(@@fetch_status = 0)
begin
   
fetchnextfrom@exec_curinto@id, @name, @age;
   
print'id: '+convert(varchar, @id) +', name: '+ @name +', age: '+convert(char, @age);
end
close@exec_cur;
deallocate@exec_cur;--删除游标

 

 

   10分页存储过程

---存储过程、row_number完成分页
if(object_id('pro_page','P')isnotnull)
   
dropprocproc_cursor
go
createprocpro_page
    @startIndex
int,
    @endIndex
int
as
   
selectcount(*)fromproduct
;   
   
select*from(
       
selectrow_number()over(orderbypid)asrowId, *fromproduct
    ) temp
   
wheretemp.rowIdbetween@startIndexand@endIndex
go
--
dropprocpro_page
execpro_page 1, 4
--
--分页存储过程
if(object_id('pro_page','P')isnotnull)
   
dropprocpro_stu
go
createprocedurepro_stu(
    @pageIndex
int,
    @pageSize
int
)

TAG: 事务 存储过程

 

评分:0

我来说两句

日历

« 2024-04-26  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 104933
  • 日志数: 55
  • 建立时间: 2012-06-11
  • 更新时间: 2017-09-07

RSS订阅

Open Toolbar