SQL Server 2005索引碎片整理SQL语句
/*******************************************************/
/* 功能:SQL Server 2005索引碎片整理 */
/* 逻辑碎片>=30重建索引,<30重新组织索引 */
/*******************************************************/
/***********SQL Server 2005索引碎片整理*****************/
/**使用方法:将需要整理索引碎片的数据库设置为当前数据库**/
set nocount on
--使用游标重新组织指定库中的索引,消除索引碎片
--R_T层游标取出当前数据库所有表
declare R_T cursor
for select name from sys.tables
declare @T varchar(50)
open r_t
fetch next from r_t into @t
while @@fetch_status=0
begin
--R_index游标判断指定表索引碎片情况并优化
declare R_Index cursor
for select t.name,i.name,s.avg_fragmentation_in_percent from sys.tables t
join sys.indexes i on i.object_id=t.object_id
join sys.dm_db_index_physical_stats(db_id(),object_id(@T),null,null,'limited') s
on s.object_id=i.object_id and s.index_id=i.index_id
declare @TName varchar(50),@IName varchar(50),@avg int,@str varchar(500)
open r_index
fetch next from r_index into @TName,@Iname,@avg
while @@fetch_status=0
begin
if @avg>=30 --如果碎片大于30,重建索引
begin
set @str='alter index '+rtrim(@Iname)+' on dbo.'+rtrim(@tname)+' rebuild'
end
else --如果碎片小于30,重新组织索引
begin
set @STR='alter index '+rtrim(@Iname)+' on dbo.'+rtrim(@tname)+' reorganize'
end
print @str
exec (@str) --执行
fetch next from r_index into @TName,@Iname,@avg
end
--结束r_index游标
close r_index
deallocate r_index
fetch next from r_t into @t
end
--结束R_T游标
close r_t
deallocate r_t
set nocount off
/*
--查看指定表的索引情况
select t.name,i.name,s.avg_fragmentation_in_percent from sys.tables t
join sy
相关文档:
Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
select a.S# from (select s#,score from SC where C#='001') a,(select s#,score
fr ......
函数如下
CREATE FUNCTION StockBalance_AmountIn(@SortID int)
RETURNS numeric(18,4)
AS
BEGIN
Declare @dblReturn numeric(18,4)
Select @dblReturn = SUM(AmountCurrentMonthIn) from AT_Materiel_StockBalance Where SortID = @SortID
Return (@dblReturn)
END
......
SQL code
/*----------------------------------------------------------------
-- Author :feixianxxx(poofly)
-- Date :2010-04-20 20:10:41
-- Version:
-- Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)
Mar 29 2009 10:27:29
Copyright (c) 1988-2008 Microsoft Co ......
SQL Server 安装注意事项!!
==============================
http://www.cnblogs.com/pvistely/archive/2008/12/31/1365702.html
1. 需要.Net Framework 3.5,若在Vista或更高的OS上需要3.5 SP1的支持(在SQL2008安装的前会自动更新安装)
2. 需要Widnows PowerShell的支持,WPS是一个功能非常强大的Shell应用,命令与 ......
Case具有两种格式。简单Case函数和Case搜索函数。
--简单Case函数
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END
--Case搜索函数
CASE WHEN sex = '1' THEN '男'
  ......