BCP export queryout error - sql-server

In Sql Server 2000, connected using osql command line, connected through a DSN ODBC, Using the OLEDB Provider, I am trying to export the result of a Stored Procedure to an XML file. However I get and error no matter how far down I simplify the query.
if I use the OUT qualifer I get the following error:
Msg 179, Level 15, State 1, Server SERVER, Line 1
Cannot use the OUTPUT option when passing a constant to a stored procedure.
which from my reading is expected. but no matter how I use QUERYOUT I get following error:
Msg 170, Level 15, State 1, Server SERVER, Line 1
Line 1: Incorrect syntax near 'queryout'.
Here is the simplest query I can muster and I still get this error:
bcp "Select * FROM [SERVER].[SCHEMA].[TABLE]" queryout \\fileserver\test.txt -c -T

You need to create a text file such as
set nocount on
select * from table
and save it. Let say it is in c:(input_text).txt
Then, in SQL server management studio, create and run following osql query to get output_text.
osql -U (login id) -P (password) -S (server name) -d (db name) -i c:(input_text).txt -o c:(output_text).txt -h-1 -s " " -n -w 2000

Related

sqlcmd utility returns "Could not find stored procedure"

I am trying to execute a stored procedure using this command line utility:
sqlcmd -m 1 -S inxcert -U user1 -P u8er1 -i "D:\ESP\RunSQL.sql" -h -1 -o "D:\ESP\testoutput.txt"
Following is what I have written in RunSQL.sql:
exec spc.load_tables
Though the stored procedure exist in the database, credentials are correct and SQL Server runs fine when run from SSMS I am getting the following error in the output file:
Msg 2812, Level 16, State 62, Server I0160SQL03\I0160SQL03, Line 1
Could not find stored procedure 'spc.mjr_vs_load_tables'.
Please help me to learn how to resolve the error.
Looks like it's executing against the default database (probably master) so not finding your procedure.
Try either adding:
USE [DBNAME]
to RunSQL.sql, or specifying:
-d DBNAME
to your sqlcmd parameters.

SQL Server BLOB image column - extracting with BCP queryout - corrupted files AND bug

I need to export pdf and image files from a column in SQL Server 2012. Jonas's 3-step process in "How to export an image column to files in sql server" is the clearest instruction I've found. I did everything exactly as he stated. Two notes:
Where he says "your_db" in the BCP statement, you need database_name.schema_name.table_name.
No line breaks are allowed.
I was able to export files one at a time after this, but they were corrupt. The files were slightly smaller (by 1-15 KB) than the actual working PDFs that I can access through the UI.
This turned out to be a format issue - if you're not exporting XML files, you have to create a special format file. There's a great solution by Conor, in "SQL Server BCP export corrupted file" that tells how to create a format file and reference it in your BCP query. First I created the format file from the command line:
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn>bcp CentricityPM.dbo.PatientProfileAttachment format nul -T -n -f C:\bcpdir\bcpfile.fmt
Then I edited and re-saved the format file as per Conor's post. I ran my BCP query:
EXEC master ..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.PatientProfileAttachment WHERE PatientProfileid = ''11568'' AND type = ''pdf'' " queryout "C:\exportdir\testfile.pdf" -T -N'
The error:
Starting copy...
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Host-file columns may be skipped only when copying into the Server
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Unable to resolve column level collations
NULL
BCP copy out failed
Jon of All Trades noted in "BCP Error: columns may be skipped only when copying into the Server" that this is a Microsoft bug reported on 8/6/2010. He suggested creating a table with the right number of columns. I created a table with one column and one row of my data (?!) which Conor had actually referenced in his post but I didn't really get it until this point.
Please note, this is NOT useful to me, because I need not only the data, but a way to identify it (I have the name I want for each file stored in another column). But I gave it a try anyway - I re-ran the bcp format file:
C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn>bcp CentricityPM.dbo.PatientProfileAttachment format nul -T -n -f C:\bcpdir\bcpfile.fmt
Here's the format file it gave me:
11.0
1
1 SQLIMAGE 4 0 "" 1 data ""
And here are the edits I made - I changed the data type as TT suggested below, and changed the 4 to a 0:
11.0
1
1 SQLBINARY 0 0 "" 1 data ""
I ran my query:
EXEC master ..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" queryout "C:\exportdir\testfile.pdf" -T -fC "C:\bcpdir\bcpfile.fmt" '
It ran with no errors... but the file is still corrupted.
Can anyone see anything I've done wrong? Does anyone know a workaround for the one-column-only bug? Or if you know of a working tool that will do this for me, that'd be great too. I tried https://sqlblobextractor.codeplex.com/ early on, with no success.
You are using parameter -f "C:\bcpdir\bcpfile.fmt" but from my experience that should be -fC "C:\bcpdir\bcpfile.fmt". To be honest I don't remember anymore why... I once made something similar to export files (.zip) from database and my command has -fC parameter for the export file. I whish I could give you a proper explanation. Anyway, HTH.
Try the following command:
EXEC master..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" QUERYOUT "C:\exportdir\testfile.pdf" -T -fC "C:\bcpdir\bcpfile.fmt"'
An alternative is to specify the -C RAW option. This specifies that no conversion is done from one code page to another.
EXEC master..xp_cmdshell 'BCP "SELECT data FROM CentricityPM.dbo.TempImageFour" QUERYOUT "C:\exportdir\testfile.pdf" -T -f "C:\bcpdir\bcpfile.fmt" -C RAW'
Also, make sure that your format file has SQLBINARY as data type for your column.

BCP utility incorrect syntax

What is incorrect about this syntax?
bcp transitschedule in calendar_dates.txt -T -f calenar_dates.fmt -F 2
I have tried this through sqlcmd and SSMS to a database on Azure. When I run the command I get:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'in'.
Yet, the examples here are not very much different syntactically. If I add database name or schema name error shifts towards the period.
Like SQLCMD, BCP is a stand-alone command-prompt utility that must be invoked from a command prompt. If the source text and format files reside on your client, you'll need to add the -S -U and -P parameters like you do with SQLCMD. For example:
bcp databasename.schemaname.transitschedule in calendar_dates.txt -f calenar_dates.fmt -F 2 /S azure-database-server /U azure-database_login /P azure-database-password

Error While using SQLCMD in PDW

I'm using SQLCMD in PDW for extracting data into a flat file. The command line syntax is given below:
sqlcmd -S "10.20.30.40,19001" -d MyPDW_DB -U PDW_User -P Password1 -Q "SET QUOTED_IDENTIFIER ON; SELECT * FROM MyPDW_DB.dbo.SampleFact" -o "FactOut.txt" -s"|"
When I try to execute the batch file, I get the following error:
Msg 104409, Level 16, State 1, Server PdwTdsServer, Line 1
Setting QuotedIdentifier to 'OFF' is not supported.
I am assuming this is due to the fact that there is a "comma" in the server name (IP address,Port Number). I can use this command for extracting data from SQL tables. Any idea on how I can make this working for PDW?
Thanks in advance
I got this working partially.
sqlcmd -S "10.20.30.40,19001" -d MyPDW_DB -U PDW_User -P Password1 -I -Q "SELECT * FROM MyPDW_DB.dbo.SampleFact" -o "FactOut.txt" -s"|"
For setting the quoted_identifier OFF, the option to use is "-I". However, I'm still trying to find an alternative for "SET NOCOUNT ON" option which is not supported in PDW. If someone can help me with that, I'd greatly appreciate that.

SQL Server BCP tool

I'm trying to IMPORT data from EXPORTED Dat file from SQL Server in this way:
bcp "SELECT FieldName FROM [BaseName].[dbo].[TableName] where xxxxxx=16"
queryout Message_out.dat -n -Uusername -Sservername
When I try to import dat to sql server like this
bcp basename.dbo.tablename in "path\to\datfile.dat" -c -T
I get error:
Error = [Microsoft][SQL Server Native Client 10.0]Unexpected EOF
encountered in BCP data-file
regards, Grigor.
Try to explicitly indicate the field and row terminators for your file, for example, if your file is comma delimited and each row is in a new line:
bcp basename.dbo.tablename in "path\to\datfile.dat" -c -T -r\n -t,
And if if there are any other peculiarities in the format of your file, use the options to help bcp understand your file format using the options. A detailed documentation is available at msdn.
I see this question is old, but maybe it will help someone in the future...

Resources