SQL儲存過程展易飛BOM

2021-07-10 16:50:01 字數 2604 閱讀 7243

create table zbomcb1

(--bom展開表算表

cb001 char(20) not null,

cb005 char(20) not null,

cb008 numeric(16) not null

)create procedure zgetbom3level

as--使用游標展算bom插zbomcb1

delete from zbomcb1

declare @cb001 char(60),@cb002 char(60)--,@mq003 char(23),@la011 int--,@cb001_p char (60);

--insert into zbomcb1 select cb001,cb005,cb008 from bomcb left join invmb  on mb001=cb005 where cb001='211-001-3003-03-02' and mb025='p' and cb022='y'

begin

declare my cursor --定義游標

--獲取游標集合

--for (select a.mq003, a.la001,a.la011 from zfuntable('20150301','20160229') a left join invmb b on a.la001=b.mb001 where mb109='y'and mb025 !='p')

--for(select cb001,cb005,cb008 from bomcb left join invmb  on mb001=cb005 where cb001='211-001-3003-03-02' and mb025='m' and cb022='y' and mb109='y' )

for(select mb001 from invmb where  mb025='m' and mb109='y'  )

open my--開啟游標

fetch next from my into  @cb002--讀取第一行資料 賦值給變數

while @@fetch_status=0

begin  

--插入bom一階

insert into zbomcb1  select @cb002,cb005,cb008 from bomcb left join invmb  on mb001=cb005 where cb001=@cb002 and mb025='p'

begin  --開始bom二階

if(exists(select cb005 from bomcb left join invmb  on mb001=cb005 where cb001=@cb002 and mb025='m'))

declare @cb001_1 char(60)

declare my1 cursor

for( select cb005 from bomcb left join invmb  on mb001=cb005 where cb001=@cb002 and mb025='m')

open my1

fetch next from my1 into @cb001_1

while @@fetch_status=0

begin

--插入bom二階

insert into zbomcb1  select @cb002,cb005,cb008 from bomcb left join invmb  on mb001=cb005 where cb001=@cb001_1 and mb025='p'

begin  --開始迴圈三階

if(exists(select cb005 from bomcb left join invmb  on mb001=cb005 where cb001=@cb001_1 and mb025='m'))

declare @cb001_2 char(60)

declare my2 cursor

for( select cb005 from bomcb left join invmb  on mb001=cb005 where cb001=@cb001_1 and mb025='m')

open my2

fetch next from my2 into @cb001_2

while @@fetch_status=0

begin

--開始插入三階

insert into zbomcb1  select @cb002,cb005,cb008 from bomcb left join invmb  on mb001=cb005 where cb001=@cb001_2 and mb025='p'

fetch next from my2 into @cb001_2

endclose my2

deallocate my2

endfetch next from my1 into @cb001_1

endclose my1

deallocate my1

endfetch next from my into  @cb002

endclose my  --關閉游標

deallocate my --釋放游標

end

go

易飛 易飛LRP相關SQL

朋友發的易飛lrp相關sql 批次需求計畫系統 重點作業的使用 日誌 是否使用生成批次需求計畫 select case when exists select top 1 progid from dscsys.dbo.worklog with nolock where progid lrpb01 an...

易飛8無銷售報價的BOM成本參考

alter procedure dbo sp bom expand itemno varchar 80 as begin bom展階 itemno 查詢品號 返回 元件品號v1001,主件品號v1002,階碼v1004,階層次v1005,尾階標誌v1006,展開標誌v1007,組成用量v1011,底...

易飛工單用料和實際用料差別分析儲存過程

工單用料和實際用料差別主要體現在bom不發料有點多,而bom表裡沒有做成直接材料,另外就是工單變更,那就什麼可能也會發生,協助分析工單用料分析和工單用料分析表差不多吧,但應該簡潔快 alter proc dbo mocyl starttime varchar 8 endtime varchar 8 ...