Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Excel to SQL and back again

Author  Topic 

allanurban
Starting Member

21 Posts

Posted - 2012-07-26 : 13:18:28
Hello all

I have recently started on a new project. The goal is to insert some data generated in Excel into a table in a SQL database. Once that is done, I need a function that, based on an input of mine, returns some of that data to excel.

My only problem is that I have no experience with SQL. Well, first things first. Getting the data into the database.

I have created a table in a SQL database. I contains of 651 columns. The first one is named 'stat' and is the primary key. The remaining columns are named 'T1', 'T2', 'T3' ... 'T650'. It holds about 240 rows.

I can upload data from excel with the following:

Sub SetLevel(stat As String, value1 As Double, value2 As Double, value3 As Double)

' Dim rst As ADODB.Recordset
Dim rst As Object
Set rst = CreateObject("ADODB.Recordset")

Dim Cmd As New ADODB.Command
With Cmd
.ActiveConnection = oConn 'is set before sub is called
.CommandText = "dbo.SetLevels"
.CommandType = adCmdStoredProc
.Parameters.Refresh
.Parameters("@stat") = stat
.Parameters("@level1") = value1
.Parameters("@level2") = value2
.Parameters("@level3") = value3
Set rst = .Execute
End With

End Sub

----------

ALTER PROCEDURE [dbo].[SetLevels]
@stat varchar(64),
@Level1 float,
@level2 float,
@level3 float
AS
BEGIN

SET NOCOUNT ON;

UPDATE dbo.Levels
SET T1 = @level1,
T2 = @level2,
T3 = @level3
WHERE stat = @stat
;
END

It works great for the first three columns, and surely I could just add another 648 input variables to the stored procedure and it would work just as well. I'm guessing however, that there is a much easier solution to the problem. I just don't know of it, and the examples found with google has not pushed me in the right direction.

Anyone able to help?

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-26 : 13:39:59
Depends on what your environment is like. Do yoou want to run this from excel or use a spreadsheet as input?
The easiest way is to generate insert statements in excel, copy them to a query window and run them

How you generate the insert statements is up to you, you could do it in script looping through the columns (then you might as well put the execute there as well) and add a button to run it.
I tend to do it in a formula in each row - you would probably generate the formula for that many columns.

You might want to use ssis if it is something that is going to be delivered regularly.
Could also use a linked server to import.
Could export to a csv then import using bulk insert

Lots of ways

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

allanurban
Starting Member

21 Posts

Posted - 2012-07-26 : 13:57:45
quote:
Originally posted by nigelrivett

Depends on what your environment is like. Do yoou want to run this from excel or use a spreadsheet as input?
The easiest way is to generate insert statements in excel, copy them to a query window and run them

How you generate the insert statements is up to you, you could do it in script looping through the columns (then you might as well put the execute there as well) and add a button to run it.
I tend to do it in a formula in each row - you would probably generate the formula for that many columns.

You might want to use ssis if it is something that is going to be delivered regularly.
Could also use a linked server to import.
Could export to a csv then import using bulk insert

Lots of ways

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.



Basically I am doing a bunch of calculations once a day. 650 for each stat to be exact. I would prefer a solution where I can just call a macro from Excel and that would take care of it. I guess the insert statement is what I need. How should it look like, and how should the store procedure be?
Go to Top of Page

allanurban
Starting Member

21 Posts

Posted - 2012-07-27 : 07:23:57
Well I guess my construction of the tabel was crazy in the first place. I have changed it to 3 columns, "stat", "date" and "level". That works way better.
Go to Top of Page
   

- Advertisement -