Solved

Generate SQL script from Excel

Posted on 2001-07-19
4
342 Views
Last Modified: 2008-03-17
I'm comfortable with VBScript and ADO in ASP but have not worked with VBA in Excel.  What I'd like to do is generate an SQL script from an Excel document.  Each row in the document contains all of the parameters needed for each SQL statement.

Dim string_Column1, string_Column2
Dim array_OfRemainingColumnsThatActuallyContainData

'' BEGIN OUTPUT FOR EACH ROW

UPDATE table
SET Column1=string_Column1, Column2=string_Column2
WHERE UniqueID IN (array_OfRemainingColumnsThatActuallyContainData)
GO

Move.Next
0
Comment
Question by:ptpovo
4 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 100 total points
ID: 6302154
ptpovo, here is an Excel VBA example which takes one sheet and generates the SQL statements in the second sheet.

It will probably need some modification for your particular environment but you should be able to figure it out.

Public Sub CreateSQL()
    Dim lngRow As Long
    Dim lngCol As Long
    Dim strRemaining As String
    lngRow = 1
    With Sheets(1)
        Do While .Cells(lngRow, 1).Value <> ""
            lngCol = 3
            strRemaining = ""
            Do While .Cells(lngRow, lngCol).Value <> ""
                strRemaining = strRemaining & .Cells(lngRow, lngCol).Value & ", "
                lngCol = lngCol + 1
            Loop
            strRemaining = Left(strRemaining, Len(strRemaining) - 2)
            Sheets(2).Cells(lngRow, 1).Value = "UPDATE MyTable Set Column1 = '" & .Cells(lngRow, 1) & "', Column2 = '" & .Cells(lngRow, 2) & "'" & _
                " WHERE UniqueID IN (" & strRemaining & ")"
            lngRow = lngRow + 1
        Loop
    End With
End Sub
0
 

Author Comment

by:ptpovo
ID: 6343554
Sorry I've let this one sit for awhile, is this a Macro or a Module?  How do I execute it?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7156229
Hi ptpovo,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept TimCottee's comment(s) as an answer.

ptpovo, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.
==========
DanRollins -- EE database cleanup volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 7182273
Comment from expert accepted as answer

Computer101
E-E Moderator
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

679 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