日期:2014-05-18 浏览次数:20384 次
EXEC sp_configure 'show advanced options', 1 GO -- 重新配置 RECONFIGURE GO -- 启用xp_cmdshell EXEC sp_configure 'xp_cmdshell', 1 GO --重新配置 RECONFIGURE GO create proc sp_test @path varchar(255),@svrname varchar(255),@username varchar(255),@password varchar(255),@indbname varchar(255) as select * from sysdatabases where dbid > 6 declare @dbname varchar(255) declare @tablename varchar(255) declare @cmdline varchar(255) declare @creatpath varchar(255) select @creatpath = 'md ' + @path exec master..xp_cmdshell @creatpath exec ('declare all_cursor cursor for select name from sysdatabases where dbid > 6 and upper(name) like ''%'+@indbname+'%''') open all_cursor fetch all_cursor into @dbname while @@fetch_status = 0 begin print @dbname select @cmdline = 'md ' + @path + '\'+ @dbname exec master..xp_cmdshell @cmdline exec ('declare tbl_cursor cursor for select name from ' + @dbname +'.dbo.sysobjects where xtype = ''U''') open tbl_cursor fetch tbl_cursor into @tablename while @@fetch_status = 0 begin print @tablename select @cmdline = 'bcp "'+ @dbname + '..' + @tablename + '" out "' + @path + '\' + @dbname + '\' +@tablename + '.txt" -c -U' + @username +' -P'+@password +' -S ' + @svrname print @cmdline exec master..xp_cmdshell @cmdline fetch tbl_cursor into @tablename end close tbl_cursor deallocate tbl_cursor fetch all_cursor into @dbname end close all_cursor deallocate all_cursor GO
EXEC sp_configure 'show advanced options', 1 GO -- 重新配置 RECONFIGURE GO -- 启用xp_cmdshell EXEC sp_configure 'xp_cmdshell', 1 GO --重新配置 RECONFIGURE GO create proc sp_test @path varchar(255),@svrname varchar(255),@username varchar(255),@password varchar(255),@indbname varchar(255) as select * from sysdatabases where dbid > 6 declare @dbname varchar(255) declare @tablename varchar(255) declare @cmdline varchar(255) declare @creatpath varchar(255) select @creatpath = 'md ' + @path exec master..xp_cmdshell @creatpath exec ('declare all_cursor cursor for select name from sysdatabases where dbid > 6 and upper(name) like ''%'+@indbname+'%''') open all_cursor fetch all_cursor into @dbname while @@fetch_status = 0 begin print @dbname select @cmdline = 'md ' + @path + '\'+ @dbname exec master..xp_cmdshell @cmdline exec ('declare tbl_cursor cursor for select name from ' + @dbname +'.dbo.sysobjects where xtype = ''U'' --and name in(''你想要导出表的名称'')') open tbl_cursor fetch tbl_cursor into @tablename while @@fetch_status = 0 begin print @tablename select @cmdline = 'bcp "'+ @dbname + '..' + @tablename + '" out "' + @path + '\' + @dbname + '\' +@tablename + '.txt" -c -U' + @username +' -P'+@password +' -S ' + @svrname print @cmdline exec master..xp_cmdshell @cmdline fetch tbl_cursor into @tablename end close tbl_cursor deallocate tbl_cursor fetch all_cursor into @dbname end close all_cursor deallocate all_cursor GO