在执行计划里会提示
对象名 '#tt' 无效。 不知为什么.create table dbo.base_flow(pro_no varchar(30),ver varchar(10),itemno smallint,itemorder numeric(5,2)) 
go
insert into dbo.base_flow 
select 'aa' ,'a', 101,1 union all
select 'aa' ,'a', 102,2 union all
select 'aa' ,'a', 103,3 union all
select 'aa' ,'a', 101,4 union all
select 'bb' ,'a', 102,1 union all
select 'bb' ,'a', 105,2 union all
select 'bb' ,'a', 106,3 union all
select 'bb' ,'a', 103,4 
go
create 
--alter
 procedure dbo.flow_available   
@pro_no  varchar(30),
@ver varchar(10)
as
set nocount on
declare @minorder  numeric(5,2) ,
          @maxorder  numeric(5,2) ,
                 @itemno  smallint  create table #tt(itemno  smallint ,id int ,id2 varchar(11)) insert into #tt(itemno)  
select itemno 
from dbo.base_flow 
where pro_no = @pro_no and ver = @ver 
group by itemno 
having count(1)>1 

declare cur cursor for 
select itemno 
from #tt 

open cur fetch next from cur into
@itemno

while (@@fetch_status = 0)
begin
select @maxorder = max(itemorder) ,@minorder = min(itemorder) 
from dbo.base_flow 
where pro_no = @pro_no and ver = @ver 
                         and  itemno = @itemno
group by itemno 

if exists (
select 1 from dbo.base_flow 
  where pro_no = @pro_no and ver = @ver 
  and  itemno <> @itemno 
   and itemorder  between @maxorder and @minorder )
  begin
   select  '出错...........'
   drop table #tt
  return 
  end
end
fetch next from cur into @itemno close cur
deallocate cur
drop table #tt
set nocount offGO -- EXEC dbo.flow_available 'aa', 'A'