BCP Utility. Creating format-file - sql

MSDN says that if table name is myTestFormatFiles, table is created in the AdventureWorks2012 sample database under the dbo schema, so the command is:
bcp AdventureWorks2012..MyTestFormatFiles format nul -c -t, -f myTestFormatFiles.Fmt -T
My case:
I tryed this one:
bcp TestDB..test_table format nul -c -t, -f d:\format.fmt -T
Here is the result:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could no
t open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-
specific error has occurred while establishing a connection to SQL Server. Serve
r is not found or not accessible. Check if instance name is correct and if SQL S
erver is configured to allow remote connections. For more information see SQL Se
rver Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
I think something wrong with TestDB..test_table. May be I need to specify k551l\sqlexpress in command, something like k551l\sqlexpress..TestDB..test_table.
What is my problem really?

add the server\instance names with by adding -S k551l\sqlexpress to your command.

Related

bcp to extract data from SQL query to local machine

I am using below code
bcp "SELECT COL1 FROM DBO.V_VIEW_1" queryout d:\VIEW_PART1.csv -S AB6Y7YYY,1433 -c -t",", -T
AB6Y7YYY : I got this with below code in SSMS
SELECT ##SERVERNAME
I am able to see VIEW_PART1.csv in d drive but it is empty and error in command line is as below
SQLState = 08001, NativeError = 10061
Error = [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired
What am i doing wrong?

I am trying to create a bcp command line that will convert my SQL view to a CSV file

I am trying to create a bcp command line that will convert my SQL view to a CSV file.
This is my code:
EXEC xp_cmdshell 'bcp "SELECT * FROM [dba].[schema Name].LeaveExtract" queryout "C:\Temp\myExport.csv" -T -c -t,'
go
I keep getting this error:
-SQLState = 08001, NativeError = 2
-Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
-SQLState = 08001, NativeError = 2
-Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
-SQLState = S1T00, NativeError = 0
-Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
NULL
I made sure that my named pipes and TCP/IP are enabled and that my services are running. I don't seen to know what the problem is.
Try this Method, Enter all required parameters for bcp and cmdshell
DECLARE #sql NVARCHAR(4000)
SELECT #sql = 'bcp "SELECT * FROM [dba].[schema Name].LeaveExtract" queryout "C:\Temp\myExport.csv" -C -T -S ' + ##servername + ' -T -t -c -C 65001'
PRINT #sql
EXEC master..xp_cmdshell #sql;

Creating bcp format file from local database table - connection error

I need to create a format file from a local database table but I get a connection error when I try to run the command in cmd. Here is the command I am using:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T
I get the following errors:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-
specific error has occurred while establishing a connection to SQL Server.
Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
I have checked Services.msc and SQL Server is indeed "Started". I have also checked Sql Server Configuration Manager and the SQL Server state is "Running" and Start Mode is "Automatic".
The connection string to the local database is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Tools_TextileMagazine\CSharp_Template\Tools_TextileMagazine\OrderDatabase.mdf;Integrated Security=True
Any ideas to what could be wrong?
Add the -S parameter like specified here:
http://msdn.microsoft.com/it-it/library/ms162802.aspx
So your connection string will be:
bcp OrderDatabase.dbo.Orders format nul -c -f D:\Format\Orders.fmt -T -S localhost\V11.0
I assume from your connection string that you named istance: V11.0

BCP in SQL command give NativeError = 2 when no -S parameter on local DB

I have this command in SQL:
DECLARE #cmd NVARCHAR(4000)
DECLARE #pathAndFileName NVARCHAR(MAX)
DECLARE #result INT
SET #pathAndFileName = 'C:\Temp\file.csv'
SET #cmd = 'bcp "SELECT TOP 1 * FROM SomeDB.dbo.SomeTable" queryout "'
+ #pathAndFileName + '" -w -T -t; '
EXEC #result = xp_cmdshell #cmd
SELECT #result
and it outputs this:
SQLState = 08001, NativeError = 2 Error = [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2 Error = [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0 Error = [Microsoft][SQL Server Native Client 11.0]Login timeout expired
#result is 1
It works only if I add -S parameter with server name and database name (like this -S "MYCOMPUTERNAME\DBINSTANCENAME").
But when I try this on remote server this works without -S parameter.
How can I setup my local DB, so I don't need -S anymore?
If you ommit -S then it will try the default, localhost unnamed instance:
-S server_name[ \instance_name]
Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility connects to the default instance of SQL Server on the local computer.
From your example it seems you do have an instance name, so it will not be possible to connect w/o specifying the -S explicitly. Besides, your code will always be more portable and easier to troubleshoot if you take the extra steps of specifying the -S params explicitly. Use SERVERPROPERTY(MachineName) and SERVERPROPERTY(InstanceName), make sure the code is cluster aware.

Getting Error with an SQL query

I'm getting an error when running this query in SQL Server 2008:
EXEC master..xp_cmdshell'bcp
"select Row_ID, Service_Code,Skill from Account_Service"
queryout "d:\text.txt" -c -T -x'
Error is:
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [2].
SQLState = 08001, NativeError = 2
Error = [Microsoft][SQL Server Native Client 10.0]A network-related or instance-specific error has occurred while
establishing a connection to SQL Server. Server is not found or not
accessible. Check if instance name is correct and if SQL Server is
configured to allow remote connections. For more information see
SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]
Login timeout expired
NOTE: Shared Memory, Named Pipes, TCP/IP are all enabled in SQL Server Configuration Manager
Look into the SQL Configuration Manager.
I guess TCP/IP Protocoll is not enabled, so you can Access your SQL Server via. Shared Memory.
So you would be able to Connect if you are on the SQL Server it self but can not Access it from another Maschine.
Otherwise I would say it is an Network Problem as you see the Connection times out. Maybe a Firewall Software is blocking or your Server can not be reached.
The most likely Cause I expririenced, fitting to oyur discrition is disabled TCP/IP Protocol for SQL Server.