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

  • 相关阅读:
    iOS 后台运行
    内存管理
    ios -晋级之路 如何去掉tableView多余行的横线
    ios 晋级之路- block深入
    iOS 晋级之路- 调用相机和相册的功能
    iOS开发 --定位
    学习笔记 ios开发 手势
    排序法系列-快速排序法
    计算文本的高度BoundingRectWithSize:options:context
    NSClassFromString的用法
  • 原文地址:https://www.cnblogs.com/liujunhaodeboke/p/5120216.html
Copyright © 2020-2023  润新知