SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Excel to SQL and back again
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allanurban
Starting Member

17 Posts

Posted - 07/26/2012 :  13:18:28  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 07/26/2012 :  13:39:59  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
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

17 Posts

Posted - 07/26/2012 :  13:57:45  Show Profile  Reply with Quote
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

17 Posts

Posted - 07/27/2012 :  07:23:57  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000