Bcp stored procedure not writing file to disk - sql-server

Below when I executing the stored procedure in either code or in sql server management studio 2008 it saids it copied successfully rows, But I am not for sure why its not writing the file to disk. can some please help me, I have been struggling with this for the pass few days. I have enable command shell and everything. The weird thing is it works from the command line, but when i execute from stored procedure, it does not write file to disks, can some some help me
USE [ColorDb]
GO
ALTER PROCEDURE [dbo].[prc_WriteTableToTextDelimitedFile]
(
#FilePath VarChar(256)
)
AS
BEGIN
DECLARE #sql varchar(8000)
SET #sql = 'bcp "SELECT * FROM ColorDb.dbo.ColorTable" queryout "'+ #FilePath +'" -c -t; -T -SXXXXXXXXXXXX'
Print #sql
exec master..xp_cmdshell #sql
END
RETURN

I would print out the contents of #sql, and #FilePath.
I just ran some code through the MS SQL Server Manager Debugger window, you could step through the code. I did notice that you don't seem to have your quotes matched, and I don't see where you intialize #FilePath to anything. Looking at your last segment it looks like it has an unmatched double quote.
I didn't use any double quotes, I ran from a query in the query builder under MSSQL Server Manager. I got file output, moved files, concatenated files and what not from the query window.
Like this simple concatenation command:
Exec master..xp_cmdshell 'type c:\bcp\sysobjects.txt >> c:\bcp\a.txt'
Also this creation/output command:
SELECT #sql = 'bcp master..sysobjects out c:\bcp\sysobjects.txt -c -t\t -T -S' + ##servername
REPLACE( #sql, 'sysobjects.txt', #EmpRecordsFILENAME)
EXEC master..xp_cmdshell #sql
The files got created, and concatenated with these simple commands!

Related

How to add a double quote text delimter using BCP

I am having an issue with setting a double quote (") text delimter to my .csv file using BCP.
Take for example the current SQL script;
declare #sql varchar(8000)select #sql = 'bcp dmP.pdiStb.vTest2 out
c:\bcp\test001234.csv -c -t"\",\"" -T -S'+ ##servername
EXEC master..xp_cmdshell #sql
When I execute the above, I get a CSV file that looks like the following;
55","sql_gmi_srv","Policy_RenewalDueDate
The above is missing a double quote at the start and end of the file, is there a solution to solve this?

BCP to .csv delimiter issue

I am having issue with the way results are displayed with a bcp export to csv.
The below works fine, but is comma delimited so won't work for what I need.
DECLARE #sql VARCHAR(8000)
SET #sql = 'bcp "SELECT * FROM db..viewname" queryout "C:\test.csv" -c –t, -T -S <SERVERNAME>'
EXEC master..xp_cmdshell #sql
Results:
But If I change the "," to a pipe (or anything else) it breaks.
DECLARE #sql VARCHAR(8000)
SET #sql = 'bcp "SELECT * FROM db..viewname" queryout "C:\test.csv" -c –t"|" -T -S <SERVERNAME>'
EXEC master..xp_cmdshell #sql
Results:
The view used is a simple column select from a table with a WHERE clause.
What am I missing here..?
I know this is an old question but I just ran into this problem yesterday. To add a pipe delimiter you need to escape the pipe with a carrot like so:
-t^|
So set your #SQL like so:
SET #sql = 'bcp "SELECT * FROM db..viewname" queryout "C:\test.csv" -c –t^| -T -S <SERVERNAME>'
Hope this helps someone. :)

Calling HTTP command from MS SQL

I had to migrate a Production SQL server from SQL 2008 to SQL 2012. The below piece of code was working fine with 2008. For some reason. it complains
"'HTTP' is not recognized as an internal or external command, operable
program or batch file."
Can someone please let me know what is going on ?
DECLARE #cmdstr VARCHAR(8000)
DECLARE #URL varchar(8000)
SET #URL = '"My_URL"'
CREATE TABLE #cmd_result (OUTPUT VARCHAR(8000))
EXEC master..xp_sprintf #cmdstr OUTPUT, 'HTTP /s %s ' ,#url
INSERT #cmd_result
EXEC master..xp_cmdshell #cmdstr
PRINT #cmdstr
SELECT * FROM #cmd_result WHERE len(rtrim(output)) > 1
DROP TABLE #cmd_result
you could run
EXEC master..xp_cmdshell 'dir http*'
and see if your http is showing up. chances are it is not or it needs to be added to the PATH environmental variable in order to access it from anywhere.

T-SQL writing to a file txt or csv

I've spent all day scouring the net on answers. Apparently tsql doesn't have its own nifty write to file commands. Here is my dilemma
I have a load file that I am creating where a single line can reach 10K+ in length. On SQL Server varchar(MAX) limit is 8000 (so I believe) so I broke those lines into several variables. I tried to do PRINT but the window pane has allows 4000. The workaround is to print those broken lines one variable at a time but that can get tedious for manual labor so I opted to look into writing it into a txt file one variable at a time.
I looked into BCP via xpcommandshell and it looked promising. Issue was that I could get this line to work on the command prompt yet that exact same line doesn't work on TSQL query:
declare #cmd varchar(8000)
select #cmd = 'bcp Client_DB "Select name from dbo.t_TagBuild_set" queryout "Desktop\LAMB\dummy.txt" -c -t, -T'
exec master..xp_cmdshell #cmd
bcp Client_DB "Select name from dbo.t_TagBuild_set" queryout "Desktop\LAMB\dummy.txt" -c -t, -T works perfectly fine on command prompt
despite this slight progress, my manager didn't want to go that route. So instead I opted for sp_OACreate and sp_OAMethod after enabling sp_configure via executing this line on SQL:
sp_configure 'Ole Automation Procedures', 1
One of the very first lines on this route is this:
EXECUTE #hr = sp_OACreate 'Scripting.FileSystemObject' , #objFileSystem OUT
#hr gives a 0 so that's good but #objFileSystem yields 16711422 and #hr eventually becomes -2146828218 which i believe is permissions.
i really am at a loss on finding something simple to do yet i've made this increasingly difficult on myself to find something concrete just to write to a couple variables in a row before adding a new line and repeat the process.
If anyone can expertly help me figure out BCP or sp_OACreate then I'd be very appreciative cause the net as is barely helps (and this is after I spent a lot of time looking through Microsofts own site for an answer)
The reason your BCP didn't work is because you were running it from xp_cmdshell with a trusted user. xp_cmdshell is not run under the user running the script. You can either a) change your bcp command to use a sql login/password or b) create a job to run it (not xp_cmdshell) because you can control what user it is run as by using run as and a credential. You can then launch the job within a script by using sp_start_job.
Your other good option is to create an SSIS package and either run it through the command line (say in a bat file) or again run it through a job.
Create a view of your query and select it using sqlcmd.
declare #cmd varchar(8000)
select #cmd = 'sqlcmd -h-1 -W -S servername -d database -U username -P password -Q "SET NOCOUNT ON; select * from VIEW_NAME " -o "C:\OUTPUT\query.csv" '
exec master..xp_cmdshell #cmd
-h-1 removes the header
SET NOCOUNT ON removes the rows affected footer
You can write to file on T-SQL using this (it works into trigger):
--char(9) = \t
DECLARE #filename nvarchar(1000);
SET #filename = ' (echo '+#parameterA+ char(9) +#parameterB+ ... + char(9) +#ParameterN+') > e:\file1.txt && type e:\file1.txt >> e:\file2.txt';
exec DatabaseName..xp_cmdshell #filename, no_output

sql writing to a csv file not showing in Desktop

I have a sql stored procedure that creates a csv file. Here is the following procedure.
SET #query = 'select * from tableName123'
SET #bcpCommand = 'bcp " ' + #query + ' " queryout "c:\linecode.csv" -T -c -t,'''
EXEC master..xp_cmdshell #bcpCommand
The code runs successfully, but I don't see linecode.csv in the Desktop. Is this an issue with the write permission or is there something wrong with what I did?
I am using sql server 2008, and c# to call the stored procedure.
I do not think that "Desktop:\" will point to the desktop.
Have you tried a trusted location such as "C:\folder\myfile.csv"?
It is likely that it otherwise created a "Desktop:\linecode.csv" file somewhere. Can you look for "*linecode.csv" ?
Try this instead:
SET #query = 'select * from tableName123'
SET #bcpCommand = 'bcp " ' + #query + ' " queryout "%USERPROFILE%\Desktop\linecode.csv" -T -c -t,'''
EXEC master..xp_cmdshell #bcpCommand
This depends on your version of Windows defining the USERPROFILE environment variable, which is present in all modern versions of Windows. You can read more about the USERPROFILE environment variable here.

Resources