sql 出错...(出现死循环) 大家帮帮忙!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (10分)

  • 主题发起人 主题发起人 krisjim
  • 开始时间 开始时间
K

krisjim

Unregistered / Unconfirmed
GUEST, unregistred user!
请大家帮我看看为什么出现死循环???
注: bomidx 子结点 bomid 父结点 unitqty单机用量
PROCEDURE JH_P_ProductMaterial
create table tmp1(bomidx int,unitqty int)
DECLARE @bomidx INT
DECLARE @UnitQty INT
TRUNCATE TABLE TMP1
select bomidx,unitqty INTO #TMP1 from JH_BOM_Child where bomidx = 1
set identity_insert #tmp1 on
WHILE (exists (select * from #TMP1))
BEGIN
declare mycursor1 cursor for select bomidx, unitqty from #TMP1
open mycursor1
DELETE #TMP1 WHERE CURRENT OF mycursor1
fetch next from mycursor1 into @bomIdx, @UnitQty
while (@@fetch_status = 0)
begin
if not exists (select bomid from JH_BOM_Child where bomid = @bomIdx)
insert into tmp1 values(@bomIdx, @UnitQty)
else
INSERT #TMP1 SELECT bomidx,unitqty*@UnitQty FROM JH_BOM_Child WHERE bomid = @bomIDx
fetch next from mycursor1 into @bomIDx, @UnitQty
END
CLOSE mycursor1
DEALLOCATE mycursor1
END

select * from tmp1
 
为什么没有人帮我,
其实我的问题就是, 为什么在对临时表插入时出错, ?
"仅当使用了列的列表,并且 IDENTITY_INSERT 为 ON 时,才能在表 '#TMP1' 中为标识列指定显式值。"
 
可以改成这样(sql server 数据库)
CREATE PROCEDURE JH_P_ProductMaterial
AS
if not exists (select * from sysobjects where id = object_id(N'[dbo].[tmp1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
create table tmp1(bomidx int,unitqty int)
DECLARE @bomidx INT
DECLARE @UnitQty INT
TRUNCATE TABLE TMP1
select bomidx,unitqty INTO #TMP1 from JH_BOM_Child where bomidx = 1
set identity_insert #tmp1 on
WHILE (exists (select * from #TMP1))
BEGIN
declare mycursor1 cursor for select bomidx, unitqty from #TMP1
open mycursor1
---DELETE #TMP1 WHERE CURRENT OF mycursor1
fetch next from mycursor1 into @bomIdx, @UnitQty
DELETE #TMP1 WHERE bomidx=@bomIdx and unitqty=@UnitQty
while (@@fetch_status = 0)
begin
if not exists (select bomid from JH_BOM_Child where bomid = @bomIdx)
insert into tmp1 values(@bomIdx, @UnitQty)
else
INSERT #TMP1 SELECT bomidx,unitqty*@UnitQty FROM JH_BOM_Child WHERE bomid = @bomIDx
fetch next from mycursor1 into @bomIDx, @UnitQty
END
CLOSE mycursor1
DEALLOCATE mycursor1
END

select * from tmp1



 
---> DELETE #TMP1 WHERE CURRENT OF mycursor1
....
---> INSERT #TMP1 SELECT bomidx,unitqty*@UnitQty FROM JH_BOM_Child WHERE bomid = @bomIDx
估计问题出在这里, 你改变了表中的记录数, 但cursor未更新。
好象定义cursor是要加某个限制选项。 具体哪个忘了。 查一下msdn吧
 
现在我的问题还是出现死循环.
总觉得这里有错:
WHILE (exists (select * from #TMP1))
BEGIN

end

 
大家帮帮忙!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
怎么就没有其它人帮我看看是什么原因出现死循环呢?
 
后退
顶部