AZUR - Export Using BCP with Query - sql

I want to export data from a database Azur to an Excel file.
To do this, i use the command :
bcp [DatabaseName].[Table] OUT C:...\Test_Export.xls -c -U UserName#ServerName.database.windows.net -S tcp:ServerName.database.windows.net -P xxxxxxxx
--> It Works
BUT, when i want do the same with SQL Query in the command, like this :
bcp "Select field1, Field2 FROM [dbo].[ForecastTrialDisag]" QUERYOUT C:..\Test_Export.xls -d [DataBaseName] -c -U UserName#ServerName.database.windows.net -S tcp:ServerName.database.windows.net -P xxxxxx
I have errors :
SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open database
"[DataBaseName]" requested by the login. The login failed.
SQLState = 28000, NativeError = 18456
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'UserName'.
I don't understand why it doesn't work with a SQL query. UserName, databaseName and password are OK.
Thank you for your help,
Simon

I believe the username is in the format of: "user#server" without the "databases.windows.net" part at the end.
Have you tried that?

If the password is a strong one - contains punctuation for example, in my case I had a * and and ^ character in the password - than the use of double quotes for the password is needed:
bcp "Select field1, Field2 FROM [dbo].[ForecastTrialDisag]" QUERYOUT C:..\Test_Export.xls -d [DataBaseName] -c -U UserName#ServerName.database.windows.net -S tcp:ServerName.database.windows.net -P "xxxxxx"

I struggled with this for some hours by using the main db admin user define in Azure and never got it to work - I'm sure I was missing something simple.
What got me over the hump was to use SQL and CREATE USER plus GRANT a user inside of SQL, the kind of user that's not apparently visible in the Azure portal but is easily visible inside of SQL Management Studio.
This worked super well for me.

Related

how to use bcp in client system not remote

i want use bcp but i cant remote to data base
i use this command but i get error below:
bcp "Select personelid, '13'+date date, scheduleid, scheduleGroupName,
DetailCollectionID, Name, TimesStructure, DayNO, Sequence, WPID,
ToleranceTimes, DayState, StructureID, ID, STime, ETime from
framework.att.PersonDateStructure" queryout "D:\test\pds.txt" -T -c -S
10.0.0.108 -U sa -P 0000
but i get this error:
SQLState = 28000, NativeError = 18456 Error = [Microsoft][ODBC Driver
13 for SQL Server][SQL Server]Login failed for user 'KASRA\mnaghsh
You have specified the username&password (sa, 0000) using the -U -P flags but at the same time you have set the -T flag/argument which is for windows authentication, that is the reason you are getting the login failure message (apparently your domain account is not a valid sql server login), just remove the -T.

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;

BCP Utility. Creating format-file

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.

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.