bcp error while exporting data using management studio in sql 2005 express but not using DOS - sql

i am using sql server 2005 express edition .
when i export data via bcp utility using dos prompt then there is no error ,
but when i created a stored procedure for the export process and use management studio express for exporting data then it gives the following error :
SQLState = S1000, NativeError = 0
Error = [Microsoft][SQL Server Native Client 10.0]Unable to open BCP host data-file
Please help.

I had the same issue, but the answer is that I was being less than bright.
As the command was being run on a server, not my local machine, it was trying to write to a non-existent folder on the server. It was executing successfully against the root of C: but not against my share folder, C:\share. The problem for me was that I was looking for the file in the root of my C Drive, not the servers.
When I changed the path to \mymachinename\share, everything worked.

When you run the bcp utility using the DOS command line you are using the logged in persons credentials (usually your own), but when running as a stored procedure you are using the credentials of the SQL server process, which usually is configured to have much less permissions than ordinary users in order to provide safety against various attacks.
Check in the Services list of which user is used for the SQL server database engine and check if that user has enough read/write permissions to the directories and files involved.

Related

SQL BULK INSERT using UNC path

I have a developer pc "A". And I have a SQL Server "B".
My SQL Server is a Microsoft SQL Server 2019.
On server B, I have a database with a stored procedure, which bulk loads data from a text file (using the BULK INSERT command).
I have now created an SSIS project on server A, which calls the stored procedure on server B using the "Execute SQL Task". The connection on the task is pointing at the database on server B. I have tried using the OLE DB connection and ADO.
When I place my text file on server B and reference the file like D:\myFolder\myFile.txt, everything works fine.
When I place my text file on server A and references the file like \\\A\myShare\myFile.txt it fails. The error I get is:
[Execute SQL Task] Error: Executing the query "exec BulkInsert
'\\A\myShare\myFile.txt'" failed with the following error: "Cannot
bulk load because the file "\\A\myShare\myFile.txt" could not be
opened. Operating system error code 5(Access is denied.).". Possible
failure reasons: Problems with the query, "ResultSet" property not set
correctly, parameters not set correctly, or connection not established
correctly.
I have tried logging on to server B and via a File Explorer open the file using the above path. It works fine. And yes, I am logged in as the same user on both servers A and B.
I have also tried giving "Everyone" read/write access to the share, but still the same poor result.
I am only interested in knowing how to fix this problem, using the stored procedure call - I do not wish to rebuild its functionality in SSIS (the stored procedure is maintained by an external company and they may change it at their will, but we agree on how I can call it).
According to what I read from Microsoft, it should not be a problem, call BULK INSERT using a UNC path.
According to what I read from Microsoft, it should not be a problem, call BULK INSERT using a UNC path.
This is true, UNC paths are supported by the BULK INSERT command.
Based on the Microsoft documentation, BULK INSERT has three main requirements:
The server must have permission to access both the file and the destination database.
The server runs the Bulk Insert task. Therefore, any format file that the task uses must be located on the server.
The source file that the Bulk Insert task loads can be on the same server as the SQL Server database into which data is inserted, or on a remote server. If the file is on a remote server, you must specify the file name using the Universal Naming Convention (UNC) name in the path.
The first requirement means that you should grant the SQL Server Service account to access the UNC path, not the Windows account you are logging in with.
You should refer to the following articles to find the SQL Server service account name:
Configure File System Permissions for Database Engine Access
How to Find Service Account for SQL Server and SQL Server Agent?
Besides, you can learn more about SQL Server service accounts and permissions in the following documentation:
Configure Windows Service Accounts and Permissions
Alternative - Mapping network drive
As an alternative, you can try mapping the network drive within SQL Server. You can check the following articles for more information:
Make Network Path Visible For SQL Server Backup and Restore in SSMS
How to Map Network Drive as Fixed Drive?

SQL Server Database is not getting attached again

I tried to detach my SQL Server 2008 R2 database for creating a duplicate of it, meanwhile connections to that database were active from Visual Studio (2010). So SQL Server denied to detach. After closing Visual Studio, I tried again and my database was successfully detached.
After detaching my database I tried to copy both files (.mdf and .ldf) to another folder as a duplicate copy, but I could not do so because I was getting error that says
You require permission from the computer administrator to make changes to this file
I am using Windows 7 and my account is Administrator. I also tried to attach my database again to SQL Server (running it with administrator permission) but I got the following error:
Failed to retrieve data for this request. (Microsoft.SqlServer.Management.Sdk.Sfc)
Additional Information:
An exception occurred while executing a Transact-Sql statement or batch.
(Microsoft.SqlServer.ConnectionInfo)
CREATE FILE encountered operating system error 5 (Access is denied,) while attempting to open or create the phyical file'C:/Mywor/MyDataBase.mdf'.
(Microsoft SQL Server, Error: 5123)
Please help me if someone know how to handle this problem.
Here are couple things you can try:
Stop SQL Server service and see if you can copy files (to stop SQL service go to SQL Server Configuration manager)
Try to copy some other database files in that directory and see if that works (make sure permissions for both files are exactly the same)
Try to copy .mdf and .ldf files in original SQL Server data directory (you can find it in SSMS -> Instance properties ->Database Settings
If you’re not using windows explorer for copying files make sure you run it as admin (right click -> Run as administrator)
This is a permissions issue on the database files directory, or the files themselves. Just because you're logged on as an administrator doesn't mean that Windows 7 allows you to do anything you want without whinging at you first. Also, the SQL Server service account may not have the appropriate permissions on the file/folder.

Failed to generate user instance in SQL Server 2012

I am using .mdf file for connectivity, when I select it it generates error:
Failed to generate user instance in SQL Server due to failure in starting the process for the user instance
I have attached image to make it clear to you.
http://i.stack.imgur.com/B1ae2.png
Can anyone answer my question.
Thank you!!
Try following steps :
Stop all user instance processes of SQL Server running under your local account (open up task manager and look for sqlsrv.exe process and end it)
Delete all files in the %LOCALAPPDATA%\Microsoft\Microsoft SQL Server Data\SQLEXPRESS folder
F5 again
The problem is known issue. SQL Server Express cannot create a user instance using the set of engine files in your local app data folder, deleting them should allow SQL Server to recreate them with the correct credentials for you to F5 successfully.

SQL Server Express unable to attach or delete mdf file

My database file Whist.mdf was attached to a SQL Server Express through Management Studio, but it stopped working in that sense that NHibernate in my asp.net solution fired some connection exceptions. I just detached it from Management Studio because I couldn't open the database in there either.
But when I try to reattach it I get this error:
Attach database failed for Server 'MyPCName\SQLECPRESS'. (Microsoft.SqlServer.Smo) Unable to open the physical file "C:\Program
Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\Whist.mdf".
Operating system error 32: "32(process cannot access the file because
it is being used by another process)". (Microsoft SQL Server, Error:
5120)
I have googled it but can't find a solution that works for me. E.g.:
http://www.byteblocks.com/post/2010/01/06/Unable-to-open-the-physical-file-Microsoft-SQL-Server.aspx
"Cannot open user default database. Login failed." after installing SQL Server Management Studio Express
There is something about User Instance but I cant figure out if it have anything to do with that:
http://blogs.msdn.com/b/sqlexpress/archive/2006/11/22/connecting-to-sql-express-user-instances-in-management-studio.aspx
I have tried to delete the mdf file in windows but I can't do that either:
The action can't be completed because the file is open in system
I have tried stopping the SQL Server process, closing explore and delete from cmd and rebooting...
Hope someone can help - I guess it is just a simple thing..
I think you are on the right track. The error messages you are getting indicate that some SQL Server process is keeping the database file open. And SQL Server is designed to only allow the database file to be open by a single SQL Server process at a time.
You are also probably right to suspect User Instance of SQL Server Express to be the guilty process here. User Instance is a special SQL Server process that doesn't run as a service and instead is started and owned by the application that tries to connect to it. In your case there are two User Instances that can be the problem, one owned by the ASP.NET application you mentioned and one owned by you and started by Visual Studio. If you are using IIS Express or you configured the IIS App Pool for the ASP.NET application to run as your account then we only have one User Instance to worry about.
My recommendation is that you don't use User Instances and don't try to connect directly to the database file. Go over your connection strings and remove User Instance=true from all of them. You want to remove AttachDBFilename=... property as well.
If the database file is added to any of your Visual Studio solutions remove it (just be careful not to delete the file itself). The last step is to remove any connections to the database file from your Data Connections node in the Server Explorer.
Then restart the machine to make sure any User Instances that were running in the background were shutdown. After the machine starts again you should be able to attach the database file back to your main SQL Server Express using SQL Server Management studio. Let's name the database Whist
To connect to your database use the following connection string:
Data Source=.\SQLExpress;Integrated Security=true;Initial Catalog=Whist
When you connect from the ASP.NET application, make sure that you have created an account in your SQL Server Express for the application.
Turned out to be an issue with Daemon tools for me as well associating the .mdf file as an image.
If you want to copy your .mdf and .ldf file from your database, you can set your database file to 'offline' first . Please follow this steps:
Open your SQL Server 2008(suppose you are using SQL Server 2008 ).
Select your Database that you want to copy then left click.
Go to Tasks -> Take Offline.
Right click.
Then you can copy this database file to any folders.

How can i test my TSQL syntax?

Quick question: How do I get some kind of database to use to test my SQL syntax and create basic data.
I have Sqlite Code which I'll soon put on a server. I have SQL Server 2008 installed with visual studio 2010. I tried connecting to the database and had no luck.
I also tried using an .mdf file instead thinking it's a file and I won't have connectivity issues. Wrong, I still couldn't connect and I used this site to help me (i'm aware its 2005)
In that case I used:
var conn = new SqlConnection(#"Server=.\SQLExpress;AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;Database=dbo;Trusted_Connection=Yes;");
exception
Unable to open the physical file "C:\dev\src\test\SQL_DB_VS_Test\test.mdf".
Operating system error 5: "5(Access is denied.)".
Cannot attach the file 'C:\dev\src\test\SQL_DB_VS_Test\test.mdf' as database 'dbo'.
With trusted = no I get Login failed for user ''. (What user am I suppose to set?). I created the .mdf with Visual Studio somehow.
What if you try this connection string:
var conn = new SqlConnection(#"Server=.\SQLExpress;
AttachDbFilename=C:\dev\src\test\SQL_DB_VS_Test\test.mdf;
Database=test;Integrated Security=SSPI;");
I don't think it's a good idea to call your database "dbo" (that's a SQL Server keyword - I wouldn't use it for my own purposes!), and also I believe you need to use Integrated Security=SSPI; to define Windows authentication - Trusted_Connection is not used for SQL Server connection strings, AFAIK.
Have you tried using SSMS to access your local instance? It's helpful for getting connected and getting everything setup. Also, I think the default install of Sql Express with VS only support trusted connections.
After creating the mdf file with visual studios right click the mdf and select properties. In it you'll see a row called Connection String. Copy/paste it into your app and it should connect. The key part is User Instance=True