Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Some simple questions...

All this time I have been writing queries in ASP pages to access data in my d/b (2-tier architecture). The question though is: If I fill my MS SQL database with stored procedures to do the same work, how do I call directly a SP from my ASP file?

E.g. I have three SPs

SP_delete_user (user_id) -> Returns 1/0
SP_add_user (name, surname, tel_no) -> Returns user_id
SP_update_user_details (name, surname, tel_no) -> Returns 1/0

how would I call them directly from my ASP ( this is again 2-tier architecture)...
1 Solution
Follow what is described here

If you happen to get errors/problems let us know

Also see for other examples

Wouter BoevinkCommented:
include adovbs.inc or
<!--METADATA NAME="Microsoft ActiveX Data Objects 2.5 Library" TYPE="TypeLib" UUID="{00000205-0000-0010-8000-00AA006D2EA4}"-->

Function SP_add_user(strname, strsurname, strtel_no)
     Dim Conn          : Set Conn = server.CreateObject("ADODB.Connection")
     Dim cmd               : Set cmd = Server.CreateObject("ADODB.Command")

     Conn.ConnectionString = Application("Cnn_ConnectionString")
     Conn.CursorLocation = adUseClient
     Conn.Mode = adModeReadWrite

     cmd.ActiveConnection = Conn
     cmd.CommandType = adCmdStoredProc
     cmd.CommandText = "SP_add_user"

     cmd.Parameters.Append cmd.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue, 0, RETURN_VALUE)
     cmd.Parameters.Append cmd.CreateParameter("@name", adVarChar, adParamInput, 20, strname)
     cmd.Parameters.Append cmd.CreateParameter("@surname", adVarChar, adParamInput, 20, strsurname)
     cmd.Parameters.Append cmd.CreateParameter("@tel_no", adVarChar, adParamInput, 16, strtel_no)


     SP_add_user = cmd.Parameters("RETURN_VALUE").Value

     Set cmd = Nothing
     'Set Conn = Nothing
End Function

Dim ReturnValue
ReturnValue = SP_add_user("test","test","1234567890")
wow, that looks much harder than anything i've ever done...

i've always just written a SQL string that calls the SP, and everything else on the page is the same as if it were a normal SQL string...

here is an example of a very simple one..

SQL = SQL & "@ACTION = 'del', "

a little bigger:

SQL1 = SQL1 & "@ACTION = 'upd', "
SQL1 = SQL1 & "@QUESTION_ID = " & QUESTION_ID & ", "
SQL1 = SQL1 & "@QUESTION = '" & QUESTION & "', "
SQL1 = SQL1 & "@SHOW_RESULTS = " & results & ", "
SQL1 = SQL1 & "@TYPE_ID = " & TYPE_ID

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Wouter BoevinkCommented:
I know it seems a lot of code, when it can be accomplished in a few lines.

Doing it this way, it's always correct you always have the right parameters and types. It's much faster.

I wrote a script which takes a stored procedure and generates the function.
Might give this a try, I use it

<%dim conn
dim rs
set conn= Server.CreateObject("ADODB.Connection")
conn.Open "Provider=SQLOLEDB;Initial Catalog=Rettips;Data Source=GP_NT3;User ID=sa;Password=;"
set rs = conn.execute("exec sp_mtabs @mt ='"&mt&"'")
I don't know if those are the real names of your SPs, but you shouldn't name custom stored procedures starting with "sp_" because that's the prefix for SQL Server system stored procedures.  SQL Server thinks it's a system stored procedure and attempts to find it in the master db first.  The time it takes SQL Server to search through the master db (instead of going directly to your SP) can potentially cause a decrease in performance.
dinosaurusAuthor Commented:
I think this is the simplest way. As far as the naming conventions are concerned, no I am not using SP_ ... but thanks for this piece of very useful advice. Thank you all.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now