Execute SQL Proc

  • Hi Guys,

    I need to execute sql server proc from MS Access or Excel... the proc has couple of parameters, but do not have any data comming out. Can we do that? And how we can do it so that user can enter values for parameter and execute the proc.

    Thanks in advance for the help.

    Laura

  • Cannot be done that I know of. you can use a simple web page that can do this. You can always use SQL Agent and schedule the sproc.

    Andrew SQLDBA

  • Laura_SqlNovice (12/3/2013)


    Hi Guys,

    I need to execute sql server proc from MS Access or Excel... the proc has couple of parameters, but do not have any data comming out. Can we do that? And how we can do it so that user can enter values for parameter and execute the proc.

    Thanks in advance for the help.

    Laura

    You would execute it using VBA. I would think you would use textboxes or some other form of input for the parameter values.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • http://datapigtechnologies.com/blog/index.php/running-a-sql-stored-procedure-from-excel/

    I've used this technique before for some quick and dirty "reports"...

  • Sean Lange (12/3/2013)


    Laura_SqlNovice (12/3/2013)


    Hi Guys,

    I need to execute sql server proc from MS Access or Excel... the proc has couple of parameters, but do not have any data comming out. Can we do that? And how we can do it so that user can enter values for parameter and execute the proc.

    Thanks in advance for the help.

    Laura

    You would execute it using VBA. I would think you would use textboxes or some other form of input for the parameter values.

    Agreed. You'll need to add a reference to the Microsoft ActiveX Data Objects library and then you use the ADODB.Connection data type, open your connection and fire your procedures.

    As a word of caution, Excel files tend to spread through network shares, email, USB drives, etc. Be careful of the connection string you define in your Excel file. You don't want to have a server/username/password combination spread throughout the world and leave your database wide open. If it were me, I'd make the user enter their connection information into a form (and don't let them save it) and then try to connect using what they entered. This will also help you restrict who can use the Excel file to run the procedure by granting execute on that procedure to those users who are authorized to use the utility.

  • Is it possible using ACCESS forms or reports? Thanks guys for the help.

  • Laura_SqlNovice (12/3/2013)


    Is it possible using ACCESS forms or reports? Thanks guys for the help.

    Access is definitely not my forte and my knowledge is more than a little outdated on it, but can't you use ADO in the event procedures behind the form? It used to be done using DAO when the form loaded. Sorry, but it's been a long time since I've touched Access.

  • Laura_SqlNovice (12/3/2013)


    Is it possible using ACCESS forms or reports? Thanks guys for the help.

    Yes you can do this from a form for sure. I suspect you could just as easily have a report call a stored proc, not sure how you would get the parameter values though. I haven't used ACCESS as a "programming tool" for a LONG LONG time.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • It's quite possible to use Access to call a SQL Server procedure; you'll need to have a link to your SQL Server established in the Access file first, however. If you have one (and if not, I can provide some assistance in doing so), create a Pass-Through query; it's one of the options grouped in with the rest of the types in the Query section.

    Once you make the pass-through query, just enter the call to the procedure as you would in SQL Server. So, EXEC (Database).(Schema).(Procname), and put a call to the query where it's needed, either in the VBA coding for a button or form event, and that should handle it.

    My memory for the process is a bit fuzzy, since I haven't had to do this in quite awhile, so if I've slipped up somewhere, let me know and I'll try to correct things.

    - 😀

  • I've recently done this for a project I was working on, all you need is a reference adding to ActiveX Data Objects

    Public Function ExecuteSP(strStoredProcedure As String) As Long

    Dim objCmd As ADODB.Command

    Dim lngRecords As Long

    Set objCmd = New ADODB.Command

    objCmd.ActiveConnection = "PROVIDER=SQLOLEDB;DRIVER={SQL Server};SERVER=" ' Complete your connection string here

    objCmd.CommandType = adCmdStoredProc

    objCmd.CommandText = strStoredProcedure

    Call objCmd.Execute(lngRecords)

    ExecuteSP = lngRecords

    Set objCmd = Nothing

    End Function

    This is a generic function for calling SPs without parameters that may return an affected record count, parameters could be added as follows

    objCmd.Parameters.Append objCmd.CreateParameter("@ParamName", adInteger, adParamInput, value:=1)

  • I was tying to use batch file to execute proc, but did't work and I was not able to see error...

    here is the script I used:

    I am trying to use batch file to execute proc... never did it before

    @echo off

    SET /P path=Enter the location:

    SET /P oldName=Enter the name:

    SET /P newName=Enter the new name:

    sqlcmd -E -Q "USE [TESTDB] EXEC sp_rename @path =N'%path%', @oldName=%oldName%", @newName=%newName%"

    SET path=

    SET oldName=

    SET newName=

    Is this the correct code? It doesn't pass and I cannot see error....

  • Sorry it works I had last " in the wrong place...

    Now I need to add a line to write error to a text file...

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply