要求摘要:
每当客户输入新订单时,在旧订单前检查信用卡信息和使用代码。如果有不同的客户使用相同的信用卡,则该信息将输入新表以验证客户数据
我面临的问题:
问题是同一客户和同一信贷的条件不起作用......
我尝试了什么:
CREATE TRIGGER order_credit_card
ON dbo.orders
FOR INSERT
AS
DECLARE @O_ID VARCHAR (20)
DECLARE @O_Date Datetime
DECLARE @O_Made_By VARCHAR (255)
DECLARE @O_Credit VARCHAR(50)
BEGIN
IF CURSOR_STATUS('global','Credit_Card_CURSOR')>=-1
BEGIN
DEALLOCATE Credit_Card_CURSOR
END
DECLARE Credit_Card_CURSOR CURSOR FOR
SELECT [order-id], [date], [made-by], [paid-with]
FROM inserted
OPEN Credit_Card_CURSOR
FETCH NEXT FROM Credit_Card_CURSOR INTO @O_ID, @O_Date, @O_Made_By, @O_Credit
WHILE (@@FETCH_STATUS = 0)
BEGIN
IF @O_Made_By IN (SELECT o.[Made-by]
FROM Orders o
WHERE o.[Made-by] = @O_Made_By
AND o.[paid-with] = @O_Credit)
BEGIN
UPDATE DBO.orders
SET [approved] = 'ok'
WHERE [Made-by] = @O_Made_By
END
ELSE
BEGIN
INSERT INTO DBO.[Security_warrnings]
VALUES (@O_ID, @O_Credit)
END
FETCH NEXT FROM Credit_Card_CURSOR INTO @O_ID, @O_Date, @O_Made_By, @O_Credit
END
CLOSE Credit_Card_CURSOR
END
答案 0 :(得分:0)
您不需要光标。您可以使用Inserted
虚拟表...根据您的要求进行修改。如果客户已使用相同的信用卡号进行购买,则代码会将数据插入到其他表中。
Create trigger dbo.test
on yourtable
after insert
as
Begin
--check if user has made purchase with same CC number
if exists
(select 1 from
from
Orders o
join
inserted i
on i.O_Made_By =o.O_Made_By
and i.creditcardnumber=o.creditcardnumber
)
Begin
--he made
insert into DBO.[Security_warrnings]
select i.orderid,i.creditcardnumber
from inserted i
join
yoursordertable o
on o.creditcardnumber=i.creditcardnumber
End
End
答案 1 :(得分:0)
CREATE TRIGGER order_credit_card
...
BEGIN
declare @orders_with_warning table ([order-id] int)
insert into DBO.[Security_warrnings] ([order-id], [paid-with])
OUTPUT INSERTED.id
INTO @orders_with_warning([order-id])
select i.[order-id], i.[paid-with]
from inserted i
where exists(select 1 from dbo.Orders o
where o.[paid-with] = i.[paid-with]
and o.[Made-by] != i.[Made-by])
UPDATE o SET
[approved]= 'ok'
from DBO.orders o
inner join inserted i
on i.[order-id] = o.[order-id]
where not exists(select 1 from @orders_with_warning ow
where ow.[order-id] = o.[order-id])
END
尝试:
@O_Made_By in (select o.[Made-by]
from Orders o
where o.[Made-by]=@O_Made_By
实际上毫无意义。将col / var name替换为number以检查方案:
23 in (select 23 where 23=23)