Declare @OrdersPerson varchar(max)
select @OrdersPerson= isnull(@OrdersPerson,'')+''''+ cast(t3.temps as nvarchar(4000))+''','
from (select * from
(select count(t.temps) as counts,temps
from  (select *,(
case id when null then '' else id end
+case [money] when null then '' else [money] end
+ case ren when null then '' else ren end
+ case [date] when null then '' else [date] end
+ case bz when null then '' else bz end
+ case kind when null then '' else kind end
+ case ren2 when null then '' else ren2 end
+ case item when null then '' else item end
+ case paytype when null then '' else paytype end
+ case jdd when null then '' else jdd end
)as temps 
from dindanbukuan) as t  group by t.temps)as t2
where counts>=2) as t3
if substring(@OrdersPerson,len(@OrdersPerson),1) = ','  
begin
	set @OrdersPerson = left(@OrdersPerson,len(@OrdersPerson) - 1)
end

set @OrdersPerson ='
Declare @tempStr varchar(max);
select @tempStr= isnull(@tempStr,'''')+''''''''+ cast(tb2.autoid as nvarchar(4000))+'''''',''
 from (select autoid from (select * from  
		  (
		  select *,(
case id when null then '''' else id end
+case [money] when null then '''' else [money] end
+ case ren when null then '''' else ren end
+ case [date] when null then '''' else [date] end
+ case bz when null then '''' else bz end
+ case kind when null then '''' else kind end
+ case ren2 when null then '''' else ren2 end
+ case item when null then '''' else item end
+ case paytype when null then '''' else paytype end
+ case jdd when null then '''' else jdd end
)as temps 
from dindanbukuan
) as tt
where temps in('+@OrdersPerson+') ) as tb where time is null) as tb2
if substring(@tempStr,len(@tempStr),1) = '',''  
begin
	set @tempStr = left(@tempStr,len(@tempStr) - 1)
end
select @tempStr
--set @tempStr=''select * from dindanbukuan where autoid in(''+@tempStr+'')''
set @tempStr=''delete dindanbukuan where autoid in(''+@tempStr+'')''
exec(@tempStr)
'
exec(@OrdersPerson)
