2013-05-02 63 views
1

作爲存儲過程的noob,我無法弄清楚如何讓一個經典的ASP(VBScript)頁面從存儲過程返回一個值。我可以將數據寫入表格,這只是檢索我卡住的東西。從存儲過程中返回一個值經典asp

如果我使用一個非常基本的例子。這是一個存儲過程,應該在一個表中返回的記錄數:

CREATE PROCEDURE [dbo].[SP_RecordCount] 

AS 

SELECT COUNT(*) FROM MyTable 

什麼經典的ASP代碼(和什麼樣的變化對存儲過程)我需要編寫能在一個顯示該值瀏覽器嗎?一旦我明白如何獲得這樣的基本輸出,我希望(!)能夠建立在知識基礎上。

謝謝。

+1

可能的重複http://stackoverflow.com/questions/2689736/execute-stored-procedure-from- classic-asp – 2013-05-02 13:30:22

回答

2

How to call SQL Server stored procedures from ASP

<%@ LANGUAGE="VBSCRIPT" %> 
    <!--#include virtual="/ASPSAMP/SAMPLES/ADOVBS.INC"--> 
    <HTML> 
    <HEAD><TITLE>Place Document Title Here</TITLE></HEAD> 
    <BODY> 
    This first method queries the data source about the parameters 
    of the stored procedure. This is the least efficient method of calling 
    a stored procedure.<BR> 
    <% 
    Set cn = Server.CreateObject("ADODB.Connection") 
    Set cmd = Server.CreateObject("ADODB.Command") 
    cn.Open "data source name", "userid", "password" 
    Set cmd.ActiveConnection = cn 
    cmd.CommandText = "sp_test" 
    cmd.CommandType = adCmdStoredProc 
    ' Ask the server about the parameters for the stored proc 
    cmd.Parameters.Refresh 
    ' Assign a value to the 2nd parameter. 
    ' Index of 0 represents first parameter. 
    cmd.Parameters(1) = 11 
    cmd.Execute 
    %> 
    Calling via method 1<BR> 
    ReturnValue = <% Response.Write cmd.Parameters(0) %><P> 

    <!-- ************************************************************ --> 

    Method 2 declares the stored procedure, and then explicitly declares 
    the parameters.<BR> 
    <% 
    Set cn = Server.CreateObject("ADODB.Connection") 
    cn.Open "data source name", "userid", "password" 
    Set cmd = Server.CreateObject("ADODB.Command") 
    Set cmd.ActiveConnection = cn 
    cmd.CommandText = "sp_test" 
    cmd.CommandType = adCmdStoredProc 
    cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _ 
     adParamReturnValue) 
    cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _ 
     adParamInput) 
    ' Set value of Param1 of the default collection to 22 
    cmd("Param1") = 22 
    cmd.Execute 
    %> 
    Calling via method 2<BR> 
    ReturnValue = <% Response.Write cmd(0) %><P> 

    <!-- ************************************************************ --> 

    Method 3 is probably the most formal way of calling a stored procedure. 
    It uses the canocial 
    <% 
    Set cn = Server.CreateObject("ADODB.Connection") 
    cn.Open "data source name", "userid", "password" 
    Set cmd = Server.CreateObject("ADODB.Command") 
    Set cmd.ActiveConnection = cn 
    ' Define the stored procedure's inputs and outputs 
    ' Question marks act as placeholders for each parameter for the 
    ' stored procedure 
    cmd.CommandText = "{?=call sp_test(?)}" 
    ' specify parameter info 1 by 1 in the order of the question marks 
    ' specified when we defined the stored procedure 
    cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _ 
    adParamReturnValue) 
    cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _ 
    adParamInput) 
    cmd.Parameters("Param1") = 33 
    cmd.Execute 
    %> 
    Calling via method 3<BR> 
    ReturnValue = <% Response.Write cmd("RetVal") %><P> 
    </BODY> 
    </HTML> 
+0

感謝您的回覆,但我仍然遇到麻煩。我試圖修改示例2來檢索我的值,但出現以下錯誤: SQL Server的Microsoft OLE DB提供程序錯誤'80040e14' 過程SP_RecordCount沒有參數且提供了參數。 recordcount.asp,第14行 – user2335705 2013-05-02 13:50:32

+0

@ user2335705:如果您有任何代碼處理'cmd.Parameters',則需要將其刪除,因爲您的SP不期望得到任何代碼。 – shahkalpesh 2013-05-02 13:57:03

3

也許最徹底的方法是修改你的存儲過程是這樣的:

CREATE PROCEDURE [dbo].[SP_RecordCount] 
@CountResult int OUTPUT 
AS 

SELECT @CountResult = COUNT(*) FROM MyTable 

...這是服務器端的代碼來調用它,並宣讀值:

Dim cmd 
Set cmd = Server.CreateObject("ADODB.Command") 

Set cmd.ActiveConnection = .... previously opened ADO connection here 
cmd.CommandType = adCmdStoredProc 'be sure adCmdStoredProc constant is set in scope, or hardcode relevant integer instead' 
cmd.CommandText = "SP_RecordCount" 
cmd.Parameters.Refresh 

cmd.Execute 
Dim count  
count = cmd.Parameters(1)