黑
黑
Unregistered / Unconfirmed
GUEST, unregistred user!
求在SQL里寫存儲過程將一個表的內容按條件倒入另一個表!在這里有兩次嵌套循環,因為條件的限制,所以要在while 里再來一個while ,另外,求有方面經驗的朋友指點一二!
我的代碼如下:
CREATE PROCEDURE pGetStore
@ycode nvarchar(20) --接收參數,方便查詢某一產品的庫存
AS
declare @levCount integer
declare @oveCount integer
declare @sublevcount integer
declare @subovecount integer
begin
set nocount on
select @levcount=1
select @sublevcount=1
declare @yinqty numeric(9,2)
declare @youtqty numeric(9,2)
declare @ymethod nvarchar(6)
declare @pqty numeric(9,2)
declare @subitem table
(
sn integer identity,
subycode nvarchar(20),
Countrecord integer
)
declare @inittmp table --定義一臨時表,取得期初數,進,出,數
(
sn integer identity,
idx integer,
ymethod nvarchar(6),
ycode nvarchar(20),
yinqty numeric(9,2) not null default 0.00,
yindate datetime,
yinno nvarchar(20),
youtqty numeric(9,2) not null default 0.00,
youtdate datetime,
youtno nvarchar(20),
ystoreqty numeric(9,2) not null default 0.00,
ypublicdate datetime
)
declare @analyzertmp table --定義一臨時表,進行進出數的分析
(
idx integer,
ymethod nvarchar(6),
ycode nvarchar(20),
yinqty numeric(9,2) not null default 0.00,
yindate datetime,
yinno nvarchar(20),
youtqty numeric(9,2) not null default 0.00,
youtdate datetime,
youtno nvarchar(20),
yrstoreqty numeric(9,2) not null default 0.00,
ypublicdate datetime
)
insert into @inittmp(ymethod,ycode,yinqty,yindate,yinno,ypublicdate) --在此處先插入期初數
select 'IN' as ymethod,a.ycode,a.yqty as yinqty,a.ydate as yindate,'INITQTY' as yinno,a.ydate as ypublicdate from yinitprocduce a where a.yqty>0 order by a.ycode
insert into @inittmp --在此處倒入庫存數
select idx,ymethod=
case
when a.yinno is null then 'OUT'
when a.youtno is null then 'IN'
end,
a.ycode,a.yinqty,a.yindate,a.yinno,a.youtqty,a.youtdate,a.youtno,convert(numeric(9,2),0.00) as ystoreqty,ypublicdate=
case
when a.yindate is null then a.youtdate --增加一個公共的日期,當以日期的前後來排序,提供出,或進的序數
when a.youtdate is null then a.yindate
end
from yidms_store a order by a.ycode
insert into @subitem(subycode,Countrecord) select ycode,count(ycode) as Countrecord from @inittmp group by ycode order by ycode --只找現單一的產品
select @subovecount=count(*) from @subitem
while (select count(*) from @subitem)>0
begin
if (@sublevcount>@subovecount)
begin
goto out
end
在此這里還要一次的while ,代碼還沒有寫完想看看大家結此有什么看法?
end
out:
set nocount off
end
out:
select * from @subitem order by subycode
GO
我的代碼如下:
CREATE PROCEDURE pGetStore
@ycode nvarchar(20) --接收參數,方便查詢某一產品的庫存
AS
declare @levCount integer
declare @oveCount integer
declare @sublevcount integer
declare @subovecount integer
begin
set nocount on
select @levcount=1
select @sublevcount=1
declare @yinqty numeric(9,2)
declare @youtqty numeric(9,2)
declare @ymethod nvarchar(6)
declare @pqty numeric(9,2)
declare @subitem table
(
sn integer identity,
subycode nvarchar(20),
Countrecord integer
)
declare @inittmp table --定義一臨時表,取得期初數,進,出,數
(
sn integer identity,
idx integer,
ymethod nvarchar(6),
ycode nvarchar(20),
yinqty numeric(9,2) not null default 0.00,
yindate datetime,
yinno nvarchar(20),
youtqty numeric(9,2) not null default 0.00,
youtdate datetime,
youtno nvarchar(20),
ystoreqty numeric(9,2) not null default 0.00,
ypublicdate datetime
)
declare @analyzertmp table --定義一臨時表,進行進出數的分析
(
idx integer,
ymethod nvarchar(6),
ycode nvarchar(20),
yinqty numeric(9,2) not null default 0.00,
yindate datetime,
yinno nvarchar(20),
youtqty numeric(9,2) not null default 0.00,
youtdate datetime,
youtno nvarchar(20),
yrstoreqty numeric(9,2) not null default 0.00,
ypublicdate datetime
)
insert into @inittmp(ymethod,ycode,yinqty,yindate,yinno,ypublicdate) --在此處先插入期初數
select 'IN' as ymethod,a.ycode,a.yqty as yinqty,a.ydate as yindate,'INITQTY' as yinno,a.ydate as ypublicdate from yinitprocduce a where a.yqty>0 order by a.ycode
insert into @inittmp --在此處倒入庫存數
select idx,ymethod=
case
when a.yinno is null then 'OUT'
when a.youtno is null then 'IN'
end,
a.ycode,a.yinqty,a.yindate,a.yinno,a.youtqty,a.youtdate,a.youtno,convert(numeric(9,2),0.00) as ystoreqty,ypublicdate=
case
when a.yindate is null then a.youtdate --增加一個公共的日期,當以日期的前後來排序,提供出,或進的序數
when a.youtdate is null then a.yindate
end
from yidms_store a order by a.ycode
insert into @subitem(subycode,Countrecord) select ycode,count(ycode) as Countrecord from @inittmp group by ycode order by ycode --只找現單一的產品
select @subovecount=count(*) from @subitem
while (select count(*) from @subitem)>0
begin
if (@sublevcount>@subovecount)
begin
goto out
end
在此這里還要一次的while ,代碼還沒有寫完想看看大家結此有什么看法?
end
out:
set nocount off
end
out:
select * from @subitem order by subycode
GO