T-SQL编程高级查询
2013-06-27 10:16
232 查看
select *from student
select * from grade
--多表查询
select grade.*,student.stuName
from student,grade
where student.stuId=grade.stuId
--连接查询
select * from student a
inner join grade b
on b.stuId=a.stuId
where stuName='奥特曼坑爹'
--左外连,左连接查询--
--以左表为基准,用左边的数据找右表中对应的数据
--如果有对应的数据,左右两边的数据连接成一条数据显示出来
--否则与一条null数据连接成一条数据
select * from student a
left join grade b
on a.stuId=b.stuId
--右连接
select * from grade b
right join student a
on a.stuId=b.stuId
--交叉连接
select * from grade a
cross join student b
--联合查询***********************************************
select stuname from student where stuid in
(
select stuid from grade where result in
(select MAX(result) from grade)
)
union
select stuname from student where stuid in
(
select stuid from grade where result in
(select min(result) from grade)
)
--union将两个表中的数据联合成一个结果集体,相同的数据值显示一次
--union all将两个表中的数据相加后显示出来
select stuname,stuno from student where sex='女'
union
select stuname,stuno from student where sex='男'
--T-SQL编程********************************
--局部变量的定义
declare @r int--=10--SQL2008中可以定义初值
--赋值
set @r=10
print'r的值为:'+convert(varchar,@r)--打印convert 转换函数,
--print @r
--第二种赋值方式
select @r=20
print @r
--把最高分查询出来并打印
select @r =MAX(result) from grade
print '最高分:'
print @r
--变量可以嵌套在sql语句中使用
--最高分的信息
select * from grade where result=@r
--服务器上的全局变量******************
print @@servername --服务器名称
print @@error --上一次出错的错误代号
--上一次插入sql语句自增长值
print @@identity
--流程语句**********************************
--if,case
--循环,while
--跳转,break,contuine
--查找出班级的平均分,如果平局分在60以下,班级成绩较差
--60<平均分<80,班级成绩良好;
--80以上成绩优秀
declare @i int
--set @i=60
select @i=AVG(result) from grade
--if语句******
if(@i<60)
begin
print '班级成绩较差'
end
if(@i<80 and @i>60)
begin
print '班级成绩良好'
end
if(@i>80)
begin
print '班级成绩优秀'
end
--case语句****
--1.case的第一种使用方法
--case
--when 条件一 then 值 1
--when 条件二 then 值 2
-- ...
--else 值 n
--end
--2.第二种使用方法
--case 变量
--when 值 1 then 返回值 1
--when 值 2 then 返回值 2
--...
--end
declare @a int
declare @msg varchar(20)
set @a=60
set @msg= case
--条件--
when @a<60
then '成绩差' --满足条件时返回值而不是做操作
when @a<=80
then '成绩良好'
else
'成绩优秀'
end
print @msg
declare @x int
declare @m varchar(20)
set @x=3
set @m=
case @x
when 1 then '星期一'
when 2 then '星期二'
when 3 then '星期三'
when 4 then '星期四'
when 5 then '星期五'
when 6 then '星期六'
else '星期天'
end
print @m
select *,成绩=case
when result<60
then '成绩差' --满足条件时返回值而不是做操作
when result<=80
then '成绩良好'
else
'成绩优秀'
end
from grade
--***********************************************
declare @s int
set @s=0
while(@s<100)
begin
set @s=@s+1
print @s
end
--添加100个学生,循环添加
--性名分别为dotnet1,dotnet2,....,dotnet100
--学号1204101,...,1204200
--学号奇数时性别为男,否则性别为女
--getdate()给生日
--备注都给'无'
select * from student
declare @name int
declare @sex char(2)
set @name=0
while(@name<100)
begin
if (@name%2=0)
begin
set
@sex='女'
end
else
begin
set
@sex='男'
end
set @name=@name+1
insert into student(stuname,stuNo,birthday,remark,sex)
values('dotnet'+convert(varchar,@name),convert(varchar,1204100+@name),getdate
(),'无',@sex)
end
delete from student where sex='男'
--***************异常捕获***********************
--try{} catch
--begin try...end try begin catch...end catch
declare @w int
begin try
set @w=CONVERT(int,'abc')
end try
begin catch
print '错误代号为:'+convert(varchar,@@error)
end catch
--**
waitfor delay '00:00:05'--延时
print '奥特曼'
waitfor time '14:37:00'--定时
print '下课了'
--*********************字符串函数**************************************
declare @str varchar(200)
set @str='abcEFG'
print len(@str)--长度不计尾部的空格
print upper(@str)
print lower(@str)
print charindex('c',@str)--起始为1的序号来计算
print ltrim(@str)--去掉左边的空格
print rtrim(@str)--去掉右边的空格
--abc替换为xyz
print replace(@str,'abc','xyz')
--substring 截取E以后的内容
print substring(@str,5,6)
--**********************时间函数
declare @dt datetime
print getdate()
set @dt=GETDATE()
print dateadd(year,1,@dt)
print datename(year,@dt)--返回的是这个时间的某一部分的值,以字符
print datepart(year,@dt)--返回的是这个时间的某一分,以整数返回
select * from grade
--多表查询
select grade.*,student.stuName
from student,grade
where student.stuId=grade.stuId
--连接查询
select * from student a
inner join grade b
on b.stuId=a.stuId
where stuName='奥特曼坑爹'
--左外连,左连接查询--
--以左表为基准,用左边的数据找右表中对应的数据
--如果有对应的数据,左右两边的数据连接成一条数据显示出来
--否则与一条null数据连接成一条数据
select * from student a
left join grade b
on a.stuId=b.stuId
--右连接
select * from grade b
right join student a
on a.stuId=b.stuId
--交叉连接
select * from grade a
cross join student b
--联合查询***********************************************
select stuname from student where stuid in
(
select stuid from grade where result in
(select MAX(result) from grade)
)
union
select stuname from student where stuid in
(
select stuid from grade where result in
(select min(result) from grade)
)
--union将两个表中的数据联合成一个结果集体,相同的数据值显示一次
--union all将两个表中的数据相加后显示出来
select stuname,stuno from student where sex='女'
union
select stuname,stuno from student where sex='男'
--T-SQL编程********************************
--局部变量的定义
declare @r int--=10--SQL2008中可以定义初值
--赋值
set @r=10
print'r的值为:'+convert(varchar,@r)--打印convert 转换函数,
--print @r
--第二种赋值方式
select @r=20
print @r
--把最高分查询出来并打印
select @r =MAX(result) from grade
print '最高分:'
print @r
--变量可以嵌套在sql语句中使用
--最高分的信息
select * from grade where result=@r
--服务器上的全局变量******************
print @@servername --服务器名称
print @@error --上一次出错的错误代号
--上一次插入sql语句自增长值
print @@identity
--流程语句**********************************
--if,case
--循环,while
--跳转,break,contuine
--查找出班级的平均分,如果平局分在60以下,班级成绩较差
--60<平均分<80,班级成绩良好;
--80以上成绩优秀
declare @i int
--set @i=60
select @i=AVG(result) from grade
--if语句******
if(@i<60)
begin
print '班级成绩较差'
end
if(@i<80 and @i>60)
begin
print '班级成绩良好'
end
if(@i>80)
begin
print '班级成绩优秀'
end
--case语句****
--1.case的第一种使用方法
--case
--when 条件一 then 值 1
--when 条件二 then 值 2
-- ...
--else 值 n
--end
--2.第二种使用方法
--case 变量
--when 值 1 then 返回值 1
--when 值 2 then 返回值 2
--...
--end
declare @a int
declare @msg varchar(20)
set @a=60
set @msg= case
--条件--
when @a<60
then '成绩差' --满足条件时返回值而不是做操作
when @a<=80
then '成绩良好'
else
'成绩优秀'
end
print @msg
declare @x int
declare @m varchar(20)
set @x=3
set @m=
case @x
when 1 then '星期一'
when 2 then '星期二'
when 3 then '星期三'
when 4 then '星期四'
when 5 then '星期五'
when 6 then '星期六'
else '星期天'
end
print @m
select *,成绩=case
when result<60
then '成绩差' --满足条件时返回值而不是做操作
when result<=80
then '成绩良好'
else
'成绩优秀'
end
from grade
--***********************************************
declare @s int
set @s=0
while(@s<100)
begin
set @s=@s+1
print @s
end
--添加100个学生,循环添加
--性名分别为dotnet1,dotnet2,....,dotnet100
--学号1204101,...,1204200
--学号奇数时性别为男,否则性别为女
--getdate()给生日
--备注都给'无'
select * from student
declare @name int
declare @sex char(2)
set @name=0
while(@name<100)
begin
if (@name%2=0)
begin
set
@sex='女'
end
else
begin
set
@sex='男'
end
set @name=@name+1
insert into student(stuname,stuNo,birthday,remark,sex)
values('dotnet'+convert(varchar,@name),convert(varchar,1204100+@name),getdate
(),'无',@sex)
end
delete from student where sex='男'
--***************异常捕获***********************
--try{} catch
--begin try...end try begin catch...end catch
declare @w int
begin try
set @w=CONVERT(int,'abc')
end try
begin catch
print '错误代号为:'+convert(varchar,@@error)
end catch
--**
waitfor delay '00:00:05'--延时
print '奥特曼'
waitfor time '14:37:00'--定时
print '下课了'
--*********************字符串函数**************************************
declare @str varchar(200)
set @str='abcEFG'
print len(@str)--长度不计尾部的空格
print upper(@str)
print lower(@str)
print charindex('c',@str)--起始为1的序号来计算
print ltrim(@str)--去掉左边的空格
print rtrim(@str)--去掉右边的空格
--abc替换为xyz
print replace(@str,'abc','xyz')
--substring 截取E以后的内容
print substring(@str,5,6)
--**********************时间函数
declare @dt datetime
print getdate()
set @dt=GETDATE()
print dateadd(year,1,@dt)
print datename(year,@dt)--返回的是这个时间的某一部分的值,以字符
print datepart(year,@dt)--返回的是这个时间的某一分,以整数返回
相关文章推荐
- SQL编程之高级查询(子查询)以及注意事项
- T-SQL编程与高级查询
- SQL SERVER 3 T-SQL编程与高级查询
- T-SQL编程与高级查询
- T-SQL编程与高级查询
- SQL SERVER T-SQL编程与高级查询
- SQL编程之高级查询(子查询)以及注意事项
- T-SQL 编程与高级查询
- ORACLE SQL 高级编程--查询转换
- T_SQL 语句编程与高级查询
- SQL编程之高级查询(子查询)以及注意事项
- 理解T-SQL:高级查询
- SQL中的高级查询
- SQL Server SQL高级查询语句小结(转)
- [转] transact---sql高级查询(下)
- SQL高级查询——50句查询(含答案)
- SQL Server SQL高级查询语句小结
- SQL Server T-SQL高级查询
- SQL高级查询
- MongoDB 教程三: 高级查询 (SQL到MongoDB映射表)