我正在尝试设计一个接收订单行的登陆表,然后触发器会将数据发送到相应的表格(即客户,订单,订单行)。
我很容易在MySQL中用新旧关键词做到这一点但是我在SQL Server中遇到了一些问题。
首先 - 这是解决此类问题的最佳方法吗?
其次,如果它是我试图创建一个触发器,它使用游标迭代SQL Server中的插入表。不幸的是我一直收到错误
Msg 16916,Level 16,State 1,Procedure _raw_b2c_lines_ins,Line 17 [Batch Start line 6]
名为“ins_cusor”的光标不存在
我附上了下面的代码(删除了订单和订单行表的代码以简化)
CREATE OR ALTER TRIGGER zap._raw_b2c_lines_ins
on zap._b2c
after insert
as
begin
declare @v_phone_cleansed varchar
, @phone varchar
, @v_curr_cust_id varchar
, @customer_key varchar
, @t_order_id int
, @customer_id varchar
, @t_raw_id int
--
declare ins_cursor CURSOR FOR SELECT t_raw_id from inserted;
--
OPEN ins_cusor;
FETCH NEXT from ins_cursor INTO @t_raw_id;
WHILE (@@FETCH_STATUS = 0)
--
BEGIN
set @v_phone_cleansed = dbo.CleanNumber((select billing_phone
from inserted
where t_raw_id = @t_raw_id
));
set @v_curr_cust_id = (select isnull(max(t_customer_id),0) +1 from sales.b2c_customer);
--
if exists( select 1
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) = lower(i.billing_email)
and ( lower(c.country) != lower(i.billing_country)
or lower(c.city) != lower(i.billing_city)
or c.phone_cleansed != @v_phone_cleansed
)
and c.current_record = 1
and i.t_raw_id = @t_raw_id )
--
Begin
set @customer_key = ( select top 1 customer_key
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) = lower(i.billing_email)
and ( lower(c.country)!= lower(i.billing_country)
or lower(c.city) != lower(i.billing_city)
or c.phone_cleansed != @v_phone_cleansed
)
and c.current_record = 1
and i.t_raw_id = @t_raw_id
);
--
update sales.b2c_customer
set current_record = 0
where exists (select *
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) = lower(i.billing_email)
and ( lower(c.country) != lower(i.billing_country)
or lower(c.city) != lower(i.billing_city)
or c.phone_cleansed != @v_phone_cleansed
)
and c.current_record = 1
and i.t_raw_id = @t_raw_id
)
insert into sales.b2c_customer(
customer_key
, last_name
, first_name
, email
, country
, city
, phone
, phone_cleansed
, current_record
) select @customer_key
, dbo.InitCap(i.billing_last_name)
, dbo.InitCap(i.billing_first_name)
, i.billing_email
, i.billing_country
, dbo.InitCap(i.billing_city)
, i.billing_phone
, @v_phone_cleansed
, 1
from inserted i
where i.t_raw_id = @t_raw_id
;
--
end
--
if exists( select 1
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) != lower(i.billing_email)
and lower(c.country) = lower(i.billing_country)
and lower(c.city) = lower(i.billing_city)
and lower(c.phone_cleansed) = @v_phone_cleansed
and current_record = 1
and i.t_raw_id = @t_raw_id
)
--
Begin
--
set @customer_key = ( select top 1 customer_key
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) != lower(i.billing_email)
and lower(c.country) = lower(i.billing_country)
and lower(c.city) = lower(i.billing_city)
and c.phone_cleansed = @v_phone_cleansed
and i.t_raw_id = @t_raw_id
);
update sales.b2c_customer
set current_record = 0
where exists (select *
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) != lower(i.billing_email)
and lower(c.country) = lower(i.billing_country)
and lower(c.city) = lower(i.billing_city)
and c.phone_cleansed = @v_phone_cleansed
and c.current_record = 1
and i.t_raw_id = @t_raw_id
)
insert into sales.b2c_customer(
customer_key
, last_name
, first_name
, email
, country
, city
, phone
, phone_cleansed
, current_record
) select @customer_key
, dbo.InitCap(i.billing_last_name)
, dbo.InitCap(i.billing_first_name)
, i.billing_email
, i.billing_country
, dbo.InitCap(i.billing_city)
, i.billing_phone
, @v_phone_cleansed
, 1
from inserted i
where i.t_raw_id = @t_raw_id
--
end
--
if not exists (
select 1
from sales.b2c_customer c
join inserted i
on lower(c.first_name) = lower(i.billing_first_name)
and lower(c.last_name) = lower(i.billing_last_name)
and lower(c.email) = lower(i.billing_email)
and lower(c.country) = lower(i.billing_country)
and lower(c.city) = lower(i.billing_city)
and c.phone_cleansed = @v_phone_cleansed
and c.current_record = 1
and i.t_raw_id = @t_raw_id
)
--
Begin
--
insert into sales.b2c_customer( customer_key
, last_name
, first_name
, email
, country
, city
, phone
, phone_cleansed
, current_record
) select concat(lower(i.billing_first_name), lower(i.billing_last_name), @v_curr_cust_id)
, dbo.InitCap(i.billing_last_name)
, dbo.InitCap(i.billing_first_name)
, i.billing_email
, i.billing_country
, dbo.InitCap(i.billing_city)
, i.billing_phone
, @v_phone_cleansed
, 1
from inserted i
where i.t_raw_id = @t_raw_id;
--
end
--
FETCH NEXT from ins_cursor INTO @t_raw_id;
end
close ins_cursor;
deallocate ins_cursor;
end
答案 0 :(得分:-1)
首先在该名称中创建过程然后执行 因为它找不到光标。