troubleshooting Question

I am having a problem to use a declared table in a dynamic sql statement

Avatar of sohairzaki2005
sohairzaki2005Flag for United States of America asked on
Microsoft SQL Server 2008
2 Comments1 Solution280 ViewsLast Modified:
I am having a problem to use a declared table in a dynamic sql statement
please see my code I am getting an error saying must declare @id1

declare @id1 table (id int, sp2sectionid int,sp2lessonid int, learningobjectcontent nvarchar(20))

DECLARE  @columnname     nvarchar(50)
declare @mysql nvarchar(1000)
 
--declare the cursor
DECLARE @LearningObject CURSOR
SET @LearningObject =cursor
 FOR

   SELECT  column_name from INFORMATION_SCHEMA.COLUMNS
   where TABLE_CATALOG=’xxxxx’
   and TABLE_NAME =’xxxxxxccccc’
   and substring(column_name,1,7)='BitRef_'

OPEN  @Learningobject
Fetch next
from @LearningObject Into @ColumnName


WHILE @@Fetch_Status = 0

   BEGIN


   set @mysql=N'
  insert into @id1
  (
 
  SP2LessonID,
  SP2SectionID,  
  LearningObjectContent)
  (
   select '
   +''''+substring(@ColumnName,8,30)+''' ,'+'ID ,SectionID ,'+
   @columnname+' from ffffffff where id='+cast(@SP2LessonID as nvarchar(10))+'
   )
  '
 
exec (@mysql)
   
                     
  fetch next
  from @learningObject into @columnname
 

   END

CLOSE @learningobject

DEALLOCATE @learningobject
Thanks
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros