zoukankan      html  css  js  c++  java
  • 输出,变量的使用,子查询,逻辑语句,循环,case..when..then..end多分支语句,Exists(判断存在)

    --------------输出----------------
    print 'hello world'--以文本形式输出
    select 'hello world'--以网格形式输出,也可以设置成以文本形式输出
    print 'abc'+'cde'
    print 3+5
    print 'ab'+5--出错,'ab'不能转换为int
    print 'ab'+convert(varchar,5)--输出ab5
    print '2'+5--输出7,因为'2'能自动转换为整型数据2

    ----------------局部变量-------------------
    --声明局部变量
    declare @age int
    ---set赋值(set一次只能给一个局部变量赋值)
    set @age=22
    set @age=@age+5
    print @age

    --select赋值(select一次可以给多个局部变量赋值)
    declare @stuAge int
    declare @stuName nvarchar(20)
    select @stuName=stuName,@stuAge=stuAge from stuInfo where stuNo='s25302'
    print '姓名是:'+@stuName+' 年龄是:'+convert(varchar,@stuAge)
    --注意:在使用select赋值时,查出来的数据行最好是一行,如果查出来多行,会以最后一行的值来进行赋值


    ----------------全局变量------------------
    print @@version--版本信息
    print @@servername--本地服务器名称
    insert into stuInfo values('张三','s25305','男',23,'汕头')
    print @@error --最后一个T-sql语句的错误号(如果最后一个T-sql语句执行失败,@@error的值会大于0,执行成功,@@error的值会等于0)
    print @@identity--获取最后一个插入行的标识列的值
    update stuInfo set stuAge=32 where stuAge=22
    print @@rowcount--受上一个sql语句影响的行数

    ---------------IF-ELSE分支结构---------------------
    use NetBarDB
    declare @pcid int--计算机号
    set @pcid=3
    declare @pcuse int --计算机状态
    select @pcuse=PCUse from PCInfo where PCId=@pcid
    if(@pcuse>0)
    begin
        print convert(varchar,@pcid)+'号是使用状态!'
    end
    else
    begin
        print convert(varchar,@pcid)+'号是空闲状态!'
    end


    ------------while循环语句--------------
    --完成:网吧回馈业务
    use NetBarDB
    declare @count int--存储余额小于20的用户数
    update cardInfo set CardBalance=CardBalance+50 where DATEDIFF(DAY,TransactTime,GETDATE())>=365
    update cardInfo set CardBalance=CardBalance+10 where DATEDIFF(DAY,TransactTime,GETDATE())<365
    while(1=1)
    begin
        select @count=COUNT(*) from cardInfo where CardBalance<20--查出余额不足20元的行数
        if(@count>0)
        begin
            update cardInfo set CardBalance=CardBalance+1
        end
        else
        begin
            break
        end
    end
    go


    ---------case..when..then..end多分支语句-----------
    --完成:计算机状态问题
    --方法一:union
    select *,'空闲' as 状态 from PCInfo where PCUse=0
    union
    select *,'使用' as 状态 from PCInfo where PCUse=1
    --方法二:case..when..then..end
    select *,
    状态=case
    when PCUse=0 then '空闲'
    when PCUse=1 then '使用'
    else '错误状态'
    end
    from PCInfo


    --------------子查询------------------
    --完成:年龄比'李斯文'大的学员信息
    --方法一:普通T-SQL
    use stuDB
    declare @age int
    select @age=stuAge from stuInfo where stuName='李斯文'  --先拿到'李斯文'的年龄
    select * from stuInfo where stuAge>@age  --再以'李斯文'的年龄作为筛选条件
    --方法二:子查询
    select * from stuInfo where stuAge>
    (select stuAge from stuInfo where stuName='李斯文')
    --特别注意:子查询与<、>、<=、>=...等关系运算符一起使用时,一定要确保子查询返回的值不多于一个,否则报错

    --完成:笔试成绩刚好60分的学员信息
    --方法一:表连接
    select stuName from stuInfo join stuMarks
    on stuInfo.stuNo=stuMarks.stuNo
    where stuMarks.writtenExam=60
    --方法二:子查询
    select stuName from stuInfo where stuNo in
    (select stuNo from stuMarks where writtenExam=60)

    --完成:查询有参加考试的学员名单
    select stuName from stuInfo where stuNo in
    (select stuNo from stuMarks)
    --上面的sql语句相当于: select stuName from stuInfo where stuNo in ('s25303','s25302','s25301')

    --完成:查询没参加考试的学员名单
    select stuName from stuInfo where stuNo not in
    (select stuNo from stuMarks)


    --------------Exists的使用-----------------------
    --语法: Exists(子查询)
    --返回值:当子查询能查到数据,返回true,如果子查询查不到数据,返回false
    use stuDB
    if exists(select * from stuMarks where writtenExam>80)--判断是否有笔试超过80分的
    begin
        update stuMarks set writtenExam=writtenExam+2
    end
    else
    begin
        update stuMarks set writtenExam=writtenExam+5
    end

    --------------not Exists的使用-----------------------
    --语法: not Exists(子查询)
    --返回值:当子查询能查到数据,返回false,如果子查询查不到数据,返回true
    use stuDB
    if not exists(select * from stuMarks where writtenExam>60 and LabExam>60)
    begin
        update stuMarks set writtenExam=writtenExam+3,LabExam=LabExam+3
    end
    else
    begin
        update stuMarks set writtenExam=writtenExam+1,LabExam=LabExam+1
    end
    go

  • 相关阅读:
    将 SharePoint 2010 网站集升级到 2013 (含沙盒方案)
    几款网络云存储服务的使用对比
    技术发展飞快,一日十年
    项目背景介绍
    初次接触,简单的了解需求
    用色彩区分 SharePoint 2010 Calendar 的日历项
    嘿,我这里有一个 Survey!
    博客页面在 IE 浏览器中样式混乱了(已经更换了样式)
    关于 Graphviz
    搭建使用 RTX51Tiny 的 C51 Keil 项目环境
  • 原文地址:https://www.cnblogs.com/danmao/p/3870799.html
Copyright © 2011-2022 走看看