2

I have a SQL database which consists of several tables like table 1 table 2 table 3 and so on

I have an excel sheet which contains about 17000 rows of data. The column headers of the excel sheet have been edited such that they match the column headers of the table (say table3) into which I want to import my excel data.

For example, in excel sheet, the data is like,

Name   Age
2      23

Table 3 is also of the same form, like

Name  Age
1      23

I would like to know, like how do I add the data in the above excel sheet to this table 3 in SQL database. Can someone give me some idea or instructions???

2
  • import excel data all at once. Commented Dec 19, 2012 at 8:12
  • 1
    Take note, some data types can mess around, so i would suggest adding it to som temp table, and then import to your data tables Commented Dec 19, 2012 at 8:18

4 Answers 4

1

There is an Import / Export wizard included with SQL Server. It's pretty easy and intuitive to use, and as long as your data types match you should not have any problems.

Sign up to request clarification or add additional context in comments.

Comments

1

I understand that you only want to add data to a specific table at a time.

Not sure which coding language you are using, but nevertheless, in short:

  • Read the CSV file line by line with using a stream reader.
  • Parse each line to some data type ( StringList or vector< String > etc. ). Delimiter will be ',' and quote character will be ' " '.
  • Construct a 'value string' from the parsed data ( needed for SQL command ). What I mean by that: The value string should be of the format "rowid, integer, integer , 'text', 'text', ... ". The amount of 'entries' in this string should be equal to the number of columns in your table ( including the primary key of the table ).

    Entries corresponding to SQL columns with value-type INTEGER need not be surrounded by single quotes. For SQL columns with value-type TEXT, TINYTEXT etc. they must be surrounded by single quotes, as shown above. I do not believe it is even required to specify the rowid, you can just replace that with a '?', giving you:

     "?, integer, 'text', 'text', integer, ..." 
    

    or whatever the case may be.

  • Then use a SQL INSERT INTO command to insert each row into the required table, something like the following:

    String sqlStatement = "INSERT INTO TableName VALUES ( " + valueString + " )";
    

In your case something like:

    String sqlStatement = "INSERT INTO Table3 VALUES ( ?, 2, 23, ... )";
  • Then execute the statement ( c++, SQLite, Note the sqlite3* should not really be declared here ):

    int      rc;
    sqlite3* db;
    char*    zErrMsg = 0;
    rc = sqlite3_exec(db, sqlStatement, NULL, NULL, &zErrMsg);
    if ( rc != SQLITE_OK )
       return false;
    return true;
    
  • Repeat for each row of csv data. Use transactions and binding to increase performance.

Comments

0

Which Database are you using? as the method varies dependant on the database engine and tools available for it. As stated by SWeko, in SQL Server Management Studio you can indeed import Excel files directly, there are tools which are more generic, like DB visualiser, which may allow you to do the same into more databases, or you can use sql to generate the sql insert code for you by witing a template and using vlookup to define the data, then you would just run all the insert commands to add the data to the DB.

Comments

0

You must see this: http://support.microsoft.com/kb/321686 There is a almost same question :http://stackoverflow.com/questions/5141521/how-to-insert-data-from-excel-sheet-to-sql-server-2005

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.