SQL之PROCEDURE(存储过程)-飞外

5 where bank_ID = @bank_id 6 if (select count(*) from account where bank_id=@bank_id)=0 7 delete from bank where bank_ID=@bank_id 8 if (select sum(account_balance) from account where bank_id=@bank_id group by bank_id) 1000000 9 insert into account(bank_id) values(@bank_id)10 END
 1 --创建存储过程 2 if (exists (select * from sys.objects where name = 'proc_get_student')) 3 drop proc proc_get_student 4 go 5 create proc proc_get_student 6 as 7 select * from student; 9 --调用、执行存储过程10 exec proc_get_student;

 1 --带通配符参数存储过程 2 if (object_id('proc_findStudentByName', 'P') is not null) 3 drop proc proc_findStudentByName 4 go 5 create proc proc_findStudentByName(@name varchar(20) = '%j%', @nextName varchar(20) = '%') 6 as 7 select * from student where name like @name and name like @nextName; 8 go10 exec proc_findStudentByName;11 exec proc_findStudentByName '%o%', 't%';

 1 if (object_id('proc_getStudentRecord', 'P') is not null) 2 drop proc proc_getStudentRecord 3 go 4 create proc proc_getStudentRecord( 5 @id int, --默认输入参数 6 @name varchar(20) out, --输出参数 7 @age varchar(20) output--输入输出参数 9 as10 select @name = name, @age = age from student where id = @id and sex = @age;11 go13 -- 14 declare @id int,15 @name varchar(20),16 @temp varchar(20);17 set @id = 7; 18 set @temp = 1;19 exec proc_getStudentRecord @id, @name out, @temp output;20 select @name, @temp;21 print @name + '#' + @temp;

 1 --加密WITH ENCRYPTION  2 if (object_id('proc_temp_encryption', 'P') is not null) 3 drop proc proc_temp_encryption 4 go 5 create proc proc_temp_encryption 6 with encryption 7 as 8 select * from student; 9 go11 exec proc_temp_encryption;12 exec sp_helptext 'proc_temp';13 exec sp_helptext 'proc_temp_encryption';

16 exec proc_cursor @cur = @exec_cur output;--调用存储过程17 fetch next from @exec_cur into @id, @name, @age;18 while (@@fetch_status = 0)19 begin20 fetch next from @exec_cur into @id, @name, @age;21 print 'id: ' + convert(varchar, @id) + ', name: ' + @name + ', age: ' + convert(char, @age);22 end23 close @exec_cur;24 deallocate @exec_cur;--删除游标
28 declare @startRow int, @endRow int29 set @startRow = (@pageIndex - 1) * @pageSize +130 set @endRow = @startRow + @pageSize -131 select * from (32 select *, row_number() over (order by id asc) as number from student 33 ) t34 where t.number between @startRow and @endRow;36 exec pro_stu 2, 2;

作者:耑新新,发布于博客园

转载请注明出处,欢迎邮件交流:zhuanxinxin@aliyun.com