2013-06-01 63 views
1

我是新来编写存储过程,我试图创建一个将用于确定数据库中是否存在贝宝事务ID,如果不是使用过程参数将客户添加到数据库,然后返回新客户的ID或-1以指示事务已经在之前处理过。SQL Server存储过程从刚刚插入的行获取值

以下是我对存储过程:

CREATE PROCEDURE [dbo].[addCustomerIfNotExist] @txn_id varchar(max), @fName nvarchar(255), @lastName nvarchar(255), @e_mail nvarchar(255), @password nvarchar(100), @customerId int OUTPUT 
AS 
    BEGIN 
    -- if the transaction id has not already been processed 
    if (select count(txn_id) from paypal_txns WHERE [email protected]_id) = 0 
     BEGIN 
      --add txn id to the paypal_txns table 
      INSERT INTO paypal_txns VALUES(@txn_id) 
      --if the email address not is already in the database 
      IF (select count(email) from customers where email = @e_mail) = 0 
       BEGIN 
        --generate a new customer account 
        INSERT INTO customers (pcCust_AgreeTerms,pcCust_Guest,pcCust_Residential,pcCust_AllowReviewEmails,name,lastname,email,password) VALUES(0,0,1,1,@fName,@lastName,@e_mail,@password) 
       END 
      --get the customer id 
      select @customerId = idcustomer from customers where email = @e_mail 
     END 
    ELSE 
     BEGIN 
      SET @customerId = -1 
     END 
    END 
GO 

我用一个简单的ASP页面来测试这个过程的结果。下面是我的代码为我的ASP页批量:

Set cmd = Server.CreateObject("ADODB.Command") 
    Set cmd.ActiveConnection = paypalIpnDbCon 
    cmd.CommandText = "addCustomerIfNotExist" 
    cmd.CommandType = 4 'adCmdStoredProc 

    cmd.Parameters.Append cmd.CreateParameter("txn_id", 200, 1,-1,"abcdefg") 

    cmd.Parameters.Append cmd.CreateParameter("fName", 202, 1,-1,"John") 

    cmd.Parameters.Append cmd.CreateParameter("lastName", 202, 1,-1,"Doe") 

    cmd.Parameters.Append cmd.CreateParameter("e_mail", 202, 1,-1,"[email protected]") 

    cmd.Parameters.Append cmd.CreateParameter("password", 202, 1,-1,randomPassword) 

    cmd.Parameters.Append cmd.CreateParameter("customerId", 3, 2) 

    Set rs = cmd.Execute 

    custId = cmd.Parameters("customerId") 
    response.write("<br>Customer ID = " & custId & "<br>") 

当运行ASP页,而无需在paypal_txns表将显示该事务ID(ABCDEFG)“客户ID =”仿佛ID是空,空或否则不存在。第二次运行ASP页时,它显示“Customer ID = -1”,因为事务ID现在在表中。

我也尝试调整一下代码,看看我是否可以得到一个有效的客户ID,如果我改变了逻辑但我仍然只在运行后才显示出-1以外的值ASP第二次。

我在想我必须在我的存储过程代码中忽略一些东西......任何帮助将不胜感激。

谢谢!

回答

0

我没有用传统的asp 工作很多,但是如果你可以在记录集对象中使用,而不是在输出参数中取值。

if (select count(txn_id) from paypal_txns WHERE [email protected]_id) = 0 
    BEGIN 
     --add txn id to the paypal_txns table 
     INSERT INTO paypal_txns VALUES(@txn_id) 
     --if the email address not is already in the database 
     IF (select count(email) from customers where email = @e_mail) = 0 
      BEGIN 
       --generate a new customer account 
       INSERT INTO customers (pcCust_AgreeTerms,pcCust_Guest,pcCust_Residential,pcCust_AllowReviewEmails,name,lastname,email,password) VALUES(0,0,1,1,@fName,@lastName,@e_mail,@password) 
      END 
     --get the customer id 
     select @customerId = idcustomer from customers where email = @e_mail 
     select @customerId as custid 
    END 
ELSE 
    BEGIN 
     SET @customerId = -1 
     select @customerId as custid 
    END 
END 

您可以阅读使用

recordset.fields的客户ID( “客户ID”)

希望这有助于你。

+0

我试过了你的建议,并得到“ADODB.recordset错误'800a0cc1'项目不能在与请求的名称或序号对应的集合中找到。”但这只会在我第一次运行存储过程时发生。第二次运行时,我得到预期值-1 – Kal

+0

我试图模拟你的问题,我发现当我们尝试用新的transactionid填充记录集时,记录集中没有返回值。 这就是为什么它给错误。 同样发生在我身上。 我认为这与记录集有关,因为存储过程在两种情况下都返回值。 这就是我所知道的...... 希望你能找到替代这个 – Timon

+0

想到的唯一解决方法是分开逻辑,以便先插入客户数据,然后在单独的查询或过程调用中检索。我希望能够在同一个过程调用中插入和检索数据,但也许分裂起来更简单。 – Kal