Question

I would like to make a copy of a table in the same database of SQL Server 2008.

Then, replace the contents of the new table with the another txt file. The txt file has the same column headers as the new table.

I know how to do it manually to create a new table.

But, the txt file is very large and I would like to do it by a script.

Update

I have created a new table with the same headers but how to load the data from a .txt file to the table ?

When I use bulk insert:

BULK INSERT [dbo].[my_new_table]
FROM '\\ServerName\myPath\myFile.csv'

I got error:

Cannot bulk load because the file "\ServerName\myPath\myFile.csv" could not be opened.
Operating system error code 3(The system cannot find the path specified.)

I have copied the file to local C:, but, the same error.

Now, the UNC path does not work. The SQL Server cannot see the local drive.

Any help would be appreciated.

Was it helpful?

Solution

Here's a quick way to create the new table:

SELECT TOP 0 *
INTO [NewTableName]
FROM [OldTableName];

Note that this only covers column names and types, and ignores features like triggers, indexes, constraints, etc. If you need these additional items, you can do that from Management Studio by right-clicking on the original table and choosing Script Table As -> Create to -> New Query Editor Window. In the query window created, just change the table name and run the query.

For importing the text data, there's too much left unsaid to give a good response, but the BULK INSERT statement may be worth investigating.


Looking at your edit, the account running the sql server service needs to have access to that file share. You'll have best luck if you copy the file locally first, and if you use an xml format file telling sql server about the fields in your text file. Make sure to read and understand the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top