1、存储过程

(1)新建一个存储过程

create proc ChaXun

as

begin

  select *from Score

end

go

(2)执行

exec chaxun

(3)删除

drop proc chaxun

(4)修改

alter proc ChaXun

as

begin

  select *from Score

end

go

(5)向存储过程中输入参数

alter proc jiayibai

@shuru int,

@canshu int

as

begin

  print @shuru+@canshu

end

go

exec jiayibai 10,120

例:

(1)判断是否闰年

create proc isRunNian

@year int

as

begin

  if @year%4=0 and @year%100!=0

  begin

    print '是闰年'

  end

  else if @year%100=0 and @year%400=0

  begin

    print '是闰年'

  end

  else

  begin

    print '不是闰年'

  end

end

go

declare @jieguo int

exec @jieguo=isRunNian 2100

print @jieguo

(2)判断是不是一个一元二次方程

create proc yiyuanerci

@a int,

@b int,

@c int

as

begin

if @a=0

begin

  --print '不是一元二次方程'

  return 1

end

else

begin

declare @sqrt decimal(18,2)

set @sqrt = @b*@b-4*@a*@c

if @sqrt>0

begin

  --print '两个不同的根'

  return 2

end

else if @sqrt=0

begin

  --print '两个相同的根'

  return 3

end

else

begin

  --print '无解'

  return 4

end

end

end

go

declare @fanhui int

exec @fanhui = yiyuanerci 0,4,3

if @fanhui=1

  print '请仔细阅读一元二次方程的构造规则'

2、out输出参数

alter proc outzhi

@shuru int,

@jiashi int output,

@jiaershi int output

as

begin

  set @jiashi=@shuru+10

  set @jiaershi=@shuru+20

  return 1

end

go

declare @shuchu1 int,@shuchu2 int,@return int

exec @return=outzhi 15,@shuchu1 output,@shuchu2 output

print @shuchu1

print @shuchu2

print @return

例:

(1)闰年

create proc runnian

@y int

as

begin

  if (@y%100=0 and @y%400=0) or(@y%100<>0 and @y%4=0)

  begin

    return 1

  end

  else

  begin

    return 2

  end

end

go

declare @a int

exec @a=runnian 1990

if @a=1

print '是闰年'

if @a=2

print '不是闰年'

一元二次方程

alter proc yiyuanercifangcheng

@a decimal(18,2),@b decimal(18,2),@c decimal(18,2),

@y1 decimal(18,2) output,

@y2 decimal(18,2) output

as

begin

  declare @n decimal(18,2)

  if @a=0

  begin

    return 1

  end

  else

  begin

    if @b*@b-4*@a*@c<0

    begin

      return 2

    end

    if @b*@b-4*@a*@c=0

    begin

      set @n=sqrt(@b*@b-4*@a*@c)

      set @y1=(-@b+@n)/(2*@a)

      return 3

    end

    if  @b*@b-4*@a*@c>0

    begin

      set @n=sqrt(@b*@b-4*@a*@c)

      set @y1=(-@b+@n)/(2*@a)

      set @y2=(-@b-@n)/(2*@a)

      return 4

    end

  end

end

go

declare @j1 decimal(18,2),@j2 decimal(18,2),@return int

exec @return=yiyuanercifangcheng 1,22,1,@j1 output,@j2 output

if @return=1

begin

  print '因为a=0,所以该方程不是一元二次方程'

end

if @return=2

begin

  print '因为Δ<0,所以该方程无解'

end

if @return=3

begin

  print '因为Δ=0,所以该方程有两个相等的实数根'

  print 'x1=x2='+cast(@j1 as varchar(20))

end

if @return=4

begin

  print '因为Δ>0,所以该方程有两个不相等的实数根'

  print 'x1='+cast(@j1 as varchar(20))

  print 'x2='+cast(@j2 as varchar(20))

End

3、while循环语句

累加求和

alter proc he

@a int,@s int output

as

begin

declare @i int

set @s=0

set @i=1

while @i<=@a

begin

set @s=@s+@i

set @i=@i+1

end

end

go

declare @b int

exec he 10,@b output

print @b

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

练习:

1、打印任意行helloworld

create proc dayin

@a int

as

begin

declare @i int

set @i=1

while @i<=@a

begin

print 'helloworld '+cast(@i as varchar(20))

set @i=@i+1

end

end

go

exec dayin 100

2、求到你输入的数之间所有奇数的和

create proc jishuhe

@a int,@s int output

as

begin

declare @i int

set @i=1

set @s=0

while @i<=@a

begin

set @s=@s+@i

set @i=@i+2

end

end

go

declare @b int

exec jishuhe 100,@b output

print @b

3、判断一个数是不是质数

方法一:

create proc zhishu

@a int

as

begin

Declare @n int

set @n=2

while @n<@a

Begin

if @a%@n=0

begin

return 1

end

set @n=@n+1

end

end

go

declare @x int

exec @x=zhishu 1111

if @x=1

print '不是质数'

else

print '是质数'

方法二:
create proc zhishu2

@a int

as

begin

declare @m int,@c int

set @m=1

set @c=0

while @m<=@a

begin

declare @yu int

set @yu=@a%@m

if @yu=0

begin

set @c=@c+1

end

set @m=@m+1

end

if @c>2

begin

return 1

end

else

begin

return 2

end

end

go

declare @x int

exec @x=zhishu2 2

if @x=1

print '不是质数'

else

print '是质数'

4、输入一个数,求这个数的阶乘

create proc jiecheng

@a int,@s int output

as

begin

declare @i int

set @s=1

set @i=1

while @i<=@a

begin

set @s=@s*@i

set @i=@i+1

end

end

go

declare @b int

exec jiecheng 6,@b output

print @b

5输入一个数,求这个数的阶乘的和

方法一:

create proc jiechenghe

@a int,@s int output,@h int output

as

begin

declare @i int

set @s=1

set @i=1

set @h=0

while @i<=@a

begin

set @s=@s*@i

set @h=@h+@s

set @i=@i+1

end

end

go

declare @b int,@c int

exec jiechenghe 3,@b output,@c output

print @c

方法二:
create proc jiechenghe2

@a int

as

begin

declare @i int,@sum int

set @i=1

set @sum=0

while @i<=@a

begin

declare @k int,@jieguo int

set @k=1

set @jieguo=1

while @k<=@i

begin

set @jieguo=@jieguo*@k

set @k=@k+1

end

set @sum=@sum+@jieguo

set @i=@i+1

end

print @sum

end

go

exec jiechenghe2 5

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

6求质数的和

方法一:

create proc zhishuhe

@shuru int,@sum int output

as

begin

declare @a int

set @a=2

set @sum=0

while @a<=@shuru

begin

declare @b int,@count int

set @b=2

set @count=0

while @b<@a

begin

if @a%@b=0

begin

set @count=@count+1

end

set @b=@b+1

end

if @count=0

begin

set @sum=@sum+@a

end

set @a=@a+1

end

end

go

declare @x int

exec zhishuhe 3,@x output

print @x

方法二:

create proc zhishuhe2

@shuru int,@sum int output

as

begin

declare @a int

set @a=2

set @sum =0

while @a<=@shuru

begin

declare @i int,@count int

set @i=1

set @count=0--计算是否还能被其他数整除

while @i<=@a

begin

declare @yu int

set @yu = @a%@i

if @yu=0

begin

set @count=@count+1

end

set @i=@i+1

end

if @count<=2

begin

set @sum=@sum+@a

end

set @a=@a+1

end

end

go

declare @shuchu int

exec zhishuhe2 100,@shuchu output

print @shuchu

转载于:https://www.cnblogs.com/XMH1217423419/p/4135809.html

10、存储过程、while语句相关推荐

  1. Oracle 存储过程调用语句

    #oracle 存储过程调用语句 declare v_custName varchar2(10); --客户姓名 v_num number; --订单分布天数 v_time number; --每日订 ...

  2. mysql存储过程 | 循环语句

    在MySQL存储过程的语句中有三个标准的循环方式:while循环,loop循环以及repeat循环.还有一种非标准的循环方式:goto(不做讲解) while 循环 -- 语法 -- while(表达 ...

  3. VBA中的10种循环语句

    VBA中的10种循环语句 1.For – Next '循环数组 ArraySum = 0 For i = 1 To 10 ArraySum = ArraySum + MyArray(i) Next i ...

  4. MySQL 存储过程 if语句

    MySQL IF语句语法 下面说明了IF语句的语法: IF expression THEN statements; END IF; 如果表达式(expression)计算结果为TRUE,那么将执行st ...

  5. mysql 存过 if语句_mysql存储过程 if 语句

    MySql的存储过程 存储过程和函数是在数据库中定义一些SQL语句的集合,然后直接调用这些存储过程和函数来执行已经定义好的SQL语句.存储过程和函数可以避免开发人员重复的编写相同的SQL语句.而且,存 ...

  6. plsql 查询存储过程死锁语句_SQL2005查看死锁存储过程sp_who_lock

    下面是我整理的监控sql server数据库,在性能测试过程中是否出现死锁.堵塞的SQL语句,还算比较准备,留下来备用. 调用方法:选中相应的数据库,执行exec sp_who_lock USE [m ...

  7. SQL Server 存储过程 SET 语句选项

    SET 语句选项 当创建或更改 Transact-SQL 存储过程后,数据库引擎将保存 SET QUOTED_IDENTIFIER 和 SET ANSI_NULLS 的设置. 执行存储过程时,将使用这 ...

  8. plsql 查询存储过程死锁语句_ORACLE-Kill 杀死正在执行的Oracle存储过程和死锁语句...

    存储过程 1.找到正在执行的存储过程的 sid ,serial# select   b.sid,b.SERIAL#,a.OBJECT, 'alter system kill session   ' | ...

  9. Oracle中修改存储过程名语句,修改存储过程中使用的语句是,select 语句使用存储过程...

    Q1:如何在SELECT语句中调用存储过程的结果 语法 SELECT [predicate] { *table.*[table.]field1 [AS alias1] [, [table.]field ...

  10. sql语言和c语言比,SQL点滴10—使用with语句来写一个稍微复杂sql语句,附加和子查询的性能对比...

    今天偶尔看到sql中也有with关键字,好歹也写了几年的sql语句,居然第一次接触,无知啊.看了一位博主的文章,自己添加了一些内容,做了简单的总结,这个语句还是第一次见到,学习了.我从简单到复杂地写, ...

最新文章

  1. 控制单元维修_「维修案例」一汽大众迈腾挡风玻璃喷水电机不喷水,实操步骤解决...
  2. linux三剑客及正则表达(grep,sed,awk)
  3. linux常用命令:wget 命令
  4. 【算法精讲】集成分类与随机森林
  5. oracle之数据处理之视图
  6. Android DDMS的打开以及查看手机页面布局层次
  7. # 根据三边求角度_七年级数学:怎么求旋转射线构成的角度?掌握这种方法口算出结果...
  8. 央采数据库集采:甲骨文、微软、腾讯、阿里等 21 家中标
  9. 白盒测试哪种测试效果好_白盒测试与黑盒测试区别(简答题)简短一些不要长的谢谢...
  10. IOS 项目性能优化
  11. 【HAVENT原创】kubernetes docker 常用指令
  12. Mac 苹果电脑创建一个新的管理员账号
  13. 用BAT创建文件夹,创建文件,回显环境变量
  14. ue4-UMG和HUD绘制UI
  15. 超级应用/_超级应用
  16. Python合并PDF
  17. itchat实现自动回复好友消息
  18. 基恩士KV7500,KV8000轴控制FB模板,直接可以拿来用,使基恩士编程也随心所欲
  19. 服务器2012系统用什么更新驱动程序,服务堆栈更新程序适用于 Windows 8 和 Windows Server 2012...
  20. Google Earth Engine(GEE)实例代码学习十一——影像全色波段融合提高分辨率(HSV Pan Sharpening)

热门文章

  1. 【JUnit】BeforeClass、AfterClass、Before与After示例
  2. windows添加删除程序打不开解决方案
  3. flutter SlideTransition实现平移动画
  4. webpack是什么?为什么要用webpack(一个小白的感想)
  5. Javascript 随机数函数 学习之一:产生服从均匀分布随机数
  6. python的requests库
  7. 中英文对照 —— 游戏
  8. WinForm中WebBrowser的使用
  9. jQuery原理第五天
  10. [Vue.js] 基础 -- Vue简介