我是编写存储过程的新手,我正在尝试创建一个用于确定数据库中是否存在paypal事务ID,以及是否使用过程参数将客户添加到数据库然后返回新客户的ID或-1表示交易之前已经处理过。
以下是我对存储过程的了解:
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
我正在使用一个简单的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,"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>")
当我在paypal_txns表中没有事务ID(abcdefg)的情况下运行ASP页面时,它将显示“Customer ID =”,就像ID为空,null或不存在一样。第二次运行ASP页面时,它显示“Customer ID = -1”,因为事务ID现在在表中。
我也尝试过稍微调整一下代码,看看我是否可以获得有效的客户ID,如果我改变逻辑,我可以得到-1以外的值,但是在运行之后它仍然只显示ASP第二次。
我在想我的存储过程代码中肯定会有一些东西......任何帮助都会受到赞赏。
谢谢!
答案 0 :(得分:0)
我对经典的asp不起作用 但是如果你可以在记录集对象中使用,而不是在输出参数中取值。
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
您可以使用
阅读custidrecordset.fields( “客户ID”)
希望这会对你有所帮助。