34,692
社区成员
发帖
与我相关
我的任务
分享
--1删除所有用户表
DECLARE @table varchar(64),
@sql varchar(150)
DECLARE curTable SCROLL CURSOR FOR SELECT name FROM sysobjects WHERE Type = 'U' And Name <> 'dtproperties' ORDER BY name
OPEN curTable
FETCH FIRST FROM curTable INTO @table
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @sql = 'delete ' + Cast(@table as varchar)
--print @sql
exec (@sql)
FETCH NEXT FROM curTable INTO @table
END
CLOSE curTable
DEALLOCATE curTable
--2备份不会,用数据导入导出应该可以吧!
if exists(select 1 from sysobjects where name='file2table' and objectproperty(id,'isprocedure')=1)
drop procedure file2table
go
create procedure file2table
@servername varchar(200) --服务器名称
,@username varchar(200) --用户名,如果用nt验证方式,则为空''
,@password varchar(200) --密码
,@tbname varchar(500) --数据库
,@filename varchar(1000) --导入/导出路径/文件名,如果@tbname参数指明是导出整个数据库,则这个参数是文件存放路径,文件名自动用表名.txt
,@isout bit --1为导出,0为导入
as
declare @sql varchar(8000)
begin --导出整个数据库,定义游标,取出所有的用户表
declare @m_tbname varchar(250)
print @m_tbname
if right(@filename,1)<>'\' set @filename=@filename+'\'--如果不是文件夹根目录,设置为根目录
set @m_tbname='declare #tb cursor for select name from '+@tbname+'.dbo.sysobjects where xtype=''u'''
exec(@m_tbname)
open #tb
fetch next from #tb into @m_tbname
while @@fetch_status=0
begin
set @sql='bcp '+@tbname+'.'+'dbo.'+@m_tbname+case when @isout=1 then ' out ' else ' in ' end
+ @filename+@m_tbname+'.txt -w'
+' /S '+@servername +case when isnull(@username,'')='' then '' else ' /U '+@username end
+' /P '+isnull(@password,'')
exec master.dbo.xp_cmdshell @sql
fetch next from #tb into @m_tbname
end
close #tb
deallocate #tb
end
go
----导入整个数据库
exec file2table '域名','sa','密码','数据库名','e:\admin',0
----导出整个数据库
exec file2table '域名','sa','密码','数据库名','e:\admin',1
--OR
EXEC sp_Msforeachtable 'DROP table ?'
--刪除所有用戶表
DECLARE @SQL VARCHAR(8000)
SELECT @SQL=ISNULL(@SQL,'')+' DROP TABLE '+NAME FROM SYSOBJECTS WHERE TYPE='U'
EXEC( @SQL)
sp_Msforeachtable 'truncate table ?'
--SQL如何备份到异机
写成存储过程,建立作业定时备份~~~
--在sql中映射一下就可以了
exec master..xp_cmdshell 'net use z: \\yizhi\D$ "密码" /user:yizhi\administrator'
/*--说明:
exec master..xp_cmdshell 'net use z: \\xz\c$ "密码" /user:xz\administrator'
z:是映射网络路径对应本机的盘符,与下面的备份对应
\\xz\c$是要映射的网络路径
xz\administrator
xz是远程的计算机名,
administrator是登陆的用户名
密码 面指定的administrator用户的密码
--*/
--备份;with init覆盖 ¦noinit添加
backup database 库名 to disk='E:\H_BACKUP.bak' with init
--COPY
exec master..xp_cmdshell 'copy E:\H_BACKUP.bak z:'
--删除(这句可以去掉)
--exec master..xp_cmdshell 'del E:\H_BACKUP.bak'
--完成后删除映射
exec master..xp_cmdshell 'net use z: /delete'