• 数据库declare 运算符和流程控制(if,while,break,continue)


    use new

    go

     

    --定义变量,@开头

    declare @hello  as varchar(20)

    --赋值

    set @hello ='销售部'

    --可以将变量作为选择条件的参数值来用

    --select *from bumen where name =@hello

    --当放到select 和from 中间,作为赋值语句,select不执行查询功能

    --select @hello=name from bumen  where code=1

    print @hello –打印出‘销售部’

    $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    --全局变量,系统变量

    --返回sqlserver 自上次启动以来的连接次数,包括成功和失败。

    print @@connections

    --返回执行上一个sql语句时的错误,返回0代表没错

    -select from bumen (例)

    print @@error

    --返回系统当前使用的语言

     print  @@language

     --返回值表示列表中受上一条语句影响的行数

    print @@rowcount

    --返回安装的SQL的版本

    print @@version

    update bumen set phone ='22342432'

    select '这一语句影响'+CAST (@@ROWCOUNT as varchar(20))+'行'

    --转变数据类型为字符串型。

     

    declare @text varchar(20)

    set @text ='123''123'

    print @text    --显示:123'123              --两个   ‘’  打印出一个!

     

    ---运算符:   +-*/%,  

     

    declare @jia   int -–创建int型变量@jia

    set @jia =1+1 -–赋值

    set @jia=10%3

    print @jia  --打印,输出!  --declare ,set ,print 都要执行,这是完整流程

     

    ---比较运算符  >,<,<=,>=,!=,!>,!<,<>(表示不等于)

    select *from bumen where code <>3         --code 不是 3的

     

    --逻辑运算符   and,or,all,any ,between ,in ,like ,not,some ,exists

    --表示满足子查询所有列元素数据,配合比较运算符使用

    select *from xuesheng where name = any (select name from xuesheng where code<=3)

    --any  表示满足任意条件就可以,some 和any 一样!

    select *from xuesheng where name > any (select name from xuesheng where code<=3)

    --exists表示存在某个条件.

    --下面语句表示:在分数表中有分数的人在学生表里的信息.not exists 就表示不存在的信息

    insert  into xuesheng values (23,'sd','sd','e',2,3,2)

    select *from xuesheng where not exists (select *from fenshu where xuesheng .code =fenshu .code )

    --not 可以结合in ,like,exists 使用,

    --查询二班数学分大于所有一班数学分的信息

     

    select *from fenshu where shufen> all(

    select shufen from fenshu where code in

    (select code from xuesheng where banji='1班')

    )    --第一种法

     

    select *from fenshu where shufen >(

    select MAX (shufen )from fenshu ,xuesheng

     where fenshu .code =xuesheng .code and banji='1班'

    group by banji )    ----第二种法

     

     

    select banji, MAX(shufen) from fenshu

    join xuesheng on xuesheng.code=fenshu.code

    group by banji   ---  --显示按照班级分组,每班的最高分

     

    select xuesheng.code ,banji ,fenshu.shufen from xuesheng

    join fenshu on xuesheng.code =fenshu.code

     order by banji --学生学号,班级,数学分数 按班级排序

     

     

     

     

     

     

     

    ------------一元运算符

    正号+,负号-(负号使用,一般加括号)

    --------优先级

      1.*%/

      2.正负号,加减号

      3.>,<,>=,<>,!=,!<,!>

      4.not

      5.and or between

      6.all any  some   in  like exists

      7. =

     

    begin --开始(开始到结束是代表一个整体,方便(区分),就是大括号的概念)

    select *from xuesheng

    end --结束

     

    declare @bianliang  int -- 定义一个int 型变量

    set @bianliang =7    --赋值变量要用set

    if @bianliang >5

      begin 

         print '你好'

         print '谢谢'

      end   --begin……end 好像是C#中,if后面的{}

    else

         print '好你'

     

    --数学最高分学生的信息

    --第一

    select *from xuesheng where code=(select top 1 code from fenshu order by shufen desc )

     

    --第二,通过临时变量临时存储,正向思维解决问题

    declare @math  decimal(18,2)

    select @math =MAX(shufen )from fenshu

    declare @sd  int

    select @sd =code from fenshu where shufen =@math

    select *from xuesheng where code=@sd --要进行下面的执行语句,要先注释这句

    declare @sex varchar(20)

    select @sex=sex from xuesheng where code=@sd 

    if @sex ='男'

      print '帅哥'

    else

      print '美女'

     

     

     

    ----while 循环

    declare @math int

    set @math=80

    while @math <95

    begin

     print '很优秀'  +cast(@math as varchar(10))  --把int型转化成字符串型

     set @math=@math+1

    -- break  --while结束语句!

      if @math =93

      break

      if @math >=85 and @math <=90--if语句后面如果不是begin…end。默认执行一句!

      --  print '恭喜发财'

        begin

        print '福如东海长流水'

        continue--这时候continue 执行的作用是返回while 判断条件!

        --如果不注释‘恭喜发财’,begin……end中的continue是跳出if进行while循环,不进行继续打印‘hhh’

        end

      print 'hhh'

    end   

  • 相关阅读:
    单调队列和单调栈
    二叉搜索树(BST)
    高斯消元和高斯约旦消元 Gauss(-Jordan) Elimination
    扩展欧几里德算法
    基数排序
    智力题研究
    快速排序和快速选择
    快读模板
    C#知识点
    C#字段属性设置
  • 原文地址:https://www.cnblogs.com/huaze/p/4080246.html
Copyright © 2020-2023  润新知