Really basic SQL Server export question

Status
Not open for further replies.

Dragondazd

Ars Tribunus Angusticlavius
9,190
Ok, this is going to be one of the dumber questions you see here.

I am trying to write a little script that will export stuff from SQL Server to an existing Excel 2013 workbook into multiple sheets. I can't use the menu system because I am asked to make a lot of really dumbly repetitive tables as one-offs (think 10 workbooks A-J with 50 sheets, one table each for years 1950 through 1999 all formatted the same so a robot posing as a human can browse through reams of data at their leisure (or not)) so I am trying to minimize my pain.

I really have a hard time figuring out the "getting started" part of any sort of programming activity, which is where one tends not to have the vocabulary to look for what one is missing. 99% of my SQL experience is select statements so I don't know where to do the thing.

I see that I can do things like

insert into OPENROWSET(.....

and

create procedure proc_generate_excel_with_columns(.....

but where do you do these things at in SQL Server (2008)? Can you just stick these at the end of a query and hit execute?

P.S. how do you pick a specific sheet?
 

Dragondazd

Ars Tribunus Angusticlavius
9,190
Nope, don't know what SSIS is. Thanks for bringing up that issue. I guess I'll create fresh workbooks and format them after.

I am pretty confident I can figure out how to do the things once I know what the names of the things are and where the things are put. It can't be worse than ODBC...

I guess another way to put that is "Where do you write the script that takes a query output and puts it in Excel, and what do you call that export process?"
 

Dragondazd

Ars Tribunus Angusticlavius
9,190
....well this has, absurdly, been harder than I expected. I can use the export wizard and make an SSIS package but I don't have the tool installed to edit the SSIS.

My current solution is to make a big query and ask the VBA guy to make me a little script to make the new spreadsheets. I also got a suggestion to use odbc within Excel. That should remove most of the compatibility issues heh.
 

Sentipede

Seniorius Lurkius
10
Use VBA in Excel. Below is the VBA code to pull from an Access database. You'd need to alter it to work with SQL Server. Should not be hard.

In the example, the sheet is named "Machines". There is a table (list object) on the sheet named "MachinesTable" with three columns: city, state, branch num.

Code:
Sub PullData()
    Application.ScreenUpdating = False
    
    Dim MachinesWs As Worksheet
    Dim Db As DAO.Database
    Dim Rs As DAO.Recordset
    Dim rCell As Range
    
    Set Db = DAO.dbengine.OpenDatabase("\\domain\thepath\yourDB.accdb")     
    Set MachinesWs = ThisWorkbook.Worksheets("Machines")
    Set Rs = Db.Openrecordset("SELECT city, state, branchnumber FROM schedule", dbreadonly)

    targetRow = 2

    Do While Not Rs.EOF
        MachinesWs.Cells(targetRow, MachinesWs.Range("MachinesTable[City]").Column) = Rs.Fields("city")
        MachinesWs.Cells(targetRow, MachinesWs.Range("MachinesTable[State]").Column) = Rs.Fields("state")
        MachinesWs.Cells(targetRow, MachinesWs.Range("MachinesTable[Branch Num]").Column) = Rs.Fields("branchnumber")
        
        Rs.MoveNext
        targetRow = targetRow + 1
    Loop

    MachinesWs.Cells.EntireColumn.AutoFit
    
    Rs.Close
    Set Rs = Nothing
    Db.Close
    Set Db = Nothing
End Sub
 
Status
Not open for further replies.