--在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. poj1236强连通缩点

    题意:给出每个学校的list 代表该学校能链接的其他学校,问1:至少给几个学校资源使所有学校都得到:2:至少加多少个边能让所有学校相互连通: 思路:1:找出缩点后入度为零的点个数  2:找出缩点后入度 ...

  2. [自制操作系统] JOS文件系统详解&支持工作路径&MSH

    本文分为两部分: 第一部分将详细分析JOS的文件系统及文件描述符的实现方法. 第二部分将实现工作路径,提供新的系统调用,完善用户空间工具. 本文中支持的新特性: 支持进程工作目录 提供getcwd与c ...

  3. Jquery 绑定标签事件

    为子元素绑定: $('#foreachResult').delegate('td', 'click', function () {            alert($(this).text());  ...

  4. 201521123109《java程序设计》第三周学习总结

    1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识组织起来.请使用纸笔或者下面的工具画出本周学习到的知识点.截图或者拍照上传. 2. 书面作 ...

  5. 201521123088《Java程序设计》第12周学习总结

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

  6. java第十二次作业

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

  7. Q:哪里可以注册hk域名?A:这里!这里!(小白绢挥手)

    注意!前方有一条比你妈手中的竹板还硬的推文出没······ 咳咳,清清喉咙,预备唱! (请自动代入甜蜜蜜的曲调) 甜蜜蜜你笑的甜蜜蜜  好像花儿开在春风里  开在春风里 在哪里在哪里见过你  .HK域 ...

  8. panda库2

    >>> a=pd.Series([1,2],index=['a','b']) >>> a a 1 b 2 dtype: int64 >>> b=p ...

  9. 前端angularJS利用directive实现移动端自定义软键盘的方法

    最近公司项目的需求上要求我们iPad项目上一些需要输入数字的地方用我们自定义的软键盘而不是移动端设备自带的键盘,刚接到需求有点懵,因为之前没有做过,后来理了一下思路发现这东西也就那样.先看一下实现之后 ...

  10. android动画的实现过程

    先上自己的测试代码,有参考apidemo中的AnimationDrawable中的方法 public class AnimateActivity extends Activity { @Overrid ...