【参加讨论】
“where 1=1” 是表示选择全部 “where 1=2”全部不选,
如:
if @strwhere !=''
begin
set @strsql = 'select count(*) as total from [' + @tblname + '] where ' + @strwhere
end
else
begin
set @strsql = 'select count(*) as total from [' + @tblname + ']'
end
我们可以直接写成
set @strsql = 'select count(*) as total from [' + @tblname + '] where 1=1 安定 '+ @strwhere
2、收缩数据库
--重建索引
dbcc reindex
dbcc indexdefrag
--收缩数据和日志
dbcc shrinkdb
dbcc shrinkfile
3、压缩数据库
dbcc shrinkdatabase(dbname)
4、转移数据库给新用户以已存在用户权限
exec sp_change_users_login 'update_one','newname','oldname'
go
5、检查备份集
restore verifyonly from disk='e:\dvbbs.bak'
6、修复数据库
alter database [dvbbs] set single_user
go
dbcc checkdb('dvbbs',repair_allow_data_loss) with tablock
go
alter database [dvbbs] set multi_user
go
7、日志清除
set nocount on
declare @logicalfilename sysname,
@maxminutes int,
@newsize int
use tablename -- 要操作的数据库名
select @logicalfilename = 'tablename_log', -- 日志文件名
@maxminutes = 10, -- limit on time allowed to wrap log.
@newsize = 1 -- 你想设定的日志文件的大小(m)
-- setup / initialize
declare @originalsize int
select @originalsize = size
from sysfiles
where name = @logicalfilename
select 'original size of ' + db_name() + ' log is ' +
convert(varchar(30),@originalsize) + ' 8k pages or ' +
convert(varchar(30),(@originalsize*8/1024)) + 'mb'
from sysfiles
where name = @logicalfilename
create table dummytrans
(dummycolumn char (8000) not null)
declare @counter int,
@starttime datetime,
@trunclog varchar(255)
select @starttime = getdate(),
@trunclog = 'backup log ' + db_name() + ' with truncate_only'
dbcc shrinkfile (@logicalfilename, @newsize)
exec (@trunclog)
-- wrap the log if necessary.
while @maxminutes > datediff (mi, @starttime, getdate()) -- time has not expired
and @originalsize = (select size from sysfiles where name = @logicalfilename)
and (@originalsize * 8 /1024) > @newsize
begin -- outer loop.
select @counter = 0
while ((@counter < @originalsize / 16) and (@counter < 50000))
begin -- update
insert dummytrans values ('fill log')