SQL Server 2008 - use cmd to output with headers to .csv

回眸只為那壹抹淺笑 提交于 2019-12-11 00:01:52

问题


I have a pretty simple question (and these are typically the ones I spend most of my time tearing my hair out about). I am using a batch file to execute all the .sql queries that are in the same directory as the batch, and to save all their results to various .csv file.

Here is my code:

@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

As a test, I run the following query:

select top(10) [name] from sysobjects 

which outputs:

sysrscols
sysrowsets
sysallocunits
sysfiles1
syspriorities
sysfgfrag
sysphfg
sysprufiles
sysftinds
sysowners

(10 rows affected)

This works fine, except for two things. Firstly, I need to have the column headers output as well. So I remove the "-h -1" parameter, which then gives:

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

(10 rows affected)

which is great except for the fact that I don't want the horizontal rule "------" between the heading and the first row of data.

Also, I don't want the "(10 rows affected)" line to be output.

Does someone know how I could achieve this?

Thanks
Karl


回答1:


try this:

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

OUTPUT FROM SSMS:

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

leave the "-h -1" parameter and the column headings will be removed, but the "Name" row will still appear in the result set first.




回答2:


I far as I can see, you cannot get rid of "---" line when using sqlcmd.




回答3:


You can use BCP instead of SQLCMD. Here's a batch file to run your query on multiple SQL servers. Copy this code in CSV.CMD in C:\SCRIPT, then create a SERVERS.LST in the same folder with with one servername per line. then try running:

CSV.CMD "select * from 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

This will work only with Windows authentication but you can change the code to your need.



来源:https://stackoverflow.com/questions/3769302/sql-server-2008-use-cmd-to-output-with-headers-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!