SQL SERVER 2000的一个存储过程有错,急求帮助

蜘蛛上网 2011-02-14 08:58:20
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON
GO


ALTER PROCEDURE sp_QuerySalesTree --@cCoding='S000004'

@cCoding varchar(15)=''
as

select cSCQCoding, cSCode=year(dSCDate), cSName=cCoGarCode+'('+cCusGarCode+')' into #temp
from tm_SalesConfirmation a,(select cSCCusCode from tm_SalesConfirmation where cSCQCoding=@cCoding) b
where a.cSCCusCode=b.cSCCusCode and a.isctype<>-1

select IDENTITY(int,1,1) as cSCQCoding,cSCode=year(dSCDate),cSName=year(dSCDate) into #temp1
from tm_SalesConfirmation a,(select cSCCusCode from tm_SalesConfirmation where cSCQCoding=@cCoding)b
where a.cSCCusCode=b.cSCCusCode and a.isctype<>-1 group by year(dSCDate)

insert into #temp(cSCQCoding,cSCode,cSName) select cSCQCoding,cSCode,cSName from #temp1


alter table #temp add cParent varchar(15),iGrade int,bEnd bit
declare @cSCode int
declare @cSCQCoding varchar(15)
declare @cSName varchar(30)
declare @iNum int
declare cur cursor for select cSCode from #temp1
open cur
FETCH NEXT FROM cur
INTO @cSCode
WHILE @@FETCH_STATUS = 0
BEGIN
set @iNum=1
declare cur1 cursor for select cSCQCoding,cSName from #temp where cSCode=@cSCode and cast(cSCode as varchar(10))<>cSName
open cur1
FETCH NEXT FROM cur1
INTO @cSCQCoding,@cSName
WHILE @@FETCH_STATUS = 0
BEGIN

update #temp set cSCode=cSCode*1000+@iNum where cSCQCoding=@cSCQCoding
set @iNum=@iNum+1
FETCH NEXT FROM cur1 iNTO @cSCQCoding,@cSName

END
close cur1
deallocate cur1

FETCH NEXT FROM cur iNTO @cSCode
END
close cur
deallocate cur

drop table #temp1

update #temp set iGrade=1,bEnd=0 where cSCode<3000 --更新语句
update a set iGrade=2,cParent=b.cSCQCoding,bEnd=1 from #temp a,#temp b
where a.cSCode>3000 and left(cast(a.cSCode as varchar(10)),4)=cast(b.cSCode as varchar(10)) --更新语句

select * from #temp order by cSCode

drop table #temp

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO



报错:服务器: 消息 207,级别 16,状态 1,过程 sp_QuerySalesTree,行 53
列名 'iGrade' 无效。
服务器: 消息 207,级别 16,状态 1,过程 sp_QuerySalesTree,行 54
列名 'iGrade' 无效。

我将最后的两条更新语句去掉后,就没有错误了,不知道是何原因?
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2011-02-14
  • 打赏
  • 举报
回复
总共就5列 自己创建一个临时表试试
AcHerat 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qzonemen 的回复:]
引用 2 楼 wxf163 的回复:
insert into #temp(cSCQCoding,cSCode,cSName) select cSCQCoding,cSCode,cSName from #temp1

update a set iGrade=2,cParent=b.cSCQCoding,bEnd=1 from #temp a,#temp b
这列在哪呢?


alte……
[/Quote]

alter table #temp add cParent varchar(15),iGrade int,bEnd bit

加个go看看

alter table #temp add cParent varchar(15),iGrade int,bEnd bit
go

不过我估计这样可能行不通,在存储过程里修改列属性。。。额。。。
蜘蛛上网 2011-02-14
  • 打赏
  • 举报
回复
cSCQCoding cSCode cSName cParent iGrade bEnd
1 2007 2007 NULL NULL NULL
2 2008 2008 NULL NULL NULL
3 2009 2009 NULL NULL NULL
4 2010 2010 NULL NULL NULL
S000002 2007001 BM207064(F7 4259CO) NULL NULL NULL
S000038 2007002 BM207133(F8 4151) NULL NULL NULL
S000753 2007003 BM208104(F8 4151) NULL NULL NULL
S000817 2007004 BM208135(F8 64004 ST) NULL NULL NULL
S000818 2007005 BM208136(F8 64106ST) NULL NULL NULL
S000526 2008001 BM208011(F8 4242CC) NULL NULL NULL
S000588 2008002 BM208031(F8 4201) NULL NULL NULL
S000617 2008003 BM208035(F8 4201P) NULL NULL NULL
S000618 2008004 BM208036(F8 4260P) NULL NULL NULL
S000619 2008005 BM208037(F8 4201BUR) NULL NULL NULL

这是我把两条更新语句去掉后的结果
蜘蛛上网 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxf163 的回复:]
insert into #temp(cSCQCoding,cSCode,cSName) select cSCQCoding,cSCode,cSName from #temp1

update a set iGrade=2,cParent=b.cSCQCoding,bEnd=1 from #temp a,#temp b
这列在哪呢?
[/Quote]

alter table #temp add cParent varchar(15),iGrade int,bEnd bit
这个看到没
蜘蛛上网 2011-02-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
SQL code

--a表里没有iGrade这个列。
[/Quote]

不是,我有这条语句 alter table #temp add cParent varchar(15),iGrade int,bEnd bit
王向飞 2011-02-14
  • 打赏
  • 举报
回复
insert into #temp(cSCQCoding,cSCode,cSName) select cSCQCoding,cSCode,cSName from #temp1

update a set iGrade=2,cParent=b.cSCQCoding,bEnd=1 from #temp a,#temp b
这列在哪呢?
AcHerat 2011-02-14
  • 打赏
  • 举报
回复

--a表里没有iGrade这个列。

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧