触发器中
CREATE TRIGGER trigger_products_data_insert
AFTER INSERT ON erp_products_data
FOR EACH ROW
BEGIN
insert into erp_products_data3(products3_sku,products3_suppliers_ids) values(new.products_sku,new.products_suppliers_ids);
END;
其中products_suppliers_ids字段的格式是”10,124,154,42″的逗号分割,现在要在触发器中分割字段值,
循环成4个insert到另外一个表,这个触发器begin里面的程序怎么样写
CREATE TRIGGER trigger_products_data_insert
AFTER INSERT ON erp_products_data
FOR EACH ROW
BEGIN
insert into erp_products_data3(products3_sku,products3_suppliers_ids) values(new.products_sku,new.products_suppliers_ids);
END;
其中products_suppliers_ids字段的格式是”10,124,154,42″的逗号分割,现在要在触发器中分割字段值,
循环成4个insert到另外一个表,这个触发器begin里面的程序怎么样写
解决方案
40
LZ你好,
可以定义下面的一个函数来分隔字符串
create function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),””)
end
insert @t(col) values (@c)
return
end
可以分隔字符串如
go
select * from dbo.f_split(“dfkd,dfdkdf,dfdkf,dffjk”,”,”)
drop function f_split
col
—
dfkd
dfdkdf
dfdkf
dffjk
可以定义下面的一个函数来分隔字符串
create function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),””)
end
insert @t(col) values (@c)
return
end
可以分隔字符串如
go
select * from dbo.f_split(“dfkd,dfdkdf,dfdkf,dffjk”,”,”)
drop function f_split
col
—
dfkd
dfdkdf
dfdkf
dffjk