@echo off
SET HOST=192.168.0.243
SET USER=sa
SET PASSWORD=passwo'r'd
SET DATABASE=database_name
SET OUTPUT=d:\BrowseResualt
IF EXIST %OUTPUT% (
RD /S /Q %OUTPUT%
)
MD %OUTPUT%\data\
rem 获取目标数据库中的所有用户表,保存到文件 %OUTPUT%\tables.txt
sqlcmd -U%USER% -P%PASSWORD% -S%HOST% -Q"select name from [%DATABASE%]..sysobjects where xtype='U'" -o"%OUTPUT%\tables.txt"
For /f "skip=2 tokens=1*" %%i in (%OUTPUT%\tables.txt) do (
echo reading from table %%i
sqlcmd -U%USER% -P%PASSWORD% -S%HOST% -Q"select top 20 * from [WD2000V30]..[%%i]" -o"%OUTPUT%\data\%%i.txt"
)
echo finish reading from tables
echo ==========================
rem 删除没有内容的表
for /R %OUTPUT%\data %%i in (*.txt) do (
echo %%i
findstr "(0 行受影响)" %%i >NUL && rm %%i && (
echo ----- has no data, deleted.
echo delete empty file %%i >> %OUTPUT%\log.txt
)
)
echo finish deleting empty files
echo ==========================
rem 所有的数据整合到一个文件中,方便搜索
for /R %OUTPUT%\data %%i in (*.txt) do (
echo %%i>>%OUTPUT%\datas.txt
type %%i>>%OUTPUT%\datas.txt
)