简体   繁体   English

在经典asp中执行存储过程并返回值

[英]execute stored procedure in classic asp and return values

I need to call from a asp page a stored procedure I have in SQL 2008 and pass to it two values and at the end return the count of two variables. 我需要从asp页调用SQL 2008中具有的存储过程,并将其传递给它两个值,最后返回两个变量的计数。

this is the stored procedure I have: 这是我的存储过程:

        /****** STUDATATAB******/
CREATE PROCEDURE StdntReconcileDups @NewSTDNT int = NULL, @OldSTDNT int = NULL output
AS
--use ZLDP01RD;
--count = 9 rows
Select count (*) from STUDATA.dbo.STUDATATAB
 where STDNT = @NewSTDNT;

-- count = 576 rows
select count(*) from STUDATA.dbo.STUDATATAB
 where STDNT = @OldSTDNT;

-- select duplicate keys with new student# count = 3 rows
select a.STDNT,a.crs,a.CRS_VRSN,a.QSTN,a.SCR
    from STUDATA.dbo.STUDATATABa, STUDATA.dbo.STUDATATABb
        where a.STDNT = @NewSTDNT
        and b.STDNT = @OldSTDNT
        and a.crs = b.crs
        and a.CRS_VRSN=b.CRS_VRSN
        and a.QSTN=b.QSTN
        and a.SCR=b.SCR 

-- select duplicate keys with new student# count = 3 rows
select count (*)
     from STUDATA.dbo.STUDATATABa
     where exists (select 1 from STUDATA.dbo.STUDATATABb
                    where a.STDNT = @NewSTDNT
                      and b.STDNT = @OldSTDNT
                      and a.crs = b.crs
                      and a.CRS_VRSN=b.CRS_VRSN
                      and a.QSTN=b.QSTN
                      and a.SCR=b.SCR );


-- delete duplicate keys with new student# 3 rows deleted
 WITH STUDENT_CTE
  AS
  (select a.*
     from STUDATA.dbo.STUDATATABa
     where exists (select 1 from STUDATA.dbo.STUDATATABb
                    where a.STDNT = @NewSTDNT
                      and b.STDNT = @OldSTDNT
                      and a.crs = b.crs
                      and a.CRS_VRSN=b.CRS_VRSN
                      and a.QSTN=b.QSTN
                      and a.SCR=b.SCR ))
delete from STUDENT_CTE;

--Convert student #10826 history records to student #123196, should update 579 rows
UPDATE STUDATA.dbo.STUDATATAB
 SET STDNT = @NewSTDNT, LSTUPDT_USER_ID_CD = 'DFERN', LSTUPDT_TS = getDate()
 where STDNT = @OldSTDNT;

-- count= 582
select count(*) from STUDATA.dbo.STUDATATAB
 where STDNT = @NewSTDNT;

-- count= 0
Select count(*) from STUDATA.dbo.STUDATATAB
 where STDNT = @OldSTDNT;
go

I want to insert the code in the if argument so it calls StdntReconcileDups and pass to it the values of KeepSTDNT and RemoveSTDNT 我想在if参数中插入代码,以便它调用StdntReconcileDups并将其值KeepSTDNT和RemoveSTDNT传递给它

    Dim DBConn  ' Connection object
If Request.Form("Action") = "Go!" then


Endif

the concept is the exeute your request ... you will get back a recordset which you will iterate over until you get to eof... then you will call next recordset to get to the next result. 这个概念就是执行您的请求...您将返回一个记录集,您将对其进行迭代直到到达eof ...然后将调用下一个记录集以获取下一个结果。

... are you looking for something like this? ...您是否正在寻找这样的东西?

connectstring = "..."
sql = "StdntReconcileDups '" & NewSTDNT & "','" & OldSTDNT & "'"
set rs = Server.CreateObject("ADODB.RecordSet")

rs.Open sql, connectstring, 3

If I got your meaning...then do like below: 如果我明白了您的意思...那么请执行以下操作:

 SqlConnection con = new SqlConnection("Data Source=  ; initial catalog= Northwind ; User Id=  ; Password=  '");

        con.open();

After creating SqlConnection you will create Storedprocedure like below: 创建SqlConnection之后,您将如下创建Storedprocedure:

        CREATE PROCEDURE RegionUpdate (@RegionID INTEGER,@RegionDescription NCHAR(50)) AS
        SET NOCOUNT OFF
        UPDATE Region
        SET RegionDescription = @RegionDescription

Create a SqlCommand object with the parameters as the name of the stored procedure that is to be executed and the connection object con to which the command is to be sent for execution. 创建一个SqlCommand对象,并使用参数作为要执行的存储过程的名称以及将要发送命令以执行的连接对象。

        SqlCommand command = new SqlCommand("RegionUpdate",con);

Change the command objects CommandType property to stored procedure . 将命令对象的CommandType属性更改为存储过程

    command.CommandType = CommandType.StoredProcedure;

Add the parameters to the command object using the Parameters collection and the SqlParameter class. 使用Parameters集合和SqlParameter类将参数添加到命令对象。

        command.Parameters.Add(new SqlParameter("@RegionID",SqlDbType.Int,0,"RegionID"));

        command.Parameters.Add(new SqlParameter("@RegionDescription",SqlDbType.NChar,50,"RegionDescription"));

Specify the values of the parameters using the Value property of the parameters 使用参数的Value属性指定参数的值

        command.Parameters[0].Value=4;

        command.Parameters[1].Value="SouthEast";

Excecute the stored procedure using the ExecuteNonQuery method which returns the number of rows effected by the stored procedure. 使用ExecuteNonQuery方法执行存储过程,该方法返回由存储过程影响的行数。

         int i=command.ExecuteNonQuery();

The code above is the right way...You can replace @KeepSTDNT and @RemoveSTDNT exactly instead of @RegionID and @RegionDescription ...I have used it myself...if you wanna more help...tell then 上面的代码是正确的方法...您可以完全替换@KeepSTDNT@RemoveSTDNT而不是@RegionID@RegionDescription ...我自己使用过...如果您需要更多帮助...告诉然后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM