Sql cursor Declare

I am trying to declare a cursor in a stored procedure that is a little different from the standard cursor declare in that
I would like it make use of a stored procedure parameter (@ConfigurationFilename) and include a like as part of the where clause.  It is the where clause with the single quotes that is causing me grief.  I thought it was a matter of just using 2 double quotes but getting syntax errors'

Here is the select clause I would like to use for the cursor


select
   syscolumns.name as [Column],
   syscolumns.xusertype as [Type],
   sysobjects.xtype as [Objtype]
from
   sysobjects, syscolumns
where sysobjects.id = syscolumns.id
and   sysobjects.xtype = 'u'
and   sysobjects.name = ' +  @ConfigurationFilename +
' and syscolumns.name like '%FileFolder%''

I tried using

DECLARE @cursor VARCHAR(1000)

SET @cursor = 'DECLARE Files_cursor CURSOR
    FOR select
   syscolumns.name as [Column],
   syscolumns.xusertype as [Type],
   sysobjects.xtype as [Objtype]
from
   sysobjects, syscolumns
where sysobjects.id = syscolumns.id
and   sysobjects.xtype = 'u'
and   sysobjects.name = ' + @ConfigurationFilename
and syscolumns.name like '%FileFolder%'


EXEC (@cursor)

OPEN Files_cursor

(for sake of space I left off  the rest of the cursor)
johnnyg123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
You cannot do it like this. The cursor variable scope is over once the exec is completed.

Please consider to avoid cursor anyhow
http://mobile.experts-exchange.com/Database/MS-SQL-Server/A_13640-processing-cursor-vs-temp-table-syntax.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John_VidmarCommented:
I don't think you need Dynamic-SQL, try this:
SELECT	[Column]	=	b.name
,	[Type]		=	b.xusertype
,	[Objtype]	=	a.xtype
FROM	sysobjects	a
JOIN	syscolumns	b	ON	a.id = b.id
WHERE   a.xtype = 'u'
AND	a.name = @ConfigurationFilename
AND	b.name LIKE '%FileFolder%'

Open in new window

0
HuaMin ChenProblem resolverCommented:
Hi,
I suggest you please do not try to handle the cursor dynamically.

If you really need to use Dynamic code, you can instead dynamically create one table to hold the records and further use Cursor to retrieve the records from that.
0
johnnyg123Author Commented:
Very interesting....Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.