1.脚本:通过视图复制数据到表中
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[UP_CopyTableFromView]
(
@sourceViewName nvarchar(50),
@newTableName nvarchar(50),
@copySuccess bit output
)
as
begin
declare @script nvarchar(500);
--判断视图是否存在
IF not EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID('[dbo].['+@sourceViewName+']') AND type in (N'V'))
begin
set @copySuccess=2;
return ;
end
--判断表是否存在
IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].['+@newTableName+']') AND type in (N'U'))
begin
begin try
set @script='select * into '+ isnull(@newTableName,'')+' from '+isnull(@sourceViewName,'');
print @script;
begin tran
exec (@script);
commit tran
--
set @copySuccess=0;
end try
begin catch
set @copySuccess=1;
end catch
end
end
2.脚本:表对表完全数据复制
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[UP_CopyTableFromTable]
(
@sourceTableName nvarchar(50),
@newTableName nvarchar(50),
@copySuccess bit output
)
as
begin
declare @script nvarchar(500);
--判断表是否存在
IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].['+@sourceTableName+']') AND type in (N'U'))
begin
set @copySuccess=2;
return ;
end
--判断表是否存在
IF not EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].['+@newTableName+']') AND type in (N'U'))
begin
begin try
set @script='select * into '+ isnull(@newTableName,'')+' from '+isnull(@sourceTableName,'');
print @script;
begin tran
exec (@script);
commit tran
--
set @copySuccess=0;
end try
begin catch
set @copySuccess=1;
end catch
end
end
3.脚本:Kill某数据库里的所有进程
可以应用于强制备份与还原,也可以用于1222错误:锁超时。
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER Proc [dbo].[Sp_KillAllProcessInDB]
@DbName VarChar(100)
as
if db_id(@DbName) = Null
begin
Print 'DataBase dose not Exist'
end
else
Begin
Declare @spId Varchar(30)
DECLARE TmpCursor CURSOR FOR
Select 'Kill ' + convert(Varchar, spid) as spId
from master..SysProcesses
where db_Name(dbID) = @DbName
and spId <> @@SpId
and dbID <> 0
OPEN TmpCursor
FETCH NEXT FROM TmpCursor
INTO @spId
WHILE @@FETCH_STATUS = 0
BEGIN
Exec (@spId)
FETCH NEXT FROM TmpCursor
INTO @spId
END
CLOSE TmpCursor
DEALLOCATE TmpCursor
end
4.脚本:创建数据库
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UP_CreateDatabase]
(
@newDatabaseName nvarchar(50),
@createSuccess bit output
)
as
begin
declare @script nvarchar(1000)
-- declare @year nvarchar(50)
declare @databaseName nvarchar(50)
-- set @year=DATEPART(year,GETDATE());
set @databaseName=@newDatabaseName
--如果数据库已存在
if exists (select 1 from sys.sysdatabases where name=@databaseName)
begin
return;
end
set @script=' ';
set @script=@script+' CREATE DATABASE ['+@databaseName+'] ON PRIMARY ';
set @script=@script+' ( NAME = '''+@databaseName+''', '
set @script=@script+' FILENAME = '+'''C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\DATA\'+@databaseName+'.mdf'''+' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )'
set @script=@script+' LOG ON '
set @script=@script+' ( NAME = '''+@databaseName+'_log'', FILENAME = '+'''C:\Program Files\Microsoft SQL Server\MSSQL.4\MSSQL\DATA\'+@databaseName+'_log.ldf'''+' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)'
set @script=@script+' COLLATE Chinese_PRC_CI_AS'
-- set @script=@script+ char(13)+char(10)+ ' GO '+char(13)+char(10);
-- set @script=@script+' EXEC dbo.sp_dbcmptlevel @dbname='''+@databaseName+''', @new_cmptlevel=90'
-- set @script=@script+ char(13)+char(10)+ ' GO '+char(13)+char(10);
set @script=@script+' IF (1 = FULLTEXTSERVICEPROPERTY('+'''IsFullTextInstalled'''+'))'
set @script=@script+' begin'
set @script=@script+' EXEC ['+@databaseName+'].[dbo].[sp_fulltext_database] @action ='+ '''disable'''
set @script=@script+' end'
exec (@script)
print @script
SET @createSuccess =0
if @@error<>0
begin
SET @createSuccess =1
end
end
5.脚本:备份数据库
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UP_BackupDatabase]
(
@sourceDatabaseName nvarchar(50),
@BackupDisk nvarchar(500),
@BackupName nvarchar(50),
@backupSuccess bit OUTPUT
)
AS
BEGIN
Declare @bakstr varchar(300)
declare @strPath nvarchar(200)
set @strPath=@BackupDisk+@BackupName;
set @bakstr='BACKUP DATABASE '+ @sourceDatabaseName+' TO disk='+''''+ @strPath +''''
exec(@bakstr)
print @bakstr
SET @backupSuccess =0
if @@error<>0
begin
SET @backupSuccess =1
end
end
6.脚本:还原数据库
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[UP_RestoreDatabase]
(
@sourcePath nvarchar(100),
@sourceDatabaseName nvarchar(50),
@restoreDatabaseName nvarchar(50),
@restoreSuccess bit output
)
as
begin
--从源数据库的.bak文件中读出源数据库的逻辑名
declare @dLogicalName nvarchar(50);--源数据库.mdf逻辑名
declare @lLogicalName nvarchar(50);--源数据库.ldf逻辑名
set @dLogicalName='';
set @lLogicalName='';
--还原数据库的物理文件名
declare @dFileName nvarchar(100);--要还原的数据库的.mdf物理文件名
declare @lFileName nvarchar(100);--要还原的数据库的.ldf物理文件名
set @dFileName='';
set @lFileName='';
begin try
--得到逻辑文件名
declare @sourceScriptD nvarchar(500);
declare @sourceScriptL nvarchar(500);
set @sourceScriptD=' select @dLogicalName=name from '+isnull(@sourceDatabaseName,'')+'.sys.database_files where type=0;'
set @sourceScriptL=' select @lLogicalName=name from '+isnull(@sourceDatabaseName,'')+'.sys.database_files where type=1;'
print @sourceScriptL;
exec sp_executesql @sourceScriptD,N'@dLogicalName nvarchar(50) output',@dLogicalName output
exec sp_executesql @sourceScriptL,N'@lLogicalName nvarchar(50) output',@lLogicalName output
--得到物理文件名
declare @restoreScriptD nvarchar(1000);
declare @restoreScriptL nvarchar(1000);
set @restoreScriptD=' select @dFileName=physical_name from '+isnull(@restoreDatabaseName,'')+'.sys.database_files where type=0;'
set @restoreScriptL=' select @lFileName=physical_name from '+isnull(@restoreDatabaseName,'')+'.sys.database_files where type=1;'
print @restoreScriptL;
exec sp_executesql @restoreScriptD,N'@dFileName nvarchar(100) output',@dFileName output
exec sp_executesql @restoreScriptL,N'@lFileName nvarchar(100) output',@lFileName output
print '1'+@dFileName
print '2'+@lFileName
--还原数据库
declare @script nvarchar(1000);
set @script= 'RESTORE DATABASE ' + isnull(@restoreDatabaseName,'') +
' FROM DISK = ' + ''''+isnull(@sourcePath,'')+'''' +
' WITH MOVE '+ ''''+ @dLogicalName +''''+' TO '+''''+@dFileName+''''+',
MOVE '+ ''''+ @lLogicalName +''''+' TO '+''''+@lFileName+''''+' ,replace';
--执行
exec (@script);
print @script;
--
set @restoreSuccess=0;
end try
begin catch
set @restoreSuccess=1;
end catch
end