SQL Server Stored Procedure Get a value from a row

2019-08-08 08:43发布

问题:

I'm new to writing stored procedures and I'm trying to create a one that will be used to determine if a paypal transaction id exists in the database and if not add a customer to the database using the procedure parameters then return either the new customer's ID or -1 to indicate the transaction was already processed before.

Here is what I have for the stored procedure:

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 txn_id=@txn_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

I'm using a simple ASP page to test the results of this procedure. Here is the bulk of my code for my ASP page:

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,"johndoe@fake.com") 

   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>")

When I run the ASP page without having the transaction ID (abcdefg) in the paypal_txns table it will display "Customer ID = " as if the ID is empty, null, or otherwise does not exist. The second time I run the ASP page it displays "Customer ID = -1" because the transaction ID is in the table now.

I've also tried tweaking the code a bit to see if I can get a valid customer ID back and I can get a value other than -1 if I change the logic around but it still only shows up after I run the ASP a second time.

I'm thinking there must be something I'm overlooking in my stored procedure code... any help would be appreciated.

Thanks!

回答1:

i didn't work much with classic asp but instead of taking value in output parameter, if you can use in recordset object.

if (select count(txn_id) from paypal_txns WHERE txn_id=@txn_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

You can read the custid using

recordset.fields("custid")

Hope this helps you.