zoukankan      html  css  js  c++  java
  • 数据库之SQL编程

    定义局部变量

    declare @num int
    途径一:set @num=5
    途径二:select @num=5

    set 和select赋值方式的区别

    唯一区别,如果从数据库表中获取数据,只能用 select

    declare @name nvarchar(32)
    select @name =stuname
    from student
    where studentno=5

    类型转换

    declare @num int
    set @num=123
    print 'num的值是'+cast (@num as nvarchar(32))
    print 'num的值是'+convert(nvarchar(32)@num)
    --日期转成字符串
    declare @mydate datetime
    set @mydate =getdate()
    print '当前时间为:'+convert (nvarchar(32),@mydate,121)

    使用SQL判定

    --定义一个变量 保存avg
    declare @avg int
    --定义一个变量 保存科目编号
    declare @subid int
    select  subid=subjectid from subject
    where subjectname='oop'
    select @avg=avg (studentresult)
    from result
    where examdate>='2013-08-09' and examdate <'2013-08-10'
    and subjectid =@subid
    print '平均分是:'+convert (nvarchar(32),@avg)
    --判定
    if(@avg>=70)
    begin
    print '优秀'
    --打印出前几名的成绩
    select top 3  studentno,studentresult
    from result
    where examdate>='2013-08-09' and examdate<'2013-08-10'
    and subjectid =@subid
    order by studentresult desc
    end
    
    else
    begin
    print''
    --打印后几名成绩
    select top 3 studentno,studentresult
    from result
    where examdate>='2013-08-09' and examdate<'2013-08-10'
    and subjectid=@subid
    order by studentresult asc
    end
      

    以上就是我刚刚学习完的使用SQL编程,有兴趣的可以继续关注我的下次博客!

  • 相关阅读:
    Spring事务
    org.apache.catalina.webresources.Cache.getResource Unable to add the resource
    CentOS7下zip解压和unzip压缩文件
    通过Maven插件发布JaveEE项目到tomcat下
    MYSQL5.7版本sql_mode=only_full_group_by问题
    CentOS下安装Tomcat
    MYSQL57密码策略修改
    CentOS下安装mysql5.7和mysql8.x
    Linux下使用systemctl命令
    076-PHP数组修改元素值
  • 原文地址:https://www.cnblogs.com/liujunhaodeboke/p/5120216.html
Copyright © 2011-2022 走看看