S
stuwe
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是一個存儲過程的內容
----------------------------------------------------------------------------
declare @SQL_str varchar(8000)
declare @tmp_str varchar(300)
declare @CLMC sysname
declare @DDCMC sysname
declare @DDType sysname
execute('select convert(varchar(300),'''') as Field1 into #MytempTable')--創建臨時表,存放sql語句
execute('declare SQL_Cursor cursor for select Field1 from #MytempTable')
begin
declare CL_Cursor cursor
for
select distinct CLMC from CLJGB where IsNull(CLMC,'')<>''
open CL_Cursor--打開材料遊標
declare DDCMC_Cursor cursor scroll
for
select b.DDCMC from DDCZLB a left join DDCZLB b on a.DDCBH=b.DDCBH
where a.DDCBH is not NULL order by a.DDCBH
open DDCMC_Cursor--打開電鍍廠名稱遊標
fetch next from CL_Cursor into @CLMC
while (@@fetch_status=0)
begin
fetch absolute 1 from DDCMC_Cursor into @DDCMC
while (@@fetch_status=0)
begin
if exists(select distinct DDLX from DDFYZLB_temp_view where CLMC=@CLMC)
begin--存在電鍍類型
declare DDType_Cursor cursor
for
select distinct DDLX from DDFYZLB_temp_view where CLMC=@CLMC
open DDType_Cursor
fetch next from DDType_Cursor into @DDType
while (@@fetch_status=0)
begin
if (@SQL_str='')
set @SQL_str='sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' and DDLX='''+@DDType+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC+'|'+@DDType
else
set @SQL_str=@SQL_str+',sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' and DDLX='''+@DDType+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC+'|'+@DDType
fetch next from DDType_Cursor into @DDType
end
close DDType_Cursor
deallocate DDType_Cursor
end
else
begin --不存在電鍍類型
if (@SQL_str='')
set @SQL_str='sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC
else
set @SQL_str=@SQL_str+',sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC
end
fetch next from DDCMC_Cursor into @DDCMC
end
execute('insert into #MytempTable (Field1)values('+@SQL_str+')');
set @SQL_str=''
fetch next from CL_Cursor into @CLMC
end
close DDCMC_Cursor
deallocate DDCMC_Cursor
close CL_Cursor
deallocate CL_Cursor
set @SQL_str=''
declare @I integer
set @I=0
open SQL_Cursor
fetch next from SQL_Cursor into @tmp_str
while (@@fetch_status=0)
begin
if(@SQL_str='')
set @SQL_str='select YSBH,YSMC,'+@tmp_str+' from DDFYZLB_temp_view group by YSBH,YSMC'
else
begin
set @SQL_str='select A'+rtrim(ltrim(str(@I-1)))+'.*,A'+rtrim(ltrim(str(@I)))+'.* from ('+@SQL_str+') as A'+rtrim(ltrim(str(@I-1)))
set @SQL_str=@SQL_str+' left join (select YSBH as YSBH_Hide,'+@tmp_str+' from DDFYZLB_temp_view group by YSBH) as A'+rtrim(ltrim(str(@I)))+' on A'+rtrim(ltrim(str(@I-1)))+'.YSBH=A'+rtrim(ltrim(str(@I)))+'.YSBH_Hide'
end
set @I=@I+1
fetch next from SQL_Cursor into @tmp_str
end
close SQL_Cursor
deallocate SQL_Cursor
--execute('drop table #MytempTable')
Print @SQL_str
--execute(@SQL_str)
end
-------------------------------------------------------------------------------
在查詢分析器裡面執行提示錯誤
Invalid object name '#MytempTable'.
A cursor with the name 'SQL_Cursor' does not exist.
我在前面已經用execute('select convert(varchar(300),'''') as Field1 into #MytempTable')創建臨時表了,怎麼還會提示Invalid object name '#MytempTable'.
第二個錯誤也類似,為什麼會出現這樣的錯誤?這樣的錯誤該怎麼解決
----------------------------------------------------------------------------
declare @SQL_str varchar(8000)
declare @tmp_str varchar(300)
declare @CLMC sysname
declare @DDCMC sysname
declare @DDType sysname
execute('select convert(varchar(300),'''') as Field1 into #MytempTable')--創建臨時表,存放sql語句
execute('declare SQL_Cursor cursor for select Field1 from #MytempTable')
begin
declare CL_Cursor cursor
for
select distinct CLMC from CLJGB where IsNull(CLMC,'')<>''
open CL_Cursor--打開材料遊標
declare DDCMC_Cursor cursor scroll
for
select b.DDCMC from DDCZLB a left join DDCZLB b on a.DDCBH=b.DDCBH
where a.DDCBH is not NULL order by a.DDCBH
open DDCMC_Cursor--打開電鍍廠名稱遊標
fetch next from CL_Cursor into @CLMC
while (@@fetch_status=0)
begin
fetch absolute 1 from DDCMC_Cursor into @DDCMC
while (@@fetch_status=0)
begin
if exists(select distinct DDLX from DDFYZLB_temp_view where CLMC=@CLMC)
begin--存在電鍍類型
declare DDType_Cursor cursor
for
select distinct DDLX from DDFYZLB_temp_view where CLMC=@CLMC
open DDType_Cursor
fetch next from DDType_Cursor into @DDType
while (@@fetch_status=0)
begin
if (@SQL_str='')
set @SQL_str='sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' and DDLX='''+@DDType+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC+'|'+@DDType
else
set @SQL_str=@SQL_str+',sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' and DDLX='''+@DDType+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC+'|'+@DDType
fetch next from DDType_Cursor into @DDType
end
close DDType_Cursor
deallocate DDType_Cursor
end
else
begin --不存在電鍍類型
if (@SQL_str='')
set @SQL_str='sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC
else
set @SQL_str=@SQL_str+',sum(case when CLMC='''+@CLMC+''' and DDCMC='''+@DDCMC+''' then DDJE else NULL end) as '+@CLMC+'|'+@DDCMC
end
fetch next from DDCMC_Cursor into @DDCMC
end
execute('insert into #MytempTable (Field1)values('+@SQL_str+')');
set @SQL_str=''
fetch next from CL_Cursor into @CLMC
end
close DDCMC_Cursor
deallocate DDCMC_Cursor
close CL_Cursor
deallocate CL_Cursor
set @SQL_str=''
declare @I integer
set @I=0
open SQL_Cursor
fetch next from SQL_Cursor into @tmp_str
while (@@fetch_status=0)
begin
if(@SQL_str='')
set @SQL_str='select YSBH,YSMC,'+@tmp_str+' from DDFYZLB_temp_view group by YSBH,YSMC'
else
begin
set @SQL_str='select A'+rtrim(ltrim(str(@I-1)))+'.*,A'+rtrim(ltrim(str(@I)))+'.* from ('+@SQL_str+') as A'+rtrim(ltrim(str(@I-1)))
set @SQL_str=@SQL_str+' left join (select YSBH as YSBH_Hide,'+@tmp_str+' from DDFYZLB_temp_view group by YSBH) as A'+rtrim(ltrim(str(@I)))+' on A'+rtrim(ltrim(str(@I-1)))+'.YSBH=A'+rtrim(ltrim(str(@I)))+'.YSBH_Hide'
end
set @I=@I+1
fetch next from SQL_Cursor into @tmp_str
end
close SQL_Cursor
deallocate SQL_Cursor
--execute('drop table #MytempTable')
Print @SQL_str
--execute(@SQL_str)
end
-------------------------------------------------------------------------------
在查詢分析器裡面執行提示錯誤
Invalid object name '#MytempTable'.
A cursor with the name 'SQL_Cursor' does not exist.
我在前面已經用execute('select convert(varchar(300),'''') as Field1 into #MytempTable')創建臨時表了,怎麼還會提示Invalid object name '#MytempTable'.
第二個錯誤也類似,為什麼會出現這樣的錯誤?這樣的錯誤該怎麼解決