sql - Invalid Object name "myTempCategoryTable" -
when execute stored procedure many of lines getting executed @ line giving error "invalid object name mytempcategorytable
". please find part of stored procedure getting error. same stored procedure works other database 1 database doesn't. there permission issue? please help.
sql server stored proc:
set @sql= 'create table mytempcategorytable(monthno int,months nvarchar(200),session_id nvarchar(200),' select @min_sno= min(sno) #tempcategory sessionid=@sessionid select @max_sno= max(sno) #tempcategory sessionid=@sessionid while(@min_sno <= @max_sno ) begin select @cur_column= textval #tempcategory sessionid=@sessionid , sno = @min_sno if(@min_sno <= @max_sno ) begin set @sql = @sql + replace (replace(@cur_column,'-','_'),'\','') + ' nvarchar(200),' end else begin set @sql = @sql + replace (replace(@cur_column,'-','_'),'\','') + ' nvarchar(200)' end set @min_sno = @min_sno+1 end set @sql = @sql + ')' exec (@sql) end select b.monthno,b.months,a.sessionid,a.textval, b.totalcost #c #tempcategory inner join #temp b on a.ocbprocessid=b.ocbprocessid order b.ocbprocessid declare @dynamicpivotquery nvarchar(max) declare @columnname nvarchar(max) select @columnname= isnull(@columnname + ',','') + platformarea (select distinct '"'+ case when pm.ocbprocessdesc='test preparation' 'test_preparation' when pm.ocbprocessdesc='test excecution' 'test_excecution' when pm.ocbprocessdesc='test debug activities' 'test_debug_activities' when pm.ocbprocessdesc='test reporting' 'test_reporting' end +'"' platformarea ocbprocessdetails pm) weeks print @columnname declare @table table ( id varchar(50) ) declare @x int = 0 declare @firstcomma int = 0 declare @nextcomma int = 0 set @x = len(@columnname) - len(replace(@columnname, ',', '')) + 1 -- number of ids in id_list while @x > 0 begin set @nextcomma = case when charindex(',', @columnname, @firstcomma + 1) = 0 len(@columnname) + 1 else charindex(',', @columnname, @firstcomma + 1) end insert @table values ( substring(@columnname, @firstcomma + 1, (@nextcomma - @firstcomma) - 1) ) set @firstcomma = charindex(',', @columnname, @firstcomma + 1) set @x = @x - 1 end set @dynamicpivotquery = n'insert mytempcategorytable select * #c pivot (sum(totalcost) textval in ('+@columnname+')) p' print @dynamicpivotquery exec sp_executesql @dynamicpivotquery
Comments
Post a Comment