0

我需要从 asp 页面调用我在 SQL 2008 中的存储过程,并将两个值传递给它,最后返回两个变量的计数。

这是我的存储过程:

        /****** 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

我想在 if 参数中插入代码,以便它调用 StdntReconcileDups 并将 KeepSTDNT 和 RemoveSTDNT 的值传递给它

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


Endif
4

2 回答 2

0

这个概念是执行您的请求...您将返回一个记录集,您将对其进行迭代,直到您到达 eof... 然后您将调用下一个记录集以获取下一个结果。

......你在寻找这样的东西吗?

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

rs.Open sql, connectstring, 3
于 2013-05-14T17:23:54.253 回答
0

如果我明白了你的意思……那么请执行以下操作:

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

        con.open();

创建 SqlConnection 后,您将创建如下所示的 Storedprocedure:

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

创建一个 SqlCommand 对象,参数为要执行的存储过程的名称,以及要向其发送命令以执行的连接对象 con。

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

将命令对象的 CommandType 属性更改为存储过程

    command.CommandType = CommandType.StoredProcedure;

使用Parameters 集合和SqlParameter 类将参数添加到命令对象。

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

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

使用参数的 Value 属性指定参数的值

        command.Parameters[0].Value=4;

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

使用 ExecuteNonQuery 方法执行存储过程,该方法返回存储过程影响的行数。

         int i=command.ExecuteNonQuery();

上面的代码是正确的方法...您可以完全替换@KeepSTDNT@RemoveSTDNT而不是@RegionID@RegionDescription ...我自己使用过...如果您需要更多帮助...请告诉

于 2013-05-14T19:18:30.417 回答