现在的位置: 首页 > 综合 > 正文

SQL之流程控制

2018年05月20日 ⁄ 综合 ⁄ 共 5397字 ⁄ 字号 评论关闭

分为变量声明、流程控制、数据操纵、数据控制、内嵌函数和自定义函数

1:局部变量的声明和赋值

     Declare @变量名 变量类型

    可以声明一个或者多个变量,变量被声明后被赋值null

    

    赋值:

     set @变量名=变量值

     select @变量名=变量值[,变量名=变量值]

  在oracle中,变量赋值的方法为:v_prodcount := 10

  在DB2中,为变量赋值的方法如下:set v_prodcount := 10;

 

   

declare @str1 char(10), @str2 varchar(50), @x1 int, @x2 real, @time1 datetime
set @str1='good'
set @str2='hello, how are you?'
set @x1=12
set @x2=13.0
set @time1='2009/05/06'

print @str1
print @str2
print @x1
print @x2
print @time1

输出:

good      
hello, how are you?
12
13
May  6 2009 12:00AM

也可以用selet赋值和输出

 

declare @str1 char(10), @str2 varchar(50), @x1 int, @x2 real, @time1 datetime
select @str1='good', @str2='hello, how are you?', @x1=12, @x2=13.0, @time1='2009/05/06'
select @str1 as str1, @str2 as str2, @x1 as x1, @x2 as x2, @time1 as time1

输出结果:

利用布局变量动态查询数据表中的数据信息

declare @x int
set @x=2000
select * from 职工 where 工资>@x

2:全局变量

    全局变量是数据库系统(如SQL SERVER)内部使用的变量,其作用范围并不局限于某个程序,而是任何程序均可调用,全局变量通常用于存储一些数据库系统的

配置设定值和效能统计数据,可以利用全局变量来测试系统的设定值或SQL命令执行后的状态值

   引用全局变量时,使用"@@"开头,局部变量的名称并不能与全局变量的名称相同,否则就会在程序中出错。

  在SQL Server2008中,常用的全局变量及其意义如下:

    @@Identity:返回最近一次插入的identity列(自动编号列)的数值 

    @@Error:返回执行上一条SQL语句返回的错误代码

    @@Connections:返回自最后一次服务器启动以后,所有针对服务器进行连接的数目,注意包括没有连接成功的次数

    @@Dbts:返回当前数据库中timestamp数据类型的当前值

    @@Fetch_Status:返回上次使用游标进行fetch操作所返回的状态

     @@IO_Busy:返回服务器自最近一次启动以来花费在输入和输出上的时间

     @@CPU_Busy:返回服务器自最近一次启动以来CPU的工作时间

     @@Pack_Sent:返回服务器自最近一次启动以来一共向网络上发送数据分组的数目

      @@Rowcount:返回上一条SQL语句所影响到的数据行的数目

      @@version:返回服务器安装日期、版本以及处理器的类型

print @@version
Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (Intel X86) 
	Apr  2 2010 15:53:02 
	Copyright (c) Microsoft Corporation
	Developer Edition on Windows NT 6.1 <X86> (Build 7600: )

print '服务器输入和输出花费时间是:' + cast(@@IO_Busy as varchar(10)) + '秒' 
print '服务器CPU工作时间是:' + cast(@@CPU_Busy as varchar(10)) + '秒'
服务器输入和输出花费时间是:64秒
服务器CPU工作时间是:235秒

3:if--else 语句

 在sql server中:

      if 条件表达式

          命令行或程序块

      else 

          命令行或程序块

  在Oracle中

       if 条件表达式  then

            命令行或程序块

      elseif

            命令行或程序块

      end if

  

     在DB2中

       if 条件表达式  then

            命令行或程序块

      elsif

            命令行或程序块

      end if

 

declare @yz real, @w int  --声明变量
set @w=120
  if @w<=100
     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)) + '元'

输出:

邮件的重量为:120克
邮费是:13元

if 条件表达式

   命令行或者程序块

else if 条件表达式

   命令行或者程序块

 --

else 

    命令行或者程序块

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='111'
           set @msg='用户名和密码正确,成功登录'
         else 
           set @msg='密码不正确,请重新输入'
      end
else if @hyuser='liping'
      begin
         if @hypwd='111'
           set @msg='用户名和密码正确,成功登录'
         else 
           set @msg='密码不正确,请重新输入'
      end
else 
   set @msg='用户名不正确,请重新输入'
  
print @msg

在数据库中的应用

create table hyuser(
   hyid int identity(1, 1) primary key,
   hyname varchar(50) unique,
   hypwd varchar(50)
)

insert into hyuser(hyname, hypwd) values('stu1', '1111')
insert into hyuser(hyname, hypwd) values('stu2', '2222')
insert into hyuser(hyname, hypwd) values('stu3', '3333')

declare @hyname varchar(50), @hypwd varchar(50), @msg varchar(20), @num int, @num1 int
select @hyname='stu1', @hypwd='1111'
select @num=COUNT(*) from hyuser where hyname=@hyname 
if @num>=1 
   begin
     select @num1=COUNT(*) from hyuser where hyname=@hyname and hypwd=@hypwd
     if @num1>=1 set @msg='用户名和密码都正确,成功登录'
     else set @msg='密码不正确,请重新输入'
   end
else set @msg='用户名不正确,请重新输入'
print @msg

也可以用exists关键字来实现:

declare @hyname varchar(50), @hypwd varchar(50), @msg varchar(20), @num int, @num1 int
select @hyname='stu1', @hypwd='1111'
select @num=COUNT(*) from hyuser where hyname=@hyname 
if(exists(select * from hyuser where hyname=@hyname))
   begin
     if(exists(select * from hyuser where hyname=@hyname and hypwd=@hypwd)) 
         set @msg='用户名和密码都正确,成功登录'
     else set @msg='密码不正确,请重新输入'
   end
else set @msg='用户名不正确,请重新输入'
print @msg

4:case语句的使用

  

declare @cj float, @str varchar(60)
set @cj=90
set @str=
case
   when @cj>100 or @cj<0 then '你输入的成绩不对,成绩应在0-100之间' 
   when @cj>=90 or @cj<=100   then '优秀'
   when @cj>=80 or @cj<90    then '优良'
   when @cj>=70 or @cj<80    then '中等'
   when @cj>=60 or @cj<70    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 职工

5:循环结构的语法结构

Sql server 2008:

    while 条件表达式

        begin

               命令行或程序块

        end

Oracle的循环结构:

     while   条件表达式

          begin

               命令行或程序块

          end     

在DB2的循环结构中

       while 条件表达式

             do

                 命令行或程序块

           end while

while语句的应用:

     

declare @x int, @sum int
select  @x=0,@sum=0
while @x <= 120
  begin
     set @sum = @sum + @x
     set @x = @x + 1
  end
print '从1加120之和是:' + cast(@sum as varchar(50))

6:break语句

     while 条件表达式

         begin

                 命令行或者程序块

              if 条件表达式

                 break

               命令行或者程序块

         end

   应用实例:

    

declare @x int, @sum int
select  @x=0,@sum=0
while @x <= 120
  begin
     set @x = @x + 1
     set @sum = @sum + @x
     if @sum > 5000
         break
  end
print 'sum:' + cast(@sum as varchar(50))

7:continue语句

   

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))

8:goto语句

      goto语句用来改变程序执行的流程,使程序跳转到标有标识符的指定的程序行在执行,格式:goto 标识符

     

declare @x int, @sum int
select @x=0, @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))

9:return语句  

return 语句用于结束当前程序的执行,返回到上一个调用它的程序或者其他程序,其语法结构如下
     return 整数值或者变量

return返回语句要指定返回值,如果没有指定返回值,SQL Server数据库系统会根据程序执行的结果返回一个内定值,内定值及其含义值如下表所示:

    返回值

     含义

     返回值

      含义

     0

   程序执行成功

      -3

     死锁

     -1

    找不到对象

      -4

     违反权限规则

     -2

数据类型错误

-5

     语法错误

     -6

用户造成的一般错误

   -10和-11

致命的内部不一致性错误

     -7

资源错误

-12

     表或指针破坏

     -8

  非致命的内部错误

     -13

     数据库破坏

     -9

  已达到系统的极限

     -14

    硬件错误

简单示例:

  

--创建函数,返回值为整数
create function myfunc(@x int) returns int   
as 
    begin
       declare @y int
       set @y = @x * 0.15
       return @y
    end

select *, dbo.myfunc(工资) as 奖金 from 职工

        

【上篇】
【下篇】

抱歉!评论已关闭.