0
オーダーラインを受け取るランディングテーブルを設計しようとしていて、トリガーが適切なテーブル(顧客、オーダー、オーダーライン)にデータを送信します。SQL Server - カーソル付きトリガー
新しいキーワードと古いキーワードで簡単にこれを行うことができましたが、SQL Serverにいくつか問題があります。
まず、このタイプの問題には、これが最善のアプローチですか?
2番目の場合は、カーソルを使用してSQL Serverの挿入されたテーブルを反復処理するトリガーを作成しようとしました。残念ながら、私はエラー
メッセージ16916、レベル16、状態1、プロシージャ_raw_b2c_lines_ins、ライン17を得続ける[バッチはスタートライン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
私はスペルミスを見つけました...これがエラーを取得している理由です...私の悪い 最初の質問に関するご意見はありますか?私が達成したいことのための最良のアプローチですか? –
'OPEN ins_cusor;の代わりに' OPEN ins_cursor; 'をタイプする – Malainine
[癖がつきにくい:(長さ)のないVARCHARの宣言](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/09/bad varchar-without-length.aspx) - あなたが使用する 'varchar'変数とパラメータの長さを常に**与える必要があります。それ以外の場合は、**正確に1文字の長さのSQL変数になります** - 通常は*あなたが望むものではありません..... –