日期:2014-05-17 浏览次数:20460 次
USE test
GO
--use master
-- go
--if exists(select * from sysdatabases where user='tempdatabase')
--drop database tempdatabase
--go
--create database tempdatabase
--go
--use tempdatabase
if object_id('records')IS NOT NULL
DROP TABLE records
create table records
(
studentname varchar(50), -- 学生姓名
subject char(4), -- 科目
score int -- 成绩
)
delete records
insert into records values('p1','c1',44)
insert into records values('p2','c1',87)
insert into records values('p3','c1',65)
insert into records values('p4','c1',29)
insert into records values('p1','c2',13)
insert into records values('p2','c2',32)
insert into records values('p3','c2',44)
insert into records values('p4','c2',34)
insert into records values('p1','c3',35)
insert into records values('p2','c3',56)
insert into records values('p3','c3',43)
insert into records values('p4','c3',23)
Go
--------------- 1.
IF object_id('v_ChengJiDan')IS NOT NULL
DROP VIEW v_ChengJiDan
Go
CREATE VIEW v_ChengJiDan
AS
SELECT
studentname
,subject
,score
FROM records AS a
WHERE NOT EXISTS(SELECT 1 FROM records AS x
WHERE x.subject=a.subject
AND x.score>a.score
)
Go
SELECT * FROM v_ChengJiDan
/*
studentname subject score
------------ ------- ------
p2 c1 87
p3 c2 44