doubleyong
管理员
管理员
  • 最后登录2025-12-02
  • 发帖数1198
  • 最爱沙发
  • 喜欢达人
  • 原创写手
  • 社区居民
  • 忠实会员
阅读:6745回复:0

sql server 编程基础知识

楼主#
更多 发布于:2018-02-26 12:50
1. 变量
select ISNULL(NULL,0)
-- 局部变量
declare @num int  -- 变量的申明
set @num = 10
print 'num的值为' + Convert(varchar,@num)

declare @name varchar(50)
declare @num1 int
select @name=stuName from student where sex='男'
print @name

declare @num2 int
set @num2=5
print convert(varchar(10),@num2+5)

-- 全局变量(两个@@符号,由系统提供,不能自定义)
select @@Version
print @@Version

更多全局变量,参考:https://www.cnblogs.com/Akeke/p/6694232.html

2. 选择结构
declare @score float
set @score = 90
if(@score>=90)
  begin
    print('A')
  end
  
 else if(@score>=80 and @score<90)
 begin
    print('B')
 end
 
  else if(@score>=70 and @score<80)
 begin
    print('C')
 end
 
   else if(@score>=60 and @score<70)
 begin
    print('D')
 end
 else
 begin
 
   print('E')
end


3. while 循环
  语法:
   while (条件)
   begin
   end
 示例:
-- 检查学生“ps”课最近一次考试是否有不及格(60分及格)的学生
-- 1. 去科目表中查询科目的id
declare @subjectNo int
declare @examDate datetime
declare @num int

select @subjectNo = SubjectId
from Subject
where SubjectName='ps'
print '科目的id'+cast(@subjectNo as varchar)

select @examDate=MAX(ExamDate)
from Result
where SubjectNo = @subjectNo

print '最近的时间'+cast(@examDate as varchar)

/*
select @num=COUNT(1)
from Result
where SubjectNo=@subjectNo and ExamDate=@examDate
     and StudentResult < 60
    
print '最初的未及格的人数'+cast(@num as varchar)    
 -- 如有,每人加2分,高于95分的学生不再加分,
--直至所有学生这次考试成绩均及格
*/
while(exists(
select *
  from Result
  where SubjectNo=@subjectNo and ExamDate=@examDate
     and StudentResult < 60
))
begin
 --每人加2分,高于95分的学生不再加分,
   update Result set StudentResult=StudentResult+2
   where SubjectNo=@subjectNo and ExamDate=@examDate
      and StudentResult<=95
end
print '所有人都及格了'

-- 4. case when
select *,(
  case
     when StudentResult>=90 then 'A'
     when StudentResult>=80 then 'B'
     when StudentResult>=70 then 'C'
     when StudentResult>=60 then 'D'
     else 'E'
  end
) as '等级' from Result

5. 函数使用

字符函数
a. charindex(寻找的字符,搜索列[,起始位置]);
常用来在一段字符中搜索字符或字符串,返回起始位置,(注:起始位置从1开始,如果没有找到返回0)
e.g select * from Student where charindex('金',StudentName)>0
b. len(字符串|列名):返回字符串长度
select len('hello world'); 返回11
c. upper(字符串|列名):把传递的值转换成大写
select upper('hello world'); HELLO WORLD
d. ltrim(字符串|列名):去字符串左边的空格
rtrim(字符串|列名):去字符串右边的空格
e.g:select ltrim('   hello world   ');
select rtrim('  hello world    ');
e. right(字符串,长度):
从字符串右边返回指定数目的字符
left(字符串,长度)
从字符串左边返回指定数目的字符
f. replace(待搜索字符,待查询字符,替换用的字符)
替换一个字符串中的字符
e.g select replace('hello world','hello','hi');结果hi world
g. stuff(字符串,开始位置,长度,插的新字符)
删除一个指定长度,并且在该位置插入一个新的字符串
日期函数
i. getdate():返回当前的系统日期
ii.dateadd(日期部分,增加值,指定的日期)
向指定日期,增加一段时间,然后返回
iii.datediff(日期部分,日期1,日期2);
两个日期指定部分的间隔。日期2-日期1
iiii. datename(日期部分,指定日期)
返回指定日期部分的字符串形式
iiiii.datepart(日期部分,指定日期)
返回指定日期部分的整数


数学函数:
a. RAND(),返回从0到1之间的随机float值
b. ceiling(值):向上取整,返回整数
floor(值):向下取整,返回整数
c. round(值,精度):将数值表达式进行四舍五入,精度,表示四舍五入在哪个小数字后的位数进行
d. sign(值):返回0,-1,1三个值,
正整数返回1,负整数返回-1,0返回0


系统函数:
a. convert(转换成的类型,值):
用来转换数据类型
 b. cast(值 AS 转换成的类型)
      CAST('12' AS int)
知识需要管理,知识需要分享
游客


返回顶部

公众号

公众号