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编程,有兴趣的可以继续关注我的下次博客!

  • 相关阅读:
    Http请求头和相应头分析
    linux扩充容量经典篇
    Redis持久化以及其原理
    redis简单应用
    Python Requests库使用2:请求方法
    加快访问GitHub的速度
    GET和POST两种基本请求方法的区别
    Python Requests库介绍
    Python urllib、urllib2、urllib3用法及区别
    Django中操作cookie和session
  • 原文地址:https://www.cnblogs.com/liujunhaodeboke/p/5120216.html
Copyright © 2011-2022 走看看