select a.num1,b.num2 into #temp from (
select id,count(*) num1 from t where id in (select id from a) group by id) a
,(
select id,datediff(day,a,b) num2 from c where id in (select id from a)
) b where a.id=b.id
create proc test
as
declare @id int
declare @a int
declare @b int
set @a=0
set @b=0
create table #temp(F1 int ,F2 int)
DECLARE cur_test CURSOR FOR select id from a
OPEN cur_test
FETCH NEXT FROM cur_test into @id
while @@fetch_status=0
begin
select @a=count(*) from t where id=@id
select @b=datediff(day,a,b) from c where c.id=@id
insert #temp values(@a,@b)
FETCH NEXT FROM cur_test into @id
end
select * from #temp
drop table #temp
close cur_test
deallocate cur_test
create table #temp(F1 int ,F2 int)
declare @id int
declare @a int
declare @b int
set @a=0
set @b=0
DECLARE cur_test CURSOR FOR select id from a
OPEN cur_test
FETCH NEXT FROM cur_test into @id
while @@fetch_status=0
begin
select @a=count(*) from t where id=@id
select @b=datediff(day,a,b) from c where c.id=@id
insert #temp values(@a,@b)
FETCH NEXT FROM cur_test into @id
end
close cur_test
deallocate cur_test
create Proc P_GetCount
@id int,
@R_COUNTS int output
as
begin
declare @sql varchar(8000)
select @sql='select'+@R_COUNTS+'=count(*) from b where b.id in (select id from a where id ='+@id +'))'
exec (@sql)
end