Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Incorrect syntax near ' + @tabnam + '

Author  Topic 

phefos
Starting Member

1 Post

Posted - 2012-03-12 : 08:42:25

Good day,

Am trying to retrive data in different tables using dynamic sql and am getting the following error message: Incorrect syntax near ' + @tabnam + '. The error is around the statemet I marked error - SELECT studentno, idnumber from quedb.dbo.' + @tabnam + '

Your assistance is highly appreciated.

My code is as follows

Declare Queue_Cursor CURSOR for
SELECT quenam, queid, tabnam from quedb.dbo.ques where quenam in (select name from vs.dbo.namemgr where type = 'queue' and subtype = 'app')
and quenam in (select que_name from domain_queues where domain = 2)

declare @number char(8)
declare @idnumber char(15)
declare @quenam char(50)
declare @tabnam varchar(50)
Open Queue_Cursor
Fetch Next from Queue_Cursor ---read from cursor
Into @quenam, @quied, @tabnam
While @@Fetch_Status = 0

PRINT ' '

SELECT @message = '----- Records for Que: ' +
@quenam
DECLARE CasesPerQue_cursor CURSOR FOR

'--Error SELECT studentno, idnumber from quedb.dbo.' + @tabnam + '
OPEN CasesPerQue_Cursor

FETCH NEXT FROM CasesPerQue_Cursor INTO @number, @idnumber

--Print idnumbers for worklist


WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @message = ' ' + @number + ' ' +@idnumber
PRINT @message
FETCH NEXT FROM CasesPerQue_Cursor INTO @number, @idnumber

END

CLOSE CasesPerQue_Cursor
DEALLOCATE CasesPerQue_Cursor

-- Get the next QUEUE.
FETCH NEXT FROM Queue_Cursor
Into @quenam, @quied, @tabnam
END

CLOSE Queue_Cursor
DEALLOCATE Queue_Cursor

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-03-12 : 10:08:43
sorry didnt understand your query

so are you trying to populate cursor using synamic query?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -