--
备份数据库
USE DataBaseName
declare @now datetime
declare @strTime nvarchar ( 50 )
declare @filename nvarchar ( 100 )
set @now = getdate ()
-- 时间格式 yyyy-mm-dd_hh-mi-ss
set @strTime = REPLACE ( REPLACE (( CONVERT ( nvarchar ( 20 ), @now , 120 )), ' : ' , ' - ' ), ' ' , ' _ ' )
set @filename = ' DataBaseName_ ' + @strTime + ' .bak '
EXEC sp_addumpdevice ' disk ' , ' DataBaseName_Back ' , @filename
print ' 开始备份 '
-- - 开始 备份
BACKUP DATABASE DataBaseName TO DataBaseName_Back
print ' 备份完成 '
print ' 新备份的文件名称: ' + @filename
EXEC sp_dropdevice ' DataBaseName_Back ' ;
USE DataBaseName
declare @now datetime
declare @strTime nvarchar ( 50 )
declare @filename nvarchar ( 100 )
set @now = getdate ()
-- 时间格式 yyyy-mm-dd_hh-mi-ss
set @strTime = REPLACE ( REPLACE (( CONVERT ( nvarchar ( 20 ), @now , 120 )), ' : ' , ' - ' ), ' ' , ' _ ' )
set @filename = ' DataBaseName_ ' + @strTime + ' .bak '
EXEC sp_addumpdevice ' disk ' , ' DataBaseName_Back ' , @filename
print ' 开始备份 '
-- - 开始 备份
BACKUP DATABASE DataBaseName TO DataBaseName_Back
print ' 备份完成 '
print ' 新备份的文件名称: ' + @filename
EXEC sp_dropdevice ' DataBaseName_Back ' ;
--
数据库信息
USE DataBaseName
declare @tableCount int
declare @ProcedureCount int
select @tableCount = count ( * ) from sysobjects where xtype = ' U '
select @ProcedureCount = count ( * ) from sysobjects where xtype = ' P '
print ' 表数量 : ' + CAST ( @tableCount AS varchar ( 5 ))
print ' 存储过程数量 : ' + CAST ( @ProcedureCount AS varchar ( 5 ))
SET NOCOUNT OFF ;
USE DataBaseName
declare @tableCount int
declare @ProcedureCount int
select @tableCount = count ( * ) from sysobjects where xtype = ' U '
select @ProcedureCount = count ( * ) from sysobjects where xtype = ' P '
print ' 表数量 : ' + CAST ( @tableCount AS varchar ( 5 ))
print ' 存储过程数量 : ' + CAST ( @ProcedureCount AS varchar ( 5 ))
SET NOCOUNT OFF ;
--
查询某个表中的所有列名
select t2.name from sysobjects t1,syscolumns t2 where t1.xtype = ' U ' and t1.id = t2.id and t1.name = ' TableName '
select t2.name from sysobjects t1,syscolumns t2 where t1.xtype = ' U ' and t1.id = t2.id and t1.name = ' TableName '
--
数据库的排它访问
use master
drop proc killspid
create proc killspid ( @dbname varchar ( 20 ))
as
begin
declare @sql nvarchar ( 500 )
declare @spid int
set @sql = ' declare getspid cursor for
select spid from sysprocesses where dbid=db_id( ''' + @dbname + ''' ) '
exec ( @sql )
open getspid
fetch next from getspid into @spid
while @@fetch_status <>- 1
begin
exec ( ' kill ' + @spid )
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
-- 用法
use master
exec killspid ' DatabaseName '
use master
drop proc killspid
create proc killspid ( @dbname varchar ( 20 ))
as
begin
declare @sql nvarchar ( 500 )
declare @spid int
set @sql = ' declare getspid cursor for
select spid from sysprocesses where dbid=db_id( ''' + @dbname + ''' ) '
exec ( @sql )
open getspid
fetch next from getspid into @spid
while @@fetch_status <>- 1
begin
exec ( ' kill ' + @spid )
fetch next from getspid into @spid
end
close getspid
deallocate getspid
end
-- 用法
use master
exec killspid ' DatabaseName '
--
定期删除日志
-- 1.清空日志
DUMP TRANSACTION DataBaseName WITH NO_LOG
-- 2.截断事务日志:
BACKUP LOG DataBaseName WITH NO_LOG
-- 3.收缩数据库
DBCC SHRINKDATABASE(DataBaseName)
-- 1.清空日志
DUMP TRANSACTION DataBaseName WITH NO_LOG
-- 2.截断事务日志:
BACKUP LOG DataBaseName WITH NO_LOG
-- 3.收缩数据库
DBCC SHRINKDATABASE(DataBaseName)
--
开启 Clr
exec sp_configure ' show advanced options ' , ' 1 ' ;
go
reconfigure ;
go
exec sp_configure ' clr enabled ' , ' 1 '
go
reconfigure ;
exec sp_configure ' show advanced options ' , ' 1 ' ;
go
exec sp_configure ' show advanced options ' , ' 1 ' ;
go
reconfigure ;
go
exec sp_configure ' clr enabled ' , ' 1 '
go
reconfigure ;
exec sp_configure ' show advanced options ' , ' 1 ' ;
go