Stored Procedure output to file truncated - sql-server

I have a stored procedure in my local SQL Server that has one line output, line is about 1500 characters long. When I execute that stored procedure from sqlcmd (both PowerShell and CMD) the line gets cut off after 255 characters.
Here's the sqlcmd statement:
sqlcmd -S (local)\sqlexpress -E -d ReleaseTeam -Q "exit(exec[dbo].[SP_LOG_GETEMAILBODYHTML_TODAY])" -h-1 -o "D:\path\index.html"
How to fix it?

Related

How to export data from sql to excel using batch - exporting one column in sql per one column in excel?

I am exporting sql view using .bat script:
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q " SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv"
And its working. However, exported csv file is not organized as when you export using SSMS: using batch one column in sql is not copied to one column in excel, but more columns from sql to one column in excel. so i cannot play with exported data. Any idea how to write script which will give same results as exported with ssms?
Thanks
you can use these options.
-h rows_per_header
-s col_separator
-W (remove trailing spaces)
and also specify set nocount on to prevent the completion message from appearing.
It looks like this.
sqlcmd -U HPH_COM -P HPH_COM -S vTF-DB\LEVEL2 -d HPH_COM -Q "set nocount on; SELECT TOP (1000) * FROM HPH_COM.dbo.REP_TEMP_STOCKS_LASTDAY " -o "C:\sajtovi\TF1_PyroMeasuresLastDay.csv" -W -h -1 -s,

outputing select statement result into file

I have an sql file that contains below script that is ran via isql.
May I ask whats wrong with my output syntax? I am getting "Incorrect syntax near the keyword 'output'"
Sybase ASE version is 15.7
select * from tempdb..M3_STI_extracts_checking
output to employee.txt format ASCII
GO
isql offers the possibility to write the output into a file, if you set the option -o (Utility Commands Reference).
input.sql
select * from tempdb..M3_STI_extracts_checking
go
isql -i input.sql -o employee.txt
-J sets the charset (ASE 15.7 charsets)
isql -i input.sql -o employee.txt -J ascii_7
Was able to workaround by passing the variable from a shell script.
test.sh
output_file=test_file_'date +%m%d%Y'
${PARAM} isql << EOF
select * from tempdb..M3_STI_extracts_checking
GO > ${output_file}
EOF

TSQL - Trying to write printed output to a text file using batch file

Im am currently using this command to try to write to a text file the output that I PRINTED in the sql console(messages) using a stored procedure that uses the 'print' command to print stuff. I want to get that printed stuff into a text file using bat executable.
sqlcmd -Q "exec myprocedure" -S servername-d dbname-o C:\yourOutput.txt
But whenever I run this batch file, it just opens the yourOutput.txt and its blank...
C:\filepath\sqlcmd -Q "exec procedureName" -S servername-d dbname-o
Sqlcmd: 'dbname-o': Unexpected argument. Enter '-?' for help.
SQL auth :
sqlcmd -Q "exec schema.myprocedure;" -S servername -d dbname -u sqluser -p "sqluserpassword" -o C:\yourOutput.txt
Windows auth
sqlcmd -Q "exec schema.myprocedure;" -S servername -d dbname -o C:\yourOutput.txt
Should work with apropriate servername, schema, sqluser and sqluserpassword
Please note that the expected servername is : -S [protocol:]server[instance_name][,port]
Exemples :
-S tcp:localhost\instanceXXXXX,1433]
-S tcp:192.168.4.9\instanceYYYYY,1333]
Type : SELECT ##servername + '\' + ##servicename to get servername\instancename. For exemple it can gives you : "CO-DESQL01\MSSQLSERVER"

Remove column space from sqlcmd output file

I am trying to use sqlcmd to output the results from a stored procedure to a text file. However, it is putting a space between the resulting columns (a default delimiter) and I need to remove this. I am using the below code and have tried specifying nothing as the column separator see code below (-s "") but this does not work. It gives me a missing argument error.
sqlcmd -S Server name -U Username-P Password -d Db name-Q
"exec Stored procedure" -h -1 -s "" -o c:\newtest.txt
How can I specify nothing as the delimiter?
It can be done with bcp, using -t 0x90
-t is the field terminator in bcp. you can use it on stored procs as well as tables so it should do everything you need.
I just verified this works:
bcp dbname.dbo.tablename out "c:\test.txt" -S serveraddress -U username -P password -t 0x90 -c
Using a stored proc would be something like:
bcp "EXEC YourDatabaseNameGoesHere.dbo.sp_myproc ''0123,0234,0345''"

How to execute a script through sqlcmd in MS Sql server

I got a .sql file in which {0} kind of things used. I would like to know how can i pass values in the sqlcmd for {0} variable.
Sample script (test.sql):
GO
PRINT '{0}'
GO
And I am trying to executing through this command line
sqlcmd -S . -d master -E -i test.sql
But, where should i pass the parameter?
Just Try as below:
test.sql
GO
Print $var
GO
sqlcmd -S . -d master -E -i test.sql -v var="helloWorld"
You can use the -v switch to pass in variables:
sqlcmd -S . -d master -E -i test.sql -v varPrintMe="{0}"
Then you'll need to put the variables in $() in your sql script:
GO
PRINT $('varPrintMe')
GO

Resources