Current location: Hot Scripts Forums » Programming Languages » ASP » SQL Stored Proc Function


SQL Stored Proc Function

Reply
  #1 (permalink)  
Old 06-10-08, 12:13 PM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Question SQL Stored Proc Function

Hi guys, i'm trying to create a function that will handle calling stored procedures and then return a recordset to the calling code.

I found out that you cant pass the parameters as a string array and then do a for each and append them. So now im trying to find another way.

Basically what i need to be able to do is type something like
asp Code:
  1. Set RS_createCustomer = Server.CreateObject("ADODB.RecordSet")
  2.    
  3.     ' Create Parameters
  4.         '.Parameters.Append .CreateParameter("ReturnVal", adInteger, adParamReturnValue)
  5.         .Parameters.Append  .CreateParameter("@CustomerEnteredBy", adVarChar, adParamInput, 100, ae_num)
  6.         .Parameters.Append  .CreateParameter("@CustomerName", adVarChar, adParamInput, 100, Customer)
  7.         .Parameters.Append  .CreateParameter("@CustomerNumber", adVarChar, adParamInput, 6, cust_num)
  8.         .Parameters.Append  .CreateParameter("@CustomerContactFirstName", adVarChar, adParamInput, 20, cust_fname)
  9.         .Parameters.Append  .CreateParameter("@CustomerContactLastName", adVarChar, adParamInput, 25, cust_lname)
  10.         .Parameters.Append  .CreateParameter("@CustomerContactEmail", adVarChar, adParamInput, 100, cust_email)
  11.         .Parameters.Append  .CreateParameter("@CustomerAddress1", adVarChar, adParamInput, 100, cust_address1)
  12.         .Parameters.Append  .CreateParameter("@CustomerAddress2", adVarChar, adParamInput, 100, cust_address2)
  13.         .Parameters.Append  .CreateParameter("@CustomerCity", adVarChar, adParamInput, 35, cust_city)
  14.         .Parameters.Append  .CreateParameter("@CustomerState", adChar, adParamInput, 2, cust_state)
  15.         .Parameters.Append  .CreateParameter("@CustomerZip", adVarChar, adParamInput, 10, cust_zip)
  16.         .Parameters.Append  .CreateParameter("@CustomerPhone1", adVarChar, adParamInput, 15, cust_phone1)
  17.         .Parameters.Append  .CreateParameter("@CustomerPhone2", adVarChar, adParamInput, 15, cust_phone2)
  18.         .Parameters.Append  .CreateParameter("@CustomerIsNew", adInteger, adParamInput, 1, cust_new)
  19.         .Parameters.Append  .CreateParameter("@CustomerNotes", adLongVarChar, adParamInput, 2, cust_notes)
  20.    
  21.    
  22.     RS_createCustomer = execQuery("CreateCustomerRecord", Cmd_createCustomer)

And then the function will handle using the parameters and then function will use the parameters sent to it. heres the function i have so far

asp Code:
  1. '===================================================================
  2.     ' SQL Wrapper
  3.     '===================================================================
  4.     Function execQuery(ProcedureName, Params)      
  5.         ' Create our Customer row in the DB and fill it in
  6.         Set DataConn_execQuery = Server.CreateObject("ADODB.Connection")
  7.         Set Cmd_execQuery = Server.CreateObject("ADODB.Command")
  8.         Set RS_execQuery = Server.CreateObject("ADODB.RecordSet")
  9.         Set prm = Server.CreateObject("ADODB.Parameter")
  10.            
  11.         DataConn_execQuery.Open MAIN_DATABASE_CONNECTION_STRING
  12.         DataConn_execQuery.CommandTimeout=10
  13.        
  14.         With Cmd_execQuery
  15.             .ActiveConnection = DataConn_execQuery
  16.             .CommandText = ProcedureName
  17.             .CommandType = 4                       
  18.         End With
  19.        
  20.         ' Execute the query and set the result set variable
  21.         RS_execQuery = Cmd_execQuery.Execute
  22.        
  23.         'Close objects
  24.         Set Cmd_execQuery = Nothing
  25.         DataConn_execQuery.Close
  26.         Set DataConn_execQuery = Nothing
  27.        
  28.         ' Return our result
  29.         execQuery = RS_execQuery
  30.     End Function

I'm at a loss as to how to do this, any ideas?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-10-08, 01:41 PM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
this is a revised semi working version
asp Code:
  1. ' Create our Customer row in the DB and fill it in
  2.     Set RS_createCustomer = Server.CreateObject("ADODB.RecordSet")
  3.     Dim ParamsArray(15)
  4.    
  5.     ' Create Parameters
  6.     ParamsArray(0) = """@CustomerEnteredBy"", " & adVarChar & ", " & adParamInput & ", 100, " & ae_num
  7.     ParamsArray(1) = """@CustomerName"", " & adVarChar & ", " & adParamInput & ", 100, " & Customer
  8.     ParamsArray(2) = """@CustomerNumber"", " & adVarChar & ", " & adParamInput & ", 6, " & cust_num
  9.     ParamsArray(3) = """@CustomerContactFirstName"", " & adVarChar & ", " & adParamInput & ", 20, " & cust_fname
  10.     ParamsArray(4) = """@CustomerContactLastName"", " & adVarChar & ", " & adParamInput & ", 25, " & cust_lname
  11.     ParamsArray(5) = """@CustomerContactEmail"", " & adVarChar & ", " & adParamInput & ", 100, " & cust_email
  12.     ParamsArray(6) = """@CustomerAddress1"", " & adVarChar & ", " & adParamInput & ", 100, " & cust_address1
  13.     ParamsArray(7) = """@CustomerAddress2"", " & adVarChar & ", " & adParamInput & ", 100, " & cust_address2
  14.     ParamsArray(8) = """@CustomerCity"", " & adVarChar & ", " & adParamInput & ", 35, " & cust_city
  15.     ParamsArray(9) = """@CustomerState"", " & adChar & ", " & adParamInput & ", 2, " & cust_state
  16.     ParamsArray(10) = """@CustomerZip"", " & adVarChar & ", " & adParamInput & ", 10, " & cust_zip
  17.     ParamsArray(11) = """@CustomerPhone1"", " & adVarChar & ", " & adParamInput & ", 15, " & cust_phone1
  18.     ParamsArray(12) = """@CustomerPhone2"", " & adVarChar & ", " & adParamInput & ", 15, " & cust_phone2
  19.     ParamsArray(13) = """@CustomerIsNew"", " & adInteger & ", " & adParamInput & ", 1, " & cust_new
  20.     ParamsArray(14) = """@CustomerNotes"", " & adLongVarChar & ", " & adParamInput & ", 2, " & cust_notes
  21.    
  22.     ' Execute our stored procedure
  23.     RS_createCustomer = execQuery("CreateCustomerRecord", ParamsArray)
asp Code:
  1. '===================================================================
  2.     ' SQL Wrapper
  3.     '===================================================================
  4.     Function execQuery(ProcedureName, Params)      
  5.         ' Create our Customer row in the DB and fill it in
  6.         Set DataConn_execQuery = Server.CreateObject("ADODB.Connection")
  7.         Set Cmd_execQuery = Server.CreateObject("ADODB.Command")
  8.         Set RS_execQuery = Server.CreateObject("ADODB.RecordSet")
  9.         Set prm = Server.CreateObject("ADODB.Parameter")
  10.            
  11.         DataConn_execQuery.Open MAIN_DATABASE_CONNECTION_STRING
  12.         DataConn_execQuery.CommandTimeout=10
  13.        
  14.         With Cmd_execQuery
  15.             .ActiveConnection = DataConn_execQuery
  16.             .CommandText = ProcedureName
  17.             .CommandType = 4                       
  18.         End With
  19.        
  20.         ' Create Parameters
  21.         if UBound(Params) > 0 then
  22.             for each key in Params
  23.                 response.write key & "<br />"
  24.                 key = split(key, ",")
  25.                
  26.                 if key(4) <> "" then
  27.                     prmValue = key(4)
  28.                 else
  29.                     prmValue = ""
  30.                 end if
  31.                    
  32.                 With prm
  33.                     .Name = key(0)
  34.                     .Type = key(1)
  35.                     .Direction = key(2)
  36.                     .Size = key(3)
  37.                     .Value = prmValue
  38.                 End With
  39.                
  40.                 Cmd_execQuery.Parameters.Append prm
  41.             next
  42.         end if
  43.        
  44.         ' Execute the query and set the result set variable
  45.         RS_execQuery = Cmd_execQuery.Execute
  46.        
  47.         'Close objects
  48.         Set Cmd_execQuery = Nothing
  49.         DataConn_execQuery.Close
  50.         Set DataConn_execQuery = Nothing
  51.        
  52.         ' Return our result
  53.         execQuery = RS_execQuery
  54.     End Function

However, im getting
Quote:
ADODB.Parameter error '800a0c93'

Operation is not allowed in this context.

/byob08_dev2/includes/functions.asp, line 294
which refers to
Quote:
.Name = key(0)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-10-08, 02:28 PM
spyke01 spyke01 is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
nm, heres the solution for anyone else who needs it
asp Code:
  1. '===================================================================
  2.     ' handles conversion for execQuery Function
  3.     '===================================================================
  4.     Function execQueryConvertString(iType, value)
  5.         ' varChar
  6.         if iType = 200 then
  7.             if value = "" then
  8.                 retVal = trim(CStr(""))
  9.             else
  10.                 retVal = trim(CStr(value))
  11.             end if
  12.         ' Char
  13.         elseif iType = 129 then
  14.             if value = "" then
  15.                 retVal = trim(CStr(""))
  16.             else
  17.                 retVal = trim(CStr(value))
  18.             end if   
  19.         ' Int
  20.         elseif iType = 3 then
  21.             if value = "" then
  22.                 retVal = trim(CInt(""))
  23.             else
  24.                 retVal = trim(CInt(value))
  25.             end if 
  26.         ' LongVarChar
  27.         elseif iType = 201 then
  28.             if value = "" then
  29.                 retVal = trim(CStr(""))
  30.             else
  31.                 retVal = trim(CStr(value))
  32.             end if   
  33.         end if
  34.        
  35.         execQueryConvertString = retVal
  36.     End Function
  37.    
  38.     '===================================================================
  39.     ' SQL Wrapper
  40.     '===================================================================
  41.     Function execQuery(ProcedureName, Params)      
  42.         ' Create our Customer row in the DB and fill it in
  43.         retVal = 0
  44.         Set DataConn_execQuery = Server.CreateObject("ADODB.Connection")
  45.         Set Cmd_execQuery = Server.CreateObject("ADODB.Command")
  46.         Set RS_execQuery = Server.CreateObject("ADODB.RecordSet")
  47.            
  48.         DataConn_execQuery.Open MAIN_DATABASE_CONNECTION_STRING
  49.         DataConn_execQuery.CommandTimeout=10
  50.        
  51.         With Cmd_execQuery
  52.             .ActiveConnection = DataConn_execQuery
  53.             .CommandText = ProcedureName
  54.             .CommandType = 4   
  55.            
  56.             .Parameters.Append .CreateParameter ("RETURN_VALUE", adInteger, adParamReturnValue)     
  57.         End With
  58.        
  59.         ' Create Parameters
  60.         if isArray(Params) then
  61.             for each key in Params
  62.                 key = split(key, ",")
  63.                
  64.                 if UBound(key) <> -1  then
  65.                     if UBound(key) > 3 then
  66.                         if key(4) <> "" then
  67.                             prmValue = execQueryConvertString(key(1), key(4))
  68.                         else
  69.                             prmValue = execQueryConvertString(key(1), "")
  70.                         end if     
  71.                     else
  72.                         prmValue = execQueryConvertString(key(1), "")
  73.                     end if
  74.                    
  75.                     Set prm = Server.CreateObject("ADODB.Parameter")
  76.                     With prm
  77.                         .Name = key(0)
  78.                         .Type = key(1)
  79.                         .Direction = key(2)
  80.                         .Size = key(3)
  81.                         .Value = prmValue
  82.                     End With
  83.                    
  84.                     Cmd_execQuery.Parameters.Append prm
  85.                 end if
  86.             next
  87.         end if
  88.        
  89.         ' Execute the query and set the result set variable
  90.         RS_execQuery = Cmd_execQuery.Execute
  91.        
  92.         ' Assign result to retVal      
  93.         If CInt(Cmd_execQuery.Parameters("RETURN_VALUE")) <> 0 Then
  94.             'Do Error stuff here
  95.             retVal = "Error"
  96.         End If
  97.        
  98.         'Close objects
  99.         Set Cmd_execQuery = Nothing
  100.         DataConn_execQuery.Close
  101.         Set DataConn_execQuery = Nothing
  102.        
  103.         ' Return our result
  104.         execQuery = retVal
  105.     End Function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[2005] StartPosition for CommonDialogs? tim8w Windows .NET Programming 10 01-08-09 04:39 AM
ASP upload prob minority ASP 1 06-27-05 09:35 AM
PHP Error Fairnie PHP 8 06-26-04 08:15 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 10:50 AM
change my field in this example sal21 ASP 3 07-14-03 03:49 AM


All times are GMT -5. The time now is 12:59 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.