sql server 获取数据库中所有表的前10行数据保存到记事本中

@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
)
    原文作者:Abbott思宇
    原文地址: https://www.jianshu.com/p/ad9bb7d3ff7b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞