declare @totalCount1 int output
declare @id1 varchar(10)
declare @strsql1 nvarchar(max)=N''
declare @strsql2 nvarchar(max)=N''
select @strsql1=N' SELECT * FROM Table1 where id=@id2 '
select @strsql2=N' SELECT @totalCount2=count(*) FROM Table1 where id=@id '
exec sp_executesql @strsql1,N' @id2 varchar(10) ',@id2=@id1
exec sp_executesql @strsql2,N' @id2 varchar(10),@totalCount2 int out ',@id2=@id1,@totalCount2=totalCount1 output
select @totalCount1