2013-06-01 69 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