How do I copy a csv file into a mssql database? - sql-server

I am looking for help to migrate my mysql over to mssql.
I have taken the database from MySQL and created a .csv file.
But from here I get stuck.
I hope you can help me to understand this.

Using Sql Server Import Wizard
1) Open Sql Server Managment Studio
2) Go Import Wizard.
3) Point the Wizard at your file (I would recommend saving the csv to excel tho).
4) Map Columns to appropriate datatypes and lengths.

Open Sql Server Management Studio; connect to your server.
Right click your database in Object Explorer, and go to Tasks -> Import Data...
In the wizard that pops up, choose a Data Source of "Flat File Source", and browse to your csv file for File Name.
For Format, choose "Delimited".
For Text Qualifier, type in one double-quote character: ". This is an important step that will let SQL read in strings properly.
Depending on your file, check or un-check the "Column names in the first data row"
For the most part, this will import your file in. You may need to tweak some of the settings under the Advanced section if your data has text > 50 characters or numeric characters, but the Preview section will show you how the file looks so far.

Related

How to generate Insert statement from PGAdmin4 Tool?

We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables into the Postgres database.
What should I do now to generate an Insert statements from the PGAdmin4 Tool similar to what SQL Studio allow us to generate an Insert statements for SQL Server? There are no options available to me. I can't use the closest one, which is to export and import the data via CSV.
I understand that you cannot import the CSV file into the actual DB as this needs to be done through ASP.NET core EF. Perhaps, you can probably create a test schema and import the CSV file into the test schema. Once you have the data imported into the test schema, you can use that to generate SQL statements using the steps below:
Right click on target table and select "Backup".
Select a file path to store the backup. You can save the file name as data.backup
Choose "Plain" as Format.
Open the tab "Options" check "Use Column Inserts".
Click the Backup-button.
Once the file gets generated you can open with Notepad++ or VSCode to get the SQL insert statements
You can use the statements generated and delete the test schema created
Here is a resource that might help you in loading data from Excel file into PostgresSQL if you still need to take this path Transfer Data from Excel to PostgreSQL

Importing tab-delimited text file - can't change the column mappings SQL Server 2008

I have a very large tab-delimited text file I'm trying to import into SQL Server 2008. Some of the field names are greater than 50 characters and when I try to change the column mappings using Management Studio, I'm unable to change the data type (default is varchar) or the size (default is 50). The Edit SQL button is also grayed out. What gives with this?
I am importing the data through the import wizard in SSMS (right click on database name, tasks, import)
Sometimes, if you run the wizard and you get an error, it creates a table. You have to go into SQL, delete the table and then start over. Otherwise, SQL Wizard does not allow you to edit the table once it has been created.

How to import excel file which is non defined way to sql server

I'm new in c# and i feel i m in trouble.
in my project i want to import excel file to sql server 2008 but i want to user choose the excel file.i search lots of source but all of them define the way of excel file in the code,but i want to user browse and user select excel file.Then other things row -row or colomn will transfer to sql server.
On the other hand i would give information on my project,it will be good for me to explain problem.First user selects excel file and it will be imported database again user selects other excel file one by one.The importent thing is how i can give way of excel file dynamically.
if you will help,i will be grateful.
Thank you...
hi it looks like you need to use Open File Dialog box. Here is the example code how to use http://msdn.microsoft.com/en-us/library/aa969773.aspx
However, remember that if you are going to deploy this application to any server you have to bear in mind that you cant use traditional ways of initiating Excel objects and reading the data in sheets. As you will not have MS office installed on server. And if you install them you will be violating the licence. Here is an example how you can do the actual import
http://www.codeproject.com/Articles/115578/Excel-to-SQL-without-JET-or-OLE-Version-2

SQL Server: how do I export entire database?

I need to export database from one server and import it into another server.
How do I export the entire database to a file, or two files mdf, ldf (either option is fine)
How do I import it into a new server using ssms?
In the instructinos frmo ponies, it says:
In the To a point in time text box,
either retain the default (Most recent
possible) or select a specific date
and time by clicking the browse
button, which opens the Point in Time
Restore dialog box. For more
information, see How to: Restore to a
Point in Time (SQL Server Management
Studio).
To specify the source and location of the backup sets to restore, click
one of the following options:
From database
Enter a database name in the list box.
I am unable to type anything in restore
https://stackoverflow.com/questions/3241108/i-backed-up-the-database-in-ssms-sql-server-how-do-i-restore-it
Using SQL Server Management Studio, you use Backup/Restore feature. The Restore process is laid out in the MSDN documentation. And here's the MSDN article for backing up a database...
Obviously, you restore to another SQL Server database instance (a "database instance" can contain multiple databases.). The version of the instance can be newer than the version the backup came from - the compatibility level will just be set accordingly.
If you want to generate a .sql file:
right click on the database in SQL Server Management Studio
select Tasks->Generate Scripts... .
In the dialog that pops up, Select All on the screen that says "Select database objects to script"
The generated .sql file can be opened again in SQL Server Management Studio and be run. If the .sql file is very large, see How do you import a large MS SQL .sql file?
If you want to export / save all data of a database into a .sql file, do this:
Right click on the database in SQL Server Management Studio
Tasks -> Generate Scripts
Script entire database and all database objects
Next
Click - Advanced
Scroll down to Types of data to script and set from Schema only -> Schema and data -> Ok
Save as script file (name it and save it where you want it)
Next
Next
Done ✔️
You can open the file now and see that all values are also included now
The best way to do this is to backup the database. This will backup to one file. Then take that file to your new server and do a restore. This should restore everything, from tables to stored procedures to foreign keys and all the data.
Also, if you just want to move a database to a new server, Detach/Attach is a quicker option. Backup/Restore is what you want if you want to keep the original database in place.

Importing a CSV file without headers into SQL 2008

I want to import a CSV with 4,8M records into a SQL 2008 table. I'm trying to do it with the Management Studio wizard but it keeps trying to recognize a header row which the CSV doesnt have. I don't find any option to skip this and although I specify the columns myself, the wizard still tries to find a header row and doesnt import anything without it.
The structure of the CSV is
"818180","25529","Dario","Pereyra","Rosario","SF","2010-09-02"
I've also tried alternatives like BULK INSERT but then I find out that with BULK INSERT I can't import files with a text qualifier.
The easiest way for a one time import would definitely be the "Import Data" function in SQL Server Management Studio. This will launch a wizard and will allow you to define where you want to import your data from - pick "Flat File Source". The next dialog allows you to browse for the file you want to import, and you can specify all sorts of things on that dialog (like the encoding of the file, what the text qualifier is - if any - and so on.
You can also select to skip any number of rows (e.g. "skip the first 5 rows"), or you can select that the first row has column names.
If your file does not have the column names in the first row, uncheck that option.
If you need to do this import over and over again, you can save all the information about the import as a Integration Services package in SQL Server (or in an external SSIS file), and you can then run that import again and again from the SQL Server Agent "Jobs" menu (enable SQL Server Agent, if you haven't already, and find the "Jobs" sub-item - you should see all your jobs under there and you can launch them again from that menu).
And if you want to, you can also launch these SSIS packages from your C# or VB.NET code - check out this CodeProject article or see Michael Entin's blog post on the topic.
Uncheck "first row has column names"
http://epicenter.geobytes.com/images/MsSqlI006.gif

Resources