Thursday, 26 April 2012

Excel Short cut to generate MS SQL query from excel data

Just few days back I use to receive lot of raw data in excel to migrate into MS SQL server. I used some features in MS SQL management studio to upload the data from excel to target tables. It was so problematic that I could not able to migrate all the data from excel to MS SQL. Because some data got truncated, data type excel found to be mismatching, transferred data are not placed in correct target column in the target table, etc.   


To overcome this problem, I formatted the input Excel document as below,
  1. Arrange column in the excel document as that of the columns in the SQL table.
  2. Modify the data types of the columns in the excel document as that of columns in SQL table using some of the predefined functions available in excel. Some time it is not necessary to use the predefined function to format columns.
  3. Place the insert query [insert into <table_name> values (”] in one of the column in excel and drag it till the end where there is no record for that row.
  4. Similarly place the ['','] & ['')] in two separate columns and you should use two single codes in the beginning then only excel will recognize as a single quotes.
  5. Finally frame the formula in the order of columns to form a sql query.
  6. Go to the Function (fx) and type “=”
  7. Hold the control key (block plus symbol appears) and press on the cell and type “&”. Report the process until you form the SQL query.
  8. The formula appears as this “=A1&B1&C1&D1&F1&G1” Or else you predefined concatenate function as this “=CONCATENATE(A1,B1,C1,D1,F1,G1)”
  9. Drag the formula column till the end where there is no record for that row. Copy all formula cells and paste in sql management studio and execute it.
This is found be more helpful and less time consuming for preparing the query.

Please post me your queries if you have doubt in this post.

No comments:

Post a Comment