定义局部变量
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编程,有兴趣的可以继续关注我的下次博客!
时间: 2024-10-25 03:25:39