BCP Permissions Error When Using queryout with SQL Azure - sql-server

I'm trying to query data from one database1..table and import it to another database2..table. database2..table already has some of the data, so I need to filter it. I'm calling this command:
bcp "SELECT * FROM Foo.dbo.Bar WHERE id > 10000" queryout ./Bar.txt -N -U user#machine -S tcp:hash.database.windows.net -P password
Error: SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "master" requested by the login. The
login failed.
Since user#machine doesn't have access to the master database, the query fails. Note that if I use bcp Foo.dbo.Bar out ./Bar.txt... instead of the above, it works, since it never tries to open the master database.
It's a SQL Azure instance, so I cannot link the servers together.
So: how can I do this filter on data freshness in Foo.dbo.Bar without BCP ever trying to connect to master?

According to the documentation, you must specify the -d parameter when using queryout with SQL Azure.

Related

Sqlcmd from app service to local SQL Server using hybrid connection

I have setup an Azure app service with a hybrid connection to my local on premise SQL Server 2014.
The connection is just fine I can ping it from app service using tcpping.
However I cannot make the connection to the database. The sqlcmd command fails complaining about the user / pass - but they are correct.
I am using this command
sqlcmd -S <servername> -d Sample_Dev -U xxx -P xxxx -I
I am getting this error:
Sqlcmd: Error: Microsoft SQL Server Native Client 11.0 : Login failed for user 'xxx'
I have searched online for that error and I am unable to make it work. Can you please help me on that ? I really appreciate your help.
Thanks
I was able to make it work by creating a database user directly on mssql and not using AD.

How to write using BCP to a remote SQL Server?

I have a remote SQL Server with a hostname I am using to connect to.
BCP suggests to use
bcp DBName.dbo.tablename in C:\test\yourfile.txt -c -T -t
However when I try this it does not connect to DBName as that is not a valid alias. I get native error 2.
How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?
How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?
You can specify the IP address (here just 127.0.0.1) instead of the server name.
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1\instance"
If you truly want to go IP\port only then use IP address and port separated by comma
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1,60905"
Of course in that case you would need to know the port the instance is using, and your instance should be set to use a fixed port
The DBName is the name of the database, you need to use -S option to provide the server (or server\instance) name.
From MSDN:
-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.
This option is required when a bcp command is run from a remote
computer on the network or a local named instance. To connect to the
default instance of SQL Server on a server, specify only server_name.
To connect to a named instance of SQL Server, specify
server_name\instance_name.

Database connection fails when connecting using sqlcmd with encrypt connection

I am trying to connect to sql server with forced encryption using sqlcmd.
I have added a certificate to sql server and enabled forced encryption.
But the sqlcmd tool is not able to connect to the sql server. Below is the command I am using:
sqlcmd -S"mymachine.mydomain.dom" -b -d"mydatabase" -i"E:\mysqlfile.sql"
-v mode="Load" -N -U"sa" -P"password"
this works when I remove mydomain.dom and -N option like:
sqlcmd -S"mymachine" -b -d"mydatabase" -i"E:\mysqlfile.sql"
-v mode="Load" -U"sa" -P"password"
What may be the reason?
Edit : I have stored the server certificate in both personal and trusted people store. I also ran the follwing query to check the encryption .
SELECT encrypt_option FROM sys.dm_exec_connections
encrypt_option is true for all the sessins.
Edit 2 : I am using a default instance. The certificate CN is mymachine.mydomain.dom. Also another connection to the same database with encrypt=true and using .net code is working fine. So this doesn't seems to be a certificate issue.
Edit 3 : Tcp/ip is enabled in network configuration. Remote connection is also enabled. I also tried executing sqlcmd as administrator but got the same error.

SQL Server Network Interfaces: Connection string is not valid [87]

When I am running this from cmd on my SQL Server 2008 instance:
sqlcmd -U sa -S mymachinen_name\MSSQLSERVER
(where MSSQLSERVER is my instance name)
I get prompt for password and after that I get this:
Password:
HResult 0x57, Level 16, State 1 SQL Server Network Interfaces: Connection string is not valid [87].
Sqlcmd: 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..
Sqlcmd: Error: Microsoft SQL Server Native Client 10.0 : Login timeout
expired.
I have my SQL Server browser service started and also I can login through Management Studio with Windows authentication.
Also found this KB article, but this is for SQL Server 2005/2000.
Typically, the MSSQLSERVER service is the service of a un-named default instance.
Therefore, try this:
sqlcmd -U sa -S mymachine_name
(without specifying any instance name - just the machine name)
I encountered this error trying to connect to the server through Jenkins and resolved it this way:
sqlcmd -e -S "SERVER_NAME,PORT_NUMBER" -U %USER% -P %PASSWORD% -i "D:\\test.sql"
Please note that when connecting via sqlcmd to another server, if the target server is part of a HA cluster group, you will want to specify -M. Also, you will want to not specify the DB that is using the AG. (this is useful for installing jobs via sqlcmd to remote locations, among other things)
sqlcmd -S SERVERNAME,1433 -d msdb -Q "SELECT job_id FROM msdb.dbo.sysjobs WHERE (name =
N'RANDOM_JOB_NAME')" -M
It is set on installation, there is no default. Try to log on without a username

bcp error with sql server

I have created a stored procedure in sql server 2005 to export data from a query to a network path. The user is a sql user and it is also created in the AD. The server that I am logged in is a remote server, with sysadmin privilege. I am encountering the following error while executing the procedure or even the query:
output
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Native Client]Login timeout expired
NULL
It looks like I have no access to the remote server, while I am executing the query while I am logged in the server. The procedure also executes automatically at 01.30 am. The command that is executed is a bcp command :
DECLARE #SQLCommand [varchar](max)
,#Query [varchar](max)
,#SERVER VARCHAR(1000)
SET #Query = 'SELECT * FROM INFINITY_SYSTEM.[dbo].[CTI_WFM_LILO_View]'
SET #SERVER = '[server_name]'
SET #SQLCommand = 'EXEC xp_cmdshell ''bcp "' + #Query + '" queryout "\\network_path\lilo'+CONVERT(VARCHAR,GETDATE(),112)+'.csv" -Uusername -Ppassword -c -t, -r, -S"'+#SERVER+'"''' --also tried with -T in stand of the -Uusername -Ppassword
EXEC (#SQLCommand)
The command works when I try it from a local DB in my local sqlserver to the same path.
Thanks
Make sure that you can telnet to the remote server. I would also try to connect to the remote server via sqlcmd. You may have a firewall blocking the connection. Also the sql server services will need permissions to the share/directory, in order for you to write a file to it.
Another thing you should consider is using an SSIS package to export the data to the remote share. Generally is not a great practice to use xp_cmdshell to export the data. You can do everything you are trying to do via a simple SSIS package. As an added benefit this also minimizes security risk because you can disable xp_cmdshell.

Resources