视图
drop view vw9
alter view vw9
create view vwl1
as
select top 3 * --将游标变成数据源
from Student
order by sAge desc
select * from vwl1
select * from Student
--出错!order by。。。成了游标,不能作为数据源!
create view vwl2
as
select * from Student
order by sAge desc
create view vwl2
as
select * from Student
select * from vwl2
order by sAge desc
--order by 后,必须有top才能作为视图的查询语句
注意,在子查询和视图中都不能有order by,除非 top 3
索引视图
在视图上创建唯一的聚集索引
一、在T-SQL中使用变量
1.声明变量
declare @name varchar(20)
daclare @age int
2.赋值
(1)方法一
set @name='yqq'
set @age=18
print @name
ptint @age
(2)方法二
select @name='yqq'
select @age=18
print @name
ptint @age
-----使用set和select为变量赋值的区别
declare @rcount int
--set @rcount=(select count(*) from Student) --先查询,后赋值
select @rcount=count(*) from Student --即查询又赋值
print @rcount
declare @sAge int
select sAge from Student
--set @sAge=(select sAge from Student)--出错!查询返回的值不止一个
set @sAge=sAge from Student
print @sAge
select @sAge=sAge from Student-- 当查询返回多值的时候,是把最后一个值赋值给变量
declare @Fage int --只定义ÿ