---临时表---
create table #MyStudents
(
sName nvarchar(50),
sAge int
)
insert into #MyStudents
select FName,FAge from MyStudents
select * from #MyStudents
delete FROM #MyStudents
-- where sName='康凯' or sName='李昂' or sName='李辰'
where sName in('康凯' ,'李昂' ,'李辰')
---
create table ##MyStudents
(
sName nvarchar(50),
sAge int
)
insert into ##MyStudents
select FName,FAge from MyStudents
select * from ##MyStudents
------表变量:---
declare @varTbl table(col1 int,col2 varchar(30))
insert into @varTbl values(100,'A')
insert into @varTbl values(101,'B')
select * from @varTbl
---视图---------------------------------------------------
alter view vw_StudentScore
as
select * from Student
select * from vw_StudentScore
update Student set sName='山西关羽' where sName='关羽'
--我们可以把很长很长的查询语句,放在一个视图中,这样我们在要得到长长的查询语句的结果时候,就可以,不在执行这段语句,而是查询一下相应的视图即可.
--视图类似于一个表,这个表和临时表不同,他不会在会话结束后释放掉,每次都能用.
--视图中不存放数据,视图中只存放查询。
--视图的每一列都要有列名。
use School
create view vw9
as
select
sName,
case
when sAge>13 and sAge<16 then '豆蔻年华'
when sAge>=50 then 'oldman'
else '青壮年'
end as 生存状态
from Student
select * from vw9
--drop view vw9
--alter view vw9
--as
--create table test
--(
--A int,
--B int
--)
create view vw11
as
select top 3 *
from Student
order by sAge DESC
SELECT * FROM VW11
SELECT * FROM Student
--出错! TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
--order by...成了游标,不能作为数据源!
CREATE VIEW VW12
AS
SELECT * FROM Student
ORDER BY sAge DESC
---------------------
CREATE VIEW VW12
AS
SELECT * FROM Student
select * from vw12 ORDER BY sAge DESC
-----------------------------------------
CREATE VIEW VW13
AS
SELECT top 100 percent * FROM Student
ORDER BY sAge DESC
select * from vw13
----!!!order by 后,必须有 top才能作为视图的查询语句
--##注意,在子查询和视图中都不能有order by,除非。。。。
---索引视图(*)
create view vw_ix_T1 WITH SCHEMABINDING
as
select autoid,uname,usrId from dbo.T1
create unique clustered index ix_vw_t1 on vw_ix_T1(autoId)
---=-------------------------------------------------
--一、在T—Sql中使用变量----
--(一)局部变量-----------------------------
--1声明变量
declare @name varchar(20)
declare @age int
---2赋值
--(1)方法1
--set @age=18
--set @name='yqq'
--(2)方法2
select @age=27
select @name='ydx'
print @age
print @name
------使用set和select 为变量赋值的区别-----
declare @rcount int
--set @rcount=(select COUNT(*) from Student)--先查询,后赋值
select @rcount=COUNT(*) from Student--即查询有赋值
print @rcount
select top 5 sAge from Student
declare @sAge int
----------------------------------
--SET @sAge=(select sAge from Student)--出错!子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。
select @sAge= sAge from Student--当查询返回多值的时候,是把 最后一个值赋值给变量。
select @sAge
-------------------------------
declare @Fage int ---只定义,没有赋值,因此@Fage为null
set @Fage=@Fage+1
select @Fage--这里输出的就是null
-----------
declare @userName varchar(10)
declare @userAge int
set @userAge=18
set @userName='hb'
--select @userAge,@userName
print @userName+cast(@userAge as varchar(10))
print @userName+cast(@userAge as varchar(10))
--print 输出的是某一种数据类型的数值
-------------------------------------------------
--------(二)全局变量----------
select * from Student
insert into Student (sName,sAge,sSex,sNo)values('韩根',33,'男',23231131)
print @@identity--
print @@version
print @@language
print @@servername
print @@error--如果返回值为0,说明上一条语句没有错误!如果上一条语句出错,返回错误消息序号。
print 'hello gay!'+222
print @@error
print @@error
---IF ELSE 条件语句---
declare @Hage int
set @Hage=50
if @Hage>10
begin
print '长大了!'
end
else
begin
print '还小!!'
end
select * from Score
--计算score表中english平均成绩,如果〉=60,查询正数前三名,否则找出倒数前三名。
select avg(ENGLISH) FROM Score
--使用isnull()函数计算平均值
select avg(isnull(ENGLISH,0))
FROM Score
--练习使用变量计算平均值
declare @count int
select @count=COUNT(*) from Score
declare @sum float
select @sum=SUM(ENGLISH) FROM Score
declare @avg float
set @avg=(@sum/@count)
if @avg>=60
begin
select top 3 * from Score order by english desc
end
else
begin
select top 3 * from Score order by english asc
end