如何使用asp classic调用存储过程?

时间:2013-05-09 17:03:16

标签: sql stored-procedures asp-classic

我正在使用sql server和asp classic,我目前正在调用这样的查询:

newHireSQL = "select * from NewHire where Archived = 0 order by HireID desc"

Set rsGetHireID = Server.CreateObject("ADODB.Recordset")
rsGetHireID.Open newHireSQL,ConnectionString,adOpenStatic

NumOfHireID = rsGetHireID.RecordCount

但是我没有在这里使用查询语句,而是想调用名为dbo.sp_selectNewHireSQL的存储过程。我怎么能这样做?

由于

编辑:

我试过这个

Dim Conn
SET Conn = Server.CreateObject("ADODB.Connection")
SET rsGetHireID = Server.CreateObject("ADODB.RecordSet")
Conn.Open ConnectionString
set rsGetHireID=Conn.Execute("Exec sp_selectNewHireSQL")

NumOfHireID = rsGetHireID.RecordCount
Response.Write (NumOfHireID)

但我得到的记录数为-1值。

4 个答案:

答案 0 :(得分:3)

只需使用execexecute声明:

Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open ConnectionString
Conn.Execute "Exec sp_selectNewHireSQL"

参考:http://msdn.microsoft.com/en-us/library/ms188332(v=sql.90).aspx

答案 1 :(得分:2)

要使RecordCount工作,您需要使用正确的光标:

Set rsGetHireID = Server.CreateObject("ADODB.RecordSet")
Conn.Open ConnectionString
rsGetHireID.CursorLocation = 3 'adUseClient
rsGetHireID.Open "Exec sp_selectNewHireSQL", Conn
NumOfHireID = rsGetHireID.RecordCount

答案 2 :(得分:0)

不要使用ADODB.Connection,而是尝试使用ADODB.Command,而不是这样:

        Set objCommand = Server.CreateObject("ADODB.Command")
        objCommand.ActiveConnection = ConnectionString
        objCommand.CommandText = "dbo.sp_selectNewHireSQL"
        objCommand.CommandType = adCmdStoredProc ' you have to include adovbs.inc file or you can use 4

        Set rsGetHireID = objCommand.Execute()
        NumOfHireID = rsGetHireID.RecordCount
        Response.Write (NumOfHireID)    

答案 3 :(得分:-3)

<%


    dim db_conn

   db_conn = "Provider=SQLOLEDB.1;Server=server name;Database=dbname;Uid=sa; Pwd=123;"
set conn = server.createobject("adodb.connection")
set Cmd = Server.CreateObject("ADODB.Command")
'-------------------------------------------------------
conn.open (db_conn)
'-------------------------------------------------------
set rs = Server.CreateObject("ADODB.RecordSet")  
sSQL = "EXEC sp_countrylist @countryname ='" & countryname & "'"
set rs = conn.execute(sSQL) 

if (rs.bof and rs.eof) then
    response.Write "<span class=""error"">No Record Found</span>"
    response.End
end if %>