在学习SQL编程前,先给大家分享几个段子吧,咱先乐呵乐呵!
《桃花庵--程序员版》
写字楼里写字间,写字间中程序员;程序人员写程序,又将程序换酒钱;
酒醒只在屏前坐,酒醉还来屏下眠;酒醉酒醒日复日,屏前屏下年复年;
但愿老死电脑间,不愿鞠躬老板前;奔驰宝马贵者趣,公交自行程序员;
别人笑我太疯癫,我笑自己命太贱;但见满街漂亮妹,哪个归得程序员。
程序员的八荣八耻
以动手实践为荣,以只看不练为耻。
以打印日志为荣,以出错不报为耻。
以局部变量为荣,以全局变量为耻。
以单元测试为荣,以手工测试为耻。 以代码重用为荣,以复制粘贴为耻。
以多态应用为荣,以分支判断为耻。
以定义常量为荣,以魔法数字为耻。
以总结思考为荣,以不求甚解为耻。
好了回归正题,进入SQL编程
1.0使用变量:
变量是可以存储数据值的对象,可以使用局部变量向SQL语句传递数据。在T-SQL中执行一批SQL语句时,可以声明许多变量以
便临时使用。声明变量以后,可以在批处理中用一条T-SQL语句设置该变量的值,该批处理中的下一条语句可以从该变量中检索数值
,并给出结果。
T-SQL中的变量分为局部变量和全局变量。局部变量的使用也是先声明,在赋值。而全局变量由系统定义和维护,可以直接使用
但一般不自定义全局变量。
1.1局部变量:
T-SQL中,局部变量的名称必须以标记@作为前缀。
声明局部变量的语句如下:
语法
DECLARE @Exam DataType
其中@Exam为局部变量的名称,DataType为数据类型
例如:
DECLARE @name nvarchar(32)
DECLARE @num int
局部变量的赋值方法有两种:也就是SET , SELECT语句
语法
SET @name ='小强'
或
SELECT @name = '小强'
示例根据学号查找“小强”的信息及“小强”的学号相邻的学生信息
--查找小强的信息
DECLARE @name nvarchar(32) --声明局部变量存储姓名
SET @name = '小强' --使用给SET给变量赋值
SELECT StuNo,StuName,BornDate,Address From Student
Where StudentName = @name
--查找与小强学号相邻的学生信息
DECLARE @StuNo int
--使用SELECT 赋值
SELECT @StuNo = StudentNo From Student
Where StuName= @name
SELECT StuNo,StuName,BornDate,Address From Student
Where (StuNo=@StuNo+1) OR (StuNo=@StuNo-1)
GO
示例中可以看出,局部变量可用于在上下语句中传递数据(比如学号@StuNo)
在TSQ语言中,为局部变量赋值的语句有SET语句和SELECT语句。其中,SET赋值语句一般
赋予给变量指定的数据常量,如本例中的小强;SELECT赋值语句一般用于从表中查询数据,在
赋给变量。需要注意的是,SELECT语句需要确保筛选的记录不多于一条。如果查询的记录多余
一条,则将把最后一条记录的值赋给变量。
SET语句与SELECT语句的区别如下:
SET | SELECT | |
同时对多个变量赋值 | 不支持 | 支持 |
表达式返回多个值时 | 出错 | 将返回的最后一个值赋给变量 |
表达式未返回值时 | 变量被赋值NULL | 变量保持原值 |
下面我用代码为你们展示他俩的不同之处
--先声明多个变量
DECLARE @add=' ' nvarchar(100) ,@name=' ' nvarchar(100) SET @add='北京',@name='张三' --发生语法错误 SELECT @add='北京',@name='张三' --为两个局部变量@add,@name赋值 SET @add = (SELECT Address FROM Student) --发生语法错误 SELECT @add = Address FROM Student --最后一条记录将赋值于@add --查询无结果时,@add被赋值NULL
SET @add =(SELECT Address From Student where 1<0) --查询无结果时,@add保持原值 '北京'
SELECT @add = '北京'
SELECT @add = Address from Student where 1<0
1.1全局变量:
T-SQL中,全局变量的名称必须以标记@@作为前缀。
变量 | 含义 |
@@ERROR | 最后一个T-SQL错误的错误号 |
@@IDENTITY | 最后一次插入的标识值 |
@@LANGUAGE | 当前使用的语言名词 |
@@MAX_CONNECTIONS | 可以创建的,同时可以连接的最大数目 |
@@ROWWCOUNT | 受上一个SQL语句影响的行数 |
@@SERVERNAME | 本地服务器的名称 |
@@SERVICENAME | 该计算机上的SQL服务器名称 |
@@TIMETICKS | 当前计算机上的每刻度的微秒数 |
@@TRANSCOUNT | 当前连接打开的事物数 |
@@VERSION | SQLSERVER的版本信息 |
全局变量的使用
SELECT @@IDLE as w --返回SQL自上次启动后闲置的时间,单位为毫秒
SELECT @@IO_BUSY AS w --返回SQL自上次启动后用于执行输入和输出操作的时间,单位为毫秒
SELECT @@LANGID AS w --返回当前所使用语言的本地语言标识符(ID)。
SELECT @@LANGUAGE AS w --返回当前使用的语言名
SELECT @@LOCK_TIMEOUT as w --当前会话的当前锁超时设置,单位为毫秒。
SELECT @@MAX_CONNECTIONS as w --返回SQL上允许的同时用户连接的最大数。返回的数不必为当前配置的数值
SELECT @@SERVERNAME as w --返回运行SQL服务器名称。
SELECT @@SERVICENAME as w --返回SQL正在其下运行的注册表键名
SELECT @@TIMETICKS as w --返回SQL服务器一刻度的微秒数
SELECT @@TOTAL_READ as w --返回 SQL服务器自启动后读取磁盘的次数。
SELECT @@TOTAL_WRITE as w --返回SQL服务器自启动后写入磁盘的次数。
SELECT @@TRANCOUNT as w --返回当前连接的活动事务数。
SELECT @@VERSION as w --返回SQL服务器安装的日期、版本和处理器类型。
有一个小技巧分析一下就是再给变量赋值的时候使用SET语句要比使用SELECT语句的效率高,因为
SET没有结果集,占用资源比较少。
2.0输出语句:
T-SQL支持输出语句,用于显示处理的数据结果,常用输出语句有两种,即PRINT和SELECT
语法
PRINT 局部变量 或字符串
SELECT 局部变量 AS 自定义列名
这里我重点说一下啊!!!其中使用SELECT 语句输出的数据是查询语句的特殊应用。
废话不多说上代码:
PRINT '服务器的名称:' + @@SERVERNAME PRINT '服务器的版本:' + @@VERSION SELECT @@SERVERNAME AS '服务器的名称' SELECT @@VERSION AS '服务器的版本'
大家可以看到我用PRINT语句输出的结果在消息窗口中以文本方式显示,用SELECT 语句输出的结果
在结果窗口中以表格中显示
使用PRINT语句 时要求+运算符两侧的操作数的数据类型必须一致,需要类型一致我想你应该想到了用
CONVERT()函数转化,是的,没错不光用CONVERT ()我们还要学习另一个转化函数CAST()
语法:
CAST(表达式 AS 数据类型)
CONVERT (数据类型[长度] ,表达式,[样式])
CAST 与 CONVERT 都是用于将某种数据类型的表达式转化为另一种数据类型的表达式。与CAST不同的是
在转化日期时间类型/浮点类型的数据转换为字符串时,CONVERT函数可以通过第三个参数指定转化后的字符样式。
上代码:
declare @date datetime
set @date=getdate()
print '今天是'+convert(nvarch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+zhMELh5AAAAAElFTkSuQmCC" alt="" />
还有俩个函数比较有意思就是floor和ceiling函数
--floor:
select floor(1.92) --向下取整
结果为1
--ceiling
select ceiling(1.01) --向上取整
结果为2
2.1逻辑控制语句
T-SQL语言中,常用的逻辑语句有以下几种
顺序结构控制语句 BEGIN-END语法:
BEGIN
语句或语句块
END
类似于C#中的{}表示语句中的开始和结束。
分支结构控制语句 IF-ELSE语句和CASE-END语句语法:
IF(条件)
BEGIN
语句1
语句2
..........
END
ELSE
.......
循环结构控制语句 WHILE语句语法:
WHILE(条件)
BEGIN
语句或语句块
[break | CONTINUE]
END
使用BREAK语句将跳出循环结束循环,使用后者将跳过CONTINUE后面的语句回到第一次根据条件循环
在SQL 中,只有while一种循环,没有do-while和for循环
并且没有while(true)的写法,可以使用while(1=1)替代。
注意:SQL中比较是否相等,用单等号(=)
while循环示例
--1到100之间所有偶数的和
--01.定义一个变量,保存总和
declare @sum int
set @sum=0
--02.定义一个初始变量
declare @num int
set @num=1
--03.进行while循环
while(@num<=100)
begin
--判定
if(@num%2=0) --代码执行到这里,证明@num是偶数
begin
set @sum+=@num
end
set @num+=1
end
--出了循环,打印总和sum
print @sum
--用循环的方式打印出直角三角形(不是重点,是难点)
第一种
declare @i int
declare @j int
declare @str nvarchar(100)
set @i=1
set @j=1
set @str=''
while(@i<=5)
begin
while(@j<=@i)
begin
set @str+='*'
set @j+=1
end
print @str
set @i+=1
end
第二种
declare @str2 nvarchar(200)
declare @count int
declare @result nvarchar(200)
set @str2='*'
set @count=0
set @result=''
while(@count<5)
begin
set @result+=@str2
print @result
set @count+=1
end
福利共享:
--经典while循环加分题目
--检查学生“oop”课最近一次考试是否有不及格(分及格)的学生。
--如有,每人加2分,高于95分的学生不再加分,直至所有学生这次考试成绩均及格
--***********************************************************************
--01.看一下oop课程最近一次考试分以下人数
--准备一个变量,保存不及格人数,科目编号,最近考试时间 --01.科目编号 select * from result declare @subid int
select @subid=subjectid from subject
where subjectname='oop' --02.最近一次考试时间
declare @mydate datetime
select @mydate=max(examdate) from result
where subjectid=@subid --03.不及格人数
declare @num int
select @num=count(1) from result
where subjectid=@subid
and examdate=@mydate
and studentresult<70 --04.
while(@num>0) --有成绩低于分的学员,
begin
--每个人+2分,但是95分以上不加分
update result set studentresult+=2
where studentresult<95
and subjectid=@subid
and examdate=@mydate select @num=count(1) from result
where subjectid=@subid
and examdate=@mydate
and studentresult<70
print @num end select * from result
order by subjectid,examdate
2.2case多分支语句语法:
CASE
WHEN 条件1 THEN 结果1
WHEN 条件2 THEN 结果2
ELSE 其他结果
END
-- .Case End 经典练习
--采用美国ABCDE五级打分制显示学生oop课最近一次考试成绩(姓名等级)
--A级: 90分以上
--B级:80-分
--C级: 70-分
--D级:60-分
--E级:60分以下 declare @subid int
declare @maxdate datetime select @subid=subjectid
from subject
where subjectname='oop' select @maxdate=max(examdate)
from result
where subjectid=@subid select studentname,等级=
case
when studentresult>=90 then 'A'
when studentresult>=80 then 'B'
when studentresult>=70 then 'C'
when studentresult>=60 then 'D'
else 'E'
end
from student,result
where student.StudentNo=result.StudentNo
and subjectid=@subid
and examdate=@maxdate
好了,今天的总结就到这里了,希望可以帮助到看完整篇的人
虽然很累但是心里很开心!热爱代码热爱生命!