简体   繁体   English

SQL Server存储过程从刚插入的行中获取值

[英]SQL Server Stored Procedure Get a value from a row just inserted

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. 我是刚开始编写存储过程的新手,我正在尝试创建一个将用于确定数据库中是否存在贝宝交易ID的数据库,如果不存在,则使用过程参数将客户添加到数据库中,然后返回新的客户的ID或-1,以表明交易之前已被处理。

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. 我正在使用一个简单的ASP页来测试此过程的结果。 Here is the bulk of my code for my ASP page: 这是我的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,"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. 当我运行ASP页时,在paypal_txns表中没有事务ID(abcdefg)时,它将显示“客户ID =”,就好像ID为空,空值或不存在。 The second time I run the ASP page it displays "Customer ID = -1" because the transaction ID is in the table now. 我第二次运行ASP页面时,它会显示“客户ID = -1”,因为事务ID现在在表中。

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. 我还尝试了一些代码调整,以查看是否可以找回有效的客户ID,并且如果改变逻辑可以得到除-1以外的值,但它仅在第二次运行ASP后才会显示时间。

I'm thinking there must be something I'm overlooking in my stored procedure code... any help would be appreciated. 我想我的存储过程代码中肯定有某些东西我会忽略的……任何帮助将不胜感激。

Thanks! 谢谢!

i didn't work much with classic asp but instead of taking value in output parameter, if you can use in recordset object. 我没有使用经典的asp进行很多工作,但是如果可以在recordset对象中使用它,则不必在输出参数中取值。

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") recordset.fields(“ custid”)

Hope this helps you. 希望这对您有所帮助。

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

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