在线等,50分求一个存储过程!!!!!!!!!!!!!!!
在实际应用中,为了便于商品的管理,我们对商品进行了分级管理,譬如:
商品编码(varchar主键) 商品简称 商品规格 商品供货企业 ......
7020001 青霉素
702000101 青霉素 20*12 湖南 ......
702000102 青霉素 25*12 湖北 ......
702000103 青霉素 20*12 华北 ......
7020002 葡萄糖
702000201 葡萄糖 100ml 河南 ......
702000202 葡萄糖 80ml 北京 ......
702000203 葡萄糖 120ml 长春 ......
702000204 葡萄糖 120ml 湖南 ......
当管理员增加商品的时候,如果该商品在已有表中有记录,如增加 "青霉素 ",30*10,河北的,
那么就首先找到青霉素的大类,为7020001,然后根据现有最大的子级编码,给要加的商品给个编码,既702000104;如果该商品不存在,如金唯他,那么就首先加一级金唯他,并给他相应的编码,既7020003,然后再把他的详细信息录入到子级中去,既
7020003 金唯他
702000301 金唯他 100ml/盒 北京 ......
[解决办法]
以三列为例:
create table test(商品编码 varchar(20),商品简称 varchar(20),商品规格 varchar(20),商品供货企业 varchar(20))
insert into test
select '7020001 ', '青霉素 ', ' ', ' '
union all
select '702000101 ', '青霉素 ', '20*12 ', '湖南 '
union all
select '702000102 ', '青霉素 ', '25*12 ', '湖北 '
union all
select '702000103 ', '青霉素 ', '20*12 ', '华北 '
union all
select '7020002 ', '葡萄糖 ', ' ', ' '
union all
select '702000201 ', '葡萄糖 ', '100ml ', '河南 '
union all
select '702000202 ', '葡萄糖 ', '80ml ', '北京 '
union all
select '702000203 ', '葡萄糖 ', '120ml ', '长春 '
union all
select '702000204 ', '葡萄糖 ', '120ml ', '湖南 '
drop table test
alter proc dbo.Proc_AddRecord(@HSCode varchar(20),@HSSize varchar(20),@HSErp varchar(20))
as
begin
set nocount on
begin tran
if exists(select 1 from test where 商品简称=@HSCode and 商品规格=@HSSize and 商品供货企业=@HSErp)
begin
print 'exists the record '
--return
end
else
begin
if exists(select 1 from test where 商品简称=@HSCode)
begin
insert into test
select (select max(商品编码) +1 from test where 商品简称=@HSCode),@HSCode,@HSSize,@HSErp
end
else
begin
insert into test
select (select max(商品编码) +1 from test where 商品规格= ' '),@HSCode, ' ', ' '
insert into test
select cast((select max(商品编码) from test where 商品简称=@HSCode) as varchar(20)) + '01 ',@HSCode,@HSSize,@HSErp
end
end
if @@error=0
commit tran
else
rollback
end
dbo.Proc_AddRecord '青霉素 ', '20*12 ', '湖南 '
dbo.Proc_AddRecord 'new ', '20*12 ', '湖南 '
dbo.Proc_AddRecord '青霉素 ', '20*12 ', '湖南 '