设为首页 加入收藏

TOP

史上最全的MSSQL复习笔记(十)
2015-11-21 02:09:59 来源: 作者: 【 】 浏览:2
Tags:史上 最全 MSSQL 复习 笔记
e @classid int ---班级ID set @classid=(select classid from grade where classname=@classname) --通过参数班级名称获取对应的班级ID select * from Student where Sex=@sex and ClassId=@classid set @cnt= (select COUNT(*) from Student where Sex=@sex and ClassId=@classid) --获取指定班级和性别的总人数 set @totalnum=(select COUNT(*) from Student) ----获取总人数 go --调用存储过程,获取指定性别的学员人数及总人数 declare @num int,@tnum int exec usp_getCountBySexandClassName @cnt=@num output ,@totalnum=@tnum output , @className='八期班' print @num print @tnum print '做完了' ---获取指定班级的人数 if exists(select * from sysobjects where name='usp_getCount') drop proc usp_getCount go create procedure usp_getCount @className nvarchar(50)='八期班' as declare @classid int=(select classid from grade where classname=@className) declare @cnt int set @cnt =(select COUNT(*) from Student where ClassId=@classid) --return 只能返回int整数值 --return '总人数是'+cast(@cnt as varchar(2)) return @cnt go --调用存储过程,接收存储过程的返回值 declare @count int --set @count=(exec usp_getCount) exec @count=usp_getCount '八期班' print @count if exists(select * from sysobjects where name='usp_getClassList') drop proc usp_getClassList go create procedure usp_getClassList as select classid,classname from grade go

?

?
21.分页存储过程

if exists(select * from sysobjects where name='usp_getPageData')
 drop proc usp_getPageData 
go 
create procedure usp_getPageData
@totalPage int output,--总页数
@pageIndex int =1 ,--当前页码,默认是第一页
@pageCount int =5 --每一页显示的记录数
as
select * from (select ROW_NUMBER() over(order by studentno) id,* from Student) temp where temp.id>(@pageindex-1)*@pagecount and temp.id<=(@pageindex*@pagecount)
set @totalPage=CEILING((select COUNT(*) from Student)*1.0/@pageCount)
go

?

?
?
22.索引
--创建局部临时表
create table #newGrade
(
 classid int ,
 classname nvarchar(50) 
)
---局部临时表只有在当前创建它的会话中使用,离开这个会话临时表就失效.如果关闭创建它的会话,那么临时表就会消失
insert into #newGrade select * from  grade 
select * from #newGrade
select * into #newnewnew from grade
select * into newGrade from #newgrade

--创建全局临时表:只要不关闭当前会话,全局临时表都可以使用,但是关闭当前会话,全局临时表也会消失
create table ##newGrade
(
 classid int ,
 classname nvarchar(50) 
)
drop table ##newGrade
select * into ##newGrade from grade
select * from ##newGrade

--创建表变量
declare @tb table(cid int,cname nvarchar(50))
insert into @tb select * from grade
select * from @tb

?

?
select * from sysindexes
?
--create ?index IX_Student_studentName
--on 表名(字段名)
?
--clustered index:聚集索引 ?nonclustered index--非聚集索引
if exists(select * from sysindexes where name='IX_Student_studentName')
?drop index student.IX_Student_studentName
go?
create clustered index IX_Student_studentName
on student(studentname)
?
--如果是先创建主键再创建聚集索引就不可以,因为主键默认就是聚集索引
--但是如果先创建聚集索引,那么还可以再创建主键,因为主键不一定需要是聚集的
?
?
23.临时表
?
?
首页 上一页 7 8 9 10 下一页 尾页 10/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇SQL存储过程将符合条件的大量记录.. 下一篇SQL 查询所有表名、字段名、类型..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: