--在sql语句中 begin...end 用来设定一个程序块 相关于c#中的{}
declare @yz real,@w int --声明变量
set @w=120 --为变量赋值
if @w<=100 --if条件语句
begin --Begin程序块
set @yz=@w*0.12 --为变量赋值
end
else
begin
set @yz=100*0.12+(@w-100)*0.05
end
/*输出邮件的重量和邮费*/
print '邮件的重量是:'+cast(@w as varchar(20))+'克'
print '邮费是:'+cast(@yz as varchar(20))+'元' -- if-else在数据库查询的应用
declare @x int
set @x =1000
if @x>950
begin
set @x=@x+1000
select * from 职工 where 工资>@x
end
else
begin
set @x=@x-50
select * from 仓库 where 面积<@x
END -- if...else if ...多条件判断
declare @hyuser varchar(50) ,@hypwd varchar(50),@msg varchar(50)
select @hyuser='liping' , @hypwd='111' if @hyuser='hystu1'
begin
if @hypwd='111'
set @msg='用户名与密码正确,成功登录!'
else
set @msg='密码不正确,请重新输入!'
end
else if @hyuser='hystu2'
begin
if @hypwd='222'
set @msg='用户名与密码正确,成功登录!'
else
set @msg='密码不正确,请重新输入!'
end
else if @hyuser='hystu3'
begin
if @hypwd='333'
set @msg='用户名与密码正确,成功登录!'
else
set @msg='密码不正确,请重新输入!'
end
else
set @msg='用户名不正确,请重新输入!'
print @msg --if...else...登陆判断
declare @hyuser varchar(50) ,@hypwd varchar(50),@msg varchar(50)
select @hyuser='stu1' , @hypwd='111'
if exists(select * from hyuser where hyname=@hyuser )
begin
if exists(select * from hyuser where hyname=@hyuser and hypwd=@hypwd )
set @msg='用户名与密码都正确,成功登录!'
else
set @msg='密码不正确,请重新输入!'
end
else
set @msg='用户名不正确,请得新输入!'
print @msg --case when 多条件判断
declare @cj float,@str varchar(60)
set @cj=90
set @str=
case
when @cj>100 or @cj<0 then '你输入的成绩不对,成绩应在0-100之间'
when @cj>=60 and @cj<70 then '及格'
when @cj>=70 and @cj<80 then '中等'
when @cj>=80 and @cj<90 then '优良'
when @cj>=90 and @cj<=100 then '优秀'
else '不及格'
end
print '该学生的成绩评语:' + @str --查询不同仓库的平均工资
select * ,
不同仓库的平均工资 =
case
when 仓库号='wh1' then (select avg(工资) from 职工 where 仓库号='wh1')
when 仓库号='wh2' then (select avg(工资) from 职工 where 仓库号='wh2')
when 仓库号='wh3' then (select avg(工资) from 职工 where 仓库号='wh3')
when 仓库号='wh4' then (select avg(工资) from 职工 where 仓库号='wh4')
end
from 职工 --利用循环语句 while计算1-120相加总和
declare @x int, @sum int --声明变量
select @x=0,@sum=0 --为变量赋值
while @x<=120 --While循环语句
begin --程序块的开始
set @sum=@sum+@x
set @x=@x+1
end --程序块的结构
--利用print进行输出
print '从1加120之和是:'+ cast(@sum as varchar(50)) --跳转语句 break
--求从1加到10的和 但如果求得的和大于30 则跳出
declare @x int, @sum int
select @x=0,@sum=0
while @x<=10
begin
set @x=@x+1
set @sum=@sum+@x
if @sum>30
break
end
waitfor delay '00:00:01'
print '等1秒后输出'
print '最后结果是:'+ cast(@sum as varchar(50)) --跳转语句 continue
--求1-120之间所有偶数的和
declare @x int, @sum int
select @x=0,@sum=0
while @x<=120
begin
set @x=@x+1
if @x%2=1
continue
set @sum=@sum+@x
end
print '从1加120所有偶数之和是:'+ cast(@sum as varchar(50)) --跳转语句 goto
--利用goto求1-100之间的和
declare @x int,@sum int
set @x=0
set @sum=0
bz:
set @x=@x+1
set @sum=@sum+@x
while @x<100 goto bz
print '利用Goto语名求从1加到100的和:'+ cast(@sum as varchar(50)) --跳转语句 return
--创建自定义函数 计算奖金
create function myfun(@x int) returns int
as
begin
declare @y int
set @y=@x*0.15
return @y
END select *,dbo.myfun(工资) as 奖金 from 职工

SqlServer和Oracle中一些常用的sql语句5 流程控制语句的更多相关文章

  1. SqlServer和Oracle中一些常用的sql语句9 SQL优化

    --SQL查询优化 尽量避免使用or,not,distinct运算符,简化连接条件 /*Or运算符*/ use db_business go select * from 仓库 where 城市='北京 ...

  2. SqlServer和Oracle中一些常用的sql语句10 特殊应用

    --482, ORACLE / SQL SERVER --订购数量超过平均值的书籍 WITH Orders_Book AS ( SELECT Book_Name, SUM(Qty) Book_Qty ...

  3. SqlServer和Oracle中一些常用的sql语句3 行列转换

    --217, SQL SERVER SELECT Cust_Name , MAX(CASE WHEN Order_Date ='2009-08-01' THEN AR END) "2009- ...

  4. SqlServer和Oracle中一些常用的sql语句6 存储过程

    --不带参数的存储过程 CREATE procedure proc_sql1 as begin declare @i int set @i=0 while @i<26 begin print c ...

  5. SqlServer和Oracle中一些常用的sql语句7 游标

    declare db_cursor4 scroll cursor for select * from 供应商 --声明游标 open db_cursor4 --打开游标 fetch first fro ...

  6. SqlServer和Oracle中一些常用的sql语句8 触发器和事务

    --创建和执行事后触发器 --更新仓库备份表中记录时自动创建数据表且插入三条记录 create trigger db_trigger1 on 仓库备份 for update as begin if E ...

  7. SqlServer和Oracle中一些常用的sql语句4 局部/全局变量

    --把wh1仓库号中姓名含有"平"字的职工工资在原来的基础上加288 update 职工备份 set 工资=工资+288 where 仓库号='wh1' and 姓名 like ' ...

  8. SqlServer 获取字符串中小写字母的sql语句

    SQL字符串截取(SubString) 作用:返回第一个参数中从第二个参数指定的位置开始.第三个参数指定的长度的子字符串. 有时候我们会截取字符串中的一些特殊想要的东西,大小写字母.模号.汉字.数字等 ...

  9. SqlServer 查看备份文件中逻辑文件信息的Sql语句

    RESTORE FILELISTONLY FROM DISK = 'D:\All\DataBase\(2013-12-18)-1.bak' 用来查看备份文件中的逻辑文件信息. 相关信息:SqlServ ...

随机推荐

  1. 开始学习.net的第二天

    今天由于原因上午没上课.下午去了学的.net的表格. <body></body><img src="../temp/新建文件夹/64aab4ae3e632dbc ...

  2. RAID基础知识总结

    1.RAID RAID:Redundant Arrays of Inexpensive(Independent)Disks,即独立磁盘冗余阵列,简称磁盘阵列.简单地说就是把多个独立的硬盘组合起来,从而 ...

  3. vim文本基础

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...

  4. select设置disable后ie修改默认字体颜色暂时解决

    找了很多资料,终于在科学上网后找到了一个方法,虽然暂时不知道原理,但是已经实现了功能就是好的 select[disabled='disabled']::-ms-value { color: #000; ...

  5. 1st 四则运算题目生成程序

    程序代码见此 程序展示 需求分析 需要程序能根据用户指定生成四则运算的题目,并且能让用户做题,并且最后打分统计正确率 功能设计 主要实现的功能就是: 接受用户输入以便知道要出多少道题目(-n x) 能 ...

  6. 201521123008《Java程序设计》第四周学习总结

    1.本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.包的命名规范 2.面向对象设计:名词/动词 3.类的设计技巧:一定将属性设计为私有priv ...

  7. 201521123115 《Java程序设计》第12周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 将Student对象(属性:int id, String name,int age,doubl ...

  8. 201521123031 《Java程序设计》第13周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 1. 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu ...

  9. 网络配置之基本网络配置(cenos6)

    目录: 关于IP的管理 Linux网卡的卸载与装载 配置网络接口 网络IP配置文件路由管理 路由管理命令 配置动态路由(简介) route的配置文件netstat命令IP命令 ip link 查看网络 ...

  10. 分区工具fdisk,gdisk,parted

    在linux中,当我们给系统添加一块新硬盘时,我们是无法使用的,因为他还没有分区和格式化,只有当我们将新硬盘分区并格式化之后,挂载在某个目录下,才能供我们正常使用,接下来我们要学习三种硬盘分区工具,f ...