SQL Server Bulk Insert Format FIle could not be opened - sql-server

I'm trying to use Bulk Insert to insert some data into my database on local computer. I am using a SQL Server Express database and executing query using Microsoft SQL Server Management Studio. When I try to execute a query I get this error.
Cannot bulk load because the file "‪D:\Countries.xml" could not be opened. Operating system error code 123(The filename, directory name, or volume label syntax is incorrect.).
I have been moving this file over my HDD by everywhere and still got the same error. To be honest I have no idea what is going on. Any tips?
BULK INSERT Research.dbo.Countries
FROM 'C:\Users\someuser\Desktop\Localization DB\countryInfo.txt' WITH
(
FORMATFILE='‪D:\Countries.xml',
FIRSTROW=2,
CHECK_CONSTRAINTS
);
GO

when you copy a path for example from properties of file
copied text have some extra thrash bytes, you need to view pasted text as ANSI and will be revealed

Are you 100% sure you are connecting to you locally installed SQL Server - not a networked one?
That's my favorite thing to do, rdp into a machine, or on a VM, and forget where I am physically connected. If you copy and paste from Word, sometimes it puts in the wrong type of ' back that out and replace.

I have no idea why but the problem was with ' mark being copied. When i deleted it and put it from keyboard it started working.

Related

SSIS Permission Issue Flat Files

I recently had to move my files to a new SSIS server. Everything seems to be working except when I try to execute a bulk insert it tells me
(Cannot bulk load because the file "E:\FlatFiles\SSG\apmast.txt" could
not be opened. Operating system error code 21(The device is not
ready.).".
It does this for all my flat files. I found an article saying you need to give the MSSQLSERVER user full control of the files, which I did but this does not seem to fix it. Any other ideas? Do I need to give other files the same permissions? I really don't want to just throw full control around if I don't have to. Thanks
I figured it out, turns out that a bulk insert tells the server to look locally for the text file. I was trying to get SSIS to do a bulk insert of flat files from one server into another sql server on the network. As soon as I put the flatfiles on the remote server it grabbed and used them. This seems like a very odd way for it to work. I would expect it to push the files to the sql server instead of asking the sql server to look for the files locally via a hard path.

Data in local .mdf file didn't changed, but no error

I use a .mdf file as my database. I also used Linq-to-SQL connection.
At first, my application can add and display the data in the .mdf file correctly. But when I try to see the data by clicking the .mdf file directly and from the table, the database is empty.
I've set the .mdf file and the .dbml file Copy if newer, but still nothing.
Please help me and thank you for your help.
I bet your connection string contains
AttachDbFileName=somefilename.mdf
The whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf file and most likely, your INSERT works just fine - but you're just looking at the wrong .mdf file in the end!
If you want to stick with this approach, then try putting a breakpoint on the myConnection.Close() call - and then inspect the .mdf file with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
The real solution in my opinion would be to
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. YourDatabase)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...

What causes "SQL01268: Msg 1834: cannot be overwritten. It is being used by database"? (in Database Project)

Full error below:
Error 1 SQL01268: .Net SqlClient Data Provider: Msg 1834, Level 16, State 1, Line 1 The file 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\testdatabase.mdf' cannot be overwritten. It is being used by database 'testdatabase'. SchemaCompare5 25 0
I read about this on some forums and quite a few people were getting this and supposedly for some it had to do with parameterising the file path name to the db etc. or ticking "ignore file names and path for files and log files" prior to doing the comparison - this I have tried to no avail.
Someone else who has the same/similar issue: http://social.msdn.microsoft.com/Forums/en/vstsdb/thread/5a8b8c52-adb4-4a5a-95ed-09ad22bacf60
Basically for me I seem to get this error irrespective of which databases I am using for target and source. Say even if I create a new database with one table and another database with no tables and different name and try to update the schema of the database with no tables using the db with the single table it still gives me the error. Almost like SQL server express has gone nuts. I remember using the schema comparison tool before with no trouble. All db connections were created, tried many ways to do this to no avail ie: pointing to copy of *.mdf db in another folder or deleting things from the DATA folder in mysql directory in program files etc.
Also believe I read someone had solved a similar issue be deleting some files the scheme
comparison tool creates, think they were *.sql type not sure which ones though.
The problem arises because the database files already exist.
Try the below within the Visual Studio database project.
Create the schema comparison.
Go to menu: Data > Schema Compare > Export to > Editor
Once the script has been created delete the alter database commands that add the physical files. Then create a connection, switch to SQLCMD mode (making sure you have focus on the script) and execute the script.
To switch to SQLCMD mode access: Data > Transact-SQL Editor > SQLCMD Mode
If the target DB already exists, just delete through Management Studio first before you deploy for the first time.
I had already created the database manually through SQL Server Management Studio when I was establishing the original connection when creating the Database Project via the SQL Server 2008 Wizard in VS. It wouldn't allow me to continue until it could detect that the database existed. Then once I got to the Deploy step for the first time, it threw the same error as above. I just went into Management Studio and deleted the DB, then tried to deploy and it worked fine. Interestingly, it's deploying every time now without me having to go in and delete it every time.
RESTORE DATABASE B FROM DISK = 'A.bak'
WITH MOVE 'DataFileLogicalName' TO 'C:\SQL Directory\DATA\B.mdf',
MOVE 'LogFileLogicalName' TO 'C:\SQL Directory\DATA\B.ldf',
REPLACE ---> Needed if database B already exists

How do I fix "incorrect syntax near GO" errors in scripts generated with Microsoft's Database Publishing Wizard?

I've been searching for a "definitive" solution to this for a couple of hours now and can't find out exactly what the problem is so am hoping someone can point me in the right direction.
I'm trying to create a script for a database (an MDF file DB) on a remote machine and use it to create the same database locally. I've got Visual Studio 2010 on both machines so used the Database Publishing Wizard to generate the script.
I next created a blank DB in Visual Studio locally (another MDF file DB) and selected to create a new query. I've pasted the copied script in but when I try and run it I get dozens of "incorrect syntax near GO" errors. I'm not sure how many in total as the alert pop-up goes off the bottom of my screen. It would seem to be a lot though.
I trimmed off all but the very top of the script and I still get the same errors with it. It now looks like this:
SET ANSI_NULLS ON
GO
I still get the "incorrect syntax near GO" error with this.
Is there something inherently wrong with the scripts this tool generates or am I doing something wrong with it?
The remote DB is 2008 R2 and my local is just plain 2008, hence why I don't just copy the MDF file across.
If anyone has any suggestions, they would be most welcome.
From the docs:
GO is not a Transact-SQL statement; it
is a command recognized by the sqlcmd
and osql utilities and SQL Server
Management Studio Code editor.
Either use such a tool to execute the script or omit the GO commands.
I ran into the same problem recently and found the right answer to it (I think).
If you create a query in some random editor other than SQL Server Management Studio Code editor at the end of a line you may get a CARRIAGE RETURN (\r) character instead of a CARRIAGE RETURN + LINE FEED (\r\n). And even if you hit the DEL key at the end of the line and then Enter it doesn't change.
Use Notepad++ to replace CR (\r) for CRLF (\r\n) - keep the order, it's important!

SQL Server Bulk Insert fails (Network related)

I'm fairly new to SQL Server.
I'm trying to bulk insert into a table, using the command in SQL Server Management Studio (2005):
BULK INSERT Table1 FROM 'c:\text.txt' WITH (FIELDTERMINATOR = '|')
I get the error:
Msg 4860, Level 16, State 1, Line 1
Cannot bulk load. The file "c:\text.txt" does not exist.
I'm positive the file actually exists.
I get the feeling that it is looking for the file on the local hard drive for where ever the server is. Is that the case? If so, how do you generally solve this problem? (to note, I've tried specifying the network address of my PC when entering the location of the text file, but I get a permission error. Also, I know in advance that my company doesn't allow files to placed on a server).
SQL Server does not have an SQL statement that reads data from the client end (as the other posters have pointed out). Other RDBMS products do implement this (eg. the Postgres COPY statement lets you specify a file on the server or a file on the client that is read by the db connectivity library on the client side).
You can achieve moving data from a file on the client to a table on SQL Server using the bcp command line program.
bcp lets you copy data from a local file to a table on the server, or from a table (or select query) on the server into a local file. For example:
bcp servername.dbname.tablename in c:\temp.txt -T -c
will copy a tabbed delimited file (temp.txt) into the specified table (assuming the file contains the right number of columns).
I am not sure if this helps, but it is the only way to move data from a client file to a server table without giving the server some sort of access across the network to the data file on client.
I'd agree that it's a problem with the file being on your C drive, and not the server's drive.
If it's a permissions issue, have you tried creating a file share on your workstation that the server does have permissions to read from? Maybe something like \YourWorkstation\SQLFile, and then granting everyone (or Guest, depending on how your network permissions are set up) read access on it?
If you can't create the share on your laptop, or you can't grant rights to it for some reason, is there a file share somewhere in the office that you do have rights to, and that SQL can also read from? Maybe a NAS or a "Common" network folder?
Have you created a share drive on your machine that the server can see? If so then you just need to refer to the path including your machine name instead of C:
Yes, it will look for the file on the SQL server itself.
If you can map a network drive to the C drive of your SQL server, then you can just copy the file over before running the bulk insert.
If you absolutely can't get any access to the server's file system, then you can look at doing something like this:
write a program that reads your text file and inserts the contents into single record in a temporary table that has a Text field, perhaps using a stored procedure
have the program execute the bcp command to export the data from the temporary table into a text file on the SQL server's local file system, to a folder that the account under which the SQL service is running has write permission
have the program run a bulk insert command specifying the path to the text file on the server
delete the text file and the temporary table

Resources