ALTER procedure [dbo].[test]
as
create table #temp(
keyid varchar(8),
artid varchar(8))
Insert Into #temp(keyid,artid)
select keyid,artid
from TC_KNEADBILL
where STATUS in (2)
and artid in (select min(artid) as artid from TC_KNEADBILL where Status = 2 group by BillID)
Insert Into #temp(keyid,artid)
select keyid,artid
from TC_KNEADBILL
where STATUS in (1)
and artid in (select min(artid) as artid from TC_KNEADBILL where Status = 1 group by BillID)
and KEYID not in (select KEYID from #temp)
Insert Into #temp(keyid,artid)
select distinct keyid,artid
from TC_Artbooking
where bookingtime in (select min(BookingTime) as bookingtime from TC_Artbooking where Status = 0 group by BillID)
and artid in (select min(artid) as artid from TC_Artbooking where Status = 0 group by BillID)
and KEYID not in (select KEYID from #temp)
select * from #temp
ALTER procedure [dbo].[test]ascreate table #temp(keyid varchar(8),artid varchar(8))Insert Into #temp(keyid,artid)select keyid,artidfrom TC_KNEADBILLwhere STATUS in (2)and artid in (select mi