Please consider the following:
declare @abbrev varchar(20); set @abbrev='';
select pk_term into #t2 from #t1 where pk_term in (select distinct(fk_term) from tblabbreviations where abbreviation like @abbrev)
select @count=count(*) from #t2
print 'count t2='+convert(varchar(10),@count)
Assume #t1 contains 10 rows. I want #t2 to contain 10 rows when @abbrev is ”, or 10 rows or less (typically less) when say @abbrev=’av%’.
I tried this:
declare @abbrev varchar(20); set @abbrev='';
if @abbrev <> ''
begin
select pk_term into #t2 from #t1 where pk_term in (select distinct(fk_term) from tblabbreviations where abbreviation like @abbrev)
select @count=count(*) from #t2 -- should be same as t1
print 'count t2='+convert(varchar(10),@count)
end
else
select pk_term into #t2 from (select pk_term = null) -- ensure #t2 is created regardless
but of course I get the error ‘There is already an object named ‘#t2′ in the database.’
How do I get around this issue?
Many thanks in advance.
Rgds, Mark
You use
CREATE TABLE #t2to create #t2 ahead of using it, rather than having it created as a result of anSELECT ... INTO.e.g.