>简单子查询
用T-SQL语句实现
declare @age int
set @age = stuage from stumarks where stuname = ''李文才''
select * from stumarks where stuage>@age
go
select stuname from stuinfo inner join stumarks
on stuinfo.stuno = stumarks.stuno
where writtenexam = 60
用子查询语句实现
select * from stumarks
where stuage > (select stuage from stumarks where stuname = ''李文才'')-----子查询的返回值不能多余一个
select stuname from stuinfo
where stuno = (select stuno from stumarks where writtenexam = 60)
………………………………………………………………………………………………………………………………………………
> in 和 not in 子查询
select stuname from stuinfo
where stuno in (select stuno from stumarks where writtenexam = 60)
go
select stuname from stuinfo
where stuno in (select stuno from stumarks)
go
select stuname from stuinfo
where stuno not in (select stuno from stumarks)
go
………………………………………………………………………………………………………………………………………………
>exists 和 not exists
if exists (select * from sysdatabases where name = ''studb'')
drop database studb
create database studb
(
………略………
)
if exists (select * from stumarks where writtenexam>80)
begin
print ''本班有人笔试成绩超过80分,每人只加2分,加分后的成绩为:''
updata stumarks set writtenexam = writtenexam + 2
select * from stumarks
end
else
begin
print ''本班无人笔试成绩超过80分,每人加5分,加分后的成绩为:''
updata stumarks set writtenexam = writtenexam + 5
select * from stumarks
end
go
if not exists (select * from stumarks where writtenexam>60 and labexam>60)
begin
print ''本班无人通过考试,试题偏难,每人加3分,加分后的成绩为:''
updata stumarks set writtenexam = writtenexam + 3,labexam = labexam + 3
select * from stumarks
end
else
begin
print