2

我有一个非常简单的问题(这些问题通常是我大部分时间都在扯头发的问题)。我正在使用批处理文件来执行与批处理位于同一目录中的所有 .sql 查询,并将其所有结果保存到各种 .csv 文件中。

这是我的代码:

@echo off

REM Check that all parameters are present
@IF "%1"=="" goto error
@IF "%2"=="" goto error
@IF "%3"=="" goto error
@IF "%4"=="" goto error

REM For every *.sql file in this folder "." and all its subfolders (/R), do the following: 
@for /R "." %%F in (*.sql) do (
REM Print the command to be executed:
@echo Database : @SqlCmd -S %1 -U %2 -P %3 -d %4 -I -l60 -i "%%F" -o "%%F.output.csv"  -h-1 -s"," -w 1000 -W
REM Execute the command:
@SqlCmd -S %1 -U %2 -P %3 -d %4 -I -l60 -i "%%F" -o "%%F.output.csv" -h-1 -s"," -w 1000 -W
)

goto :EOF

:error

@echo Incorrect syntax : 
@echo     extract.cmd [servername] [user id] [password] [databasename]
@echo   
@pause

goto :EOF

作为测试,我运行以下查询:

select top(10) [name] from sysobjects 

输出:

sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
sysfgfrag
sysphfg
sysprufiles
sysftinds
sysowners

(10 rows affected)

这工作正常,除了两件事。首先,我还需要输出列标题。所以我删除了“-h -1”参数,然后给出:

name
----
sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
sysfgfrag
sysphfg
sysprufiles
sysftinds
sysowners

(10 rows affected)

这很好,除了我不希望标题和第一行数据之间的水平规则“------”。

另外,我不希望输出“(受影响的 10 行)”行。

有人知道我怎么能做到这一点吗?

谢谢卡尔
_

4

3 回答 3

1

尝试这个:

SET NOCOUNT ON;
SELECT
    [Name]
    FROM (SELECT TOP(10)
              2 AS SortBy, [name]
              FROM sysobjects 
          UNION
          SELECT 1, 'Name'
         ) dt
    ORDER BY [Name]

SSMS 的输出:

Name
----------------------------------------------
Name
sysallocunits
sysfiles1
sysftinds
syshobtcolumns
syshobts
sysowners
sysprivs
sysrowsetcolumns
sysrowsets
sysserefs

保留“-h -1”参数,列标题将被删除,但“名称”行仍会首先出现在结果集中。

于 2010-09-22T12:59:26.177 回答
0

您可以使用 BCP 代替 SQLCMD。这是一个批处理文件,用于在多个 SQL 服务器上运行您的查询。将此代码复制到 C:\SCRIPT 中的 CSV.CMD 中,然后在同一文件夹中创建一个 SERVERS.LST,每行一个服务器名。然后尝试运行:

CSV.CMD "从 master..sysdatabases 中选择 *"

@echo off
REM Execute a SQL command or a SQL file on multiple instances.
REM Pollus Brodeur January 2007

if not x==%1x goto MAIN
echo Error  : You need to pass a query in parameter
echo Exemple: C:\>csv.bat "select * from table"
goto END

:MAIN

echo ===============================================================================
echo CSV) Execute a TSQL command on multiple servers and consolidate the result
echo ===============================================================================

set QUERY=%1

echo For the following query :
echo %QUERY%

:EXEC
echo ===============================================================================
echo On the followinf instances:
type servers.lst
echo ===============================================================================
echo To modify list : Ctrl+C notepad c:\script\servers.lst
echo ===============================================================================
PAUSE

echo Executing...
if NOT EXIST .\out mkdir out
if EXIST out\output.csv (del out\output.csv)
FOR /F %%s in (servers.lst) DO echo %%s & bcp %QUERY% queryout out\csv.%%s.out -S %%s -T -c -t;

:ENDEXEC
type out\csv.*.out >> out\output.csv
echo ===============================================================================
echo Execution finished
echo To view results with MORE      : more /E /C out\csv.*.out.*.txt
echo ===============================================================================

:END
DEL out\csv.*.out
PAUSE
out\output.csv

这仅适用于 Windows 身份验证,但您可以根据需要更改代码。

于 2012-07-09T16:01:59.117 回答
0

据我所知,使用 sqlcmd 时您无法摆脱“---”行。

于 2010-09-22T19:24:12.870 回答