Exporting SQL results in Excel format - sql

exec master..xp_cmdshell'bcp "select * from [db name].[table name]"
queryout D:\testing1.xls -o "D:\querycommanddetails.txt" -T -c -C RAW'
I'm running above query on my local machine but System is showing output as 'NULL'.Can anybody help?

should be like this instead .. no queryout only out
exec master..xp_cmdshell'bcp "select * from [db name].[table name]"
out "D:\testing1.xls" -o "D:\querycommanddetails.txt" -e "D:\error.txt" -T -c -C RAW'
EDIT:
Try running this way from windows command prompt and see if it works
bcp "select * from [db name].[table name]" queryout "D:\testing1.xls" -c -T

Related

How can I specifie column order while Bulk Copying (BCP) Out the tables from SYBASE_IQ.

Below is the code I am using in the batch script to BCP process.
call bcp.exe DBName.tablename out FILENAME.csv -e FILENAMEerr.txt -c -t"|" -U USER DETAILS -P PASSWORD -S Servicename -r"\n"

Unexpected argument executing cmdexec on a SQL job to export to CSV

I try to run this on a SQL job:
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" > D:\Test.csv
How can I fix this error?
Sqlcmd: '> D:\Test.csv': Unexpected argument.
Have you tried like this -
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" -o D:\Test.csv
where -o output_file which would identify the file that receives output from sqlcmd.
Additionally you could try BCP which is best suited for bulk coping data between an instance of Microsoft SQL Server and a data file in a user-specified format.
Read more here.

Why does BCP export utility write output file in chinese?

I'm using the BCP utility to write SQL data to an output file. At random, some of the file contents are written in Mandarin (or so it seems), while the rest are written in English.
Here is the command I run from command prompt:
bcp.exe "SELECT * FROM MyTable" queryout "C:\Temp\MyTableExport.txt" -
e"C:\Temp\MyTableExport_Error.txt" -T -c -t, -S [server IP] -d [database]
Any idea why some of the files are outputting content in a different language?
Try this:
bcp.exe "SELECT * FROM MyTable" queryout "C:\Temp\MyTableExport.txt" -
e"C:\Temp\MyTableExport_Error.txt" -T -w -t, -S [server IP] -d [database]

Stored procedure execution

I have written a stored procedure to be called from sqlcmd (batch file). This is the code - for some reason it is not executing.
#ECHO OFF
SET /P FDate=Enter From Date:
SET /P TDate=Enter To Date:
ECHO sqlcmd -E -Q "dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
SET Param1=
SET Param2=
Any thoughts?
Try to add EXEC before procedure's name:
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'%Param1%', #TDate=N'%Param2%'" -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt
You can also try to passparameters via the /v argument. More datais here
sqlcmd -E -Q "exec dbo.SavingsAccountsAllDetail #FDate=N'$(Param1)', #TDate=N'$(Param2)'" /v Param1=%Param1% Param2=%Param2% -S OMNIDB-UAT -d HNBG_LOAN_TEST -o C:\SavingsAccountsAllDetailRepo.txt

How to export query results to csv in Microsoft SQL Server Management Studio?

Trying to export custom query to csv file I wrote the following command:
sqlcmd [-S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,total_events_count ,event_duration FROM dbo.event_daily_stats ORDER BY column1" -o "D:\MyData.csv" -h-1 -s"," -w 700]
but it returned the following error message:
The identifier that starts with '-S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,' is too long. Maximum length is 128.
Does anyone know how this issue could be solved?
Thank you!
I executed the command without "[" and "]" with no problem, have you tried in this way?
sqlcmd -S myserver -d mydb -E -Q "SELECT column1 ,column_date, DATENAME(WEEKDAY, column_date) AS day_of_week ,distinc_events_count ,total_events_count ,event_duration FROM dbo.event_daily_stats ORDER BY column1" -o "D:\MyData.csv" -h-1 -s"," -w 700
I think the problem is that you're trying to run this inside of SSMS when it should be run at a command prompt instead.