码迷,mamicode.com
首页 > 其他好文 > 详细

创建存储过程

时间:2014-11-09 23:33:46      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:style   color   ar   os   使用   sp   strong   文件   数据   

Use new

go

 

select *from xuesheng

select *from fenshu

select *from jiaoshi

==============================================

--查‘赵文‘教的语文成绩>80 的学生信息,如果人数超过个,考核达标

----正确的写法

declare @count int

select @count=count(*) from fenshu  where code  in (

select code from xuesheng  where yuhao =(select code from jiaoshi  where name=‘赵文‘)

) and yufen >=80

print @count

if @count>3

print ‘达标‘

else

print ‘不达标‘

 

==================================================

create proc firstproc

as

select *from fenshu

go   --好比C#中创建的函数,要引用才起作用!

--在数据库的存储过程文件夹里,找到保存的存储过程,右键单击打开‘执行存储过程‘就会完成执行.

--并返回值,0代表完成!

declare @fanhuizhi  int 

execute  @fanhuizhi=firstproc  --查询

--或者exec  firstproc  也可以执行firstproc 存储过程。

--print  @fanhuizhi --在结果中打印出表格,在消息中显示.

select @fanhuizhi --在结果中显示两个表,一个表示查询的表,另一个是返回值!

 

-----------------------修改存储过程-----------------------

alter  proc firstproc

as

select fenshu.code ,yufen,name from fenshu,xuesheng

where fenshu.code =xuesheng.code

go

exec firstproc

 

 ----------------------查询多个表-------------------------

 create proc secondproc

 as

 begin

  select *from xuesheng

  select *from fenshu

 end

 go

 execute  secondproc

 

 -----------------------含参数的存储过程-----------------------------

create proc thirdproc

 @name varchar(20)

as

begin

 declare @jiaoshicode   int, @kecheng varchar(20)

 select  @jiaoshicode ,@kecheng from jiaoshi where name =@name

 if @kecheng =‘语文‘

   begin

   select @count=count(*) from fenshu  where code  in (

select code from xuesheng  where yuhao =(select code from jiaoshi  where name=‘赵文‘)

   end

 if @kecheng=‘数学‘

   begin

      select @count=count(*) from fenshu  where code  in (

select code from xuesheng  where shuhao =(select code from jiaoshi  where name=‘李数‘)

   end

 if @kecheng =‘英语‘

   begin

         select @count=count(*) from fenshu  where code  in (

select code from xuesheng  where yinghao =(select code from jiaoshi  where name=‘张外‘)

   end

end

go

exec thirdproc  ‘李数‘--  ‘李数‘(教师表中的name)是执行存储过程的变量值!

------------------------------------------------------

create proc fouthproc

@hello  varchar(20)

as

 begin

 print @hello

 end

go

exec  fouthproc   ‘今天是个好天气‘表示变量@hello被赋值‘今天是个好天气--注意书写流程!

----------------输入学号,判断学生结业--------------------------------------

create proc biye

@xuehao int----------创建变量.  就是需要输入值去赋值的变量

as

begin

    declare @count int

    select @count=COUNT(*) from xuesheng where code=@xuehao

    if @count =0

       print ‘请检查输入的学号是否有误!‘

    else

    begin

    declare @yu int

    select @yu=COUNT(*) from fenshu where code=@xuehao and yufen>=60

    declare @shu int

    select @shu=COUNT(*) from fenshu where code=@xuehao and shufen>=60

    declare @ying int

    select @ying=COUNT(*) from fenshu where code=@xuehao and yingfen>=60

    declare @he int

    set @he=@yu+@shu+@ying

    if @he=3

       print ‘优秀!已经结业!‘

    if @he=2

       print ‘已经结业!‘

    if @he=1

       print ‘不能结业!请重修两门课程‘

    if @he=0

       print ‘请重修所有课程!‘

    end

end

go

exec biye  1   --输入code值是 1,表示查询存储过程biye

drop proc biye  --删除创建的存储过程biye

------------------使用return接收---------------------------------------

create proc jieshou

@hao int----------创建变量.  就是需要输入值去赋值的变量

as

begin

    set @hao=@hao+10  --赋值!

    return @hao 

end

go   --存储过程完成,return接收,但是没有打印

 

declare @abc  int    --打印结果要创建新变量,打印!

exec @abc=jieshou  3     --写   存储过程   名字!

print  @abc          --当变量@hao=3时,存储过程jieshou,  return的结果   13

-------------------使用return 累加求和-------------------------

create proc leijia

@n int

as

begin

  declare @sum int=0 ,@a int =0   --创建,并且赋值为

   while @a <=@n          --while循环        

   begin

     set @sum =@sum+@a

     set @a=@a+1               --赋值时一定要  set 

   end

  return @sum

end

go

 

declare @haha int 

exec @haha=leijia   5

print @haha      --输出5的累加     15

 

--------输出多变量情况 (return、output 接收方法)-----------

 craete proc sixproc  --做修改后,要变成alter proc sixproc

 @code  int ,

 @yu decimal(18,2) output,

 @shu decimal(18,2) output,

 @ying decimal(18,2) output

as

begin

 declare @count int 

 select @count=COUNT (*) from  fenshu where code=@code

 select @yu=yufen ,@shu=shufen,@ying=yingfen  from fenshu where code=@code

 return @count  --运行到return语句,就结束语句执行。所以一般在最后或循环里面

end

 go

 

 declare @yu decimal(18,2),@shu decimal(18,2),@ying decimal(18,2),@count int

--创建对应的 接收 变量

 exec  @count=sixproc 1 (表示查询的方式),@yu output ,@shu output,@ying output

--(output的接收方法)

 print @yu +@shu +@ying      --表示code是1 的语数外总和! 

 print @count        --此时返回1.表示个数!

创建存储过程

标签:style   color   ar   os   使用   sp   strong   文件   数据   

原文地址:http://www.cnblogs.com/huaze/p/4086146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!