Solved

Generate insert/update script from Excel sheet

Posted on 2013-11-26
3
533 Views
Last Modified: 2013-11-27
Hi Experts,

   Please do refer the attache excel sheets which contain two sample tables.  In my database I've two respective tables as given below.
CREATE TABLE CONFIGURATION
(
Name VARCHAR(100) NOT NULL,
NType  VARCHAR(100) NOT NULL,
Data	VARCHAR(100) NOT NULL,
Datatype VARCHAR(100) NOT NULL,
DataVal VARCHAR(100) NOT NULL,
Remarks VARCHAR(100)  NULL) 

GO
ALTER TABLE CONFIGURATION
ADD CONSTRAINT PK_NTD PRIMARY KEY CLUSTERED (Name, NType, Data);


GO

CREATE TABLE Settings
(
ID INT NOT NULL,
NKey VARCHAR(100) NOT NULL,
Value VARCHAR(100) NOT NULL,
Remarks VARCHAR(100)  NULL
)
GO
ALTER TABLE Settings
ADD CONSTRAINT PK_IDNkey PRIMARY KEY CLUSTERED (ID, NKey);
GO

Open in new window


I would like to read the excel sheet and generate sql script for each table based on the table's PK.

Say for example, for the  configuration table the expected result looks like given below.


IF EXISTS (SELECT 1 FROM CONFIGURATION WHERE Name= 'Auditing'  AND NType = 'Audit'  AND  Data= 'AuditDt')
BEGIN
 UPDATE CONFIGURATION
 SET 
 Datatype = 'D',	DataVal	= '11/26/2013', Remarks  = 'Audit Date'
 WHERE  Name= 'Auditing'  AND NType = 'Audit'  AND  Data= 'AuditDt'

END
ELSE
BEGIN
 INSERT CONFIGURATION ([Name],[NType],[Data],[Datatype],[DataVal],[Remarks])
 VALUES ('Auditing','Audit'	,'AuditDt','D','11/26/2013','Audit Date')

END

Open in new window



Please guide me how to achieve this? It would be great if you share the coding or existing tool?

Thanks in advance.
SampleExcel.xlsx
0
Comment
Question by:Easwaran Paramasivam
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
unknown_routine earned 500 total points
ID: 39677492
.NET(C#) lets you read Excel easily:

string MyCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\temp\test.xls;Extended Properties='Excel 8.0;HDR=Yes;'"
using(OleDbConnection connection = new OleDbConnection(MyCon ))
{
    connection.Open();
    OleDbCommand command = new OleDbCommand("select * from [Configuration$]", connection)
    using(OleDbDataReader dr = command.ExecuteReader())
    {
         while(dr.Read())
         {
            'rest of your code....
            ...

         }
    }
}
0
 
LVL 16

Author Comment

by:Easwaran Paramasivam
ID: 39679936
Thanks. It would be great if you share sample application.
0
 
LVL 16

Author Closing Comment

by:Easwaran Paramasivam
ID: 39682581
Thanks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question