You need a temp table.Just like this.
declare @sqlstring nvarchar(2000)
select * into dbo.# from employees where 1=2
set @sqlstring = 'select * from employees'
insert into # exec (@sqlstring)
declare cursTemp Cursor for select * from #
drop table #
--------------------------------------------------------------
--------------------------------------------------------------
[quoted text, click to view] "Uri Dimant" wrote:
> Chandra
> If I remember well it is called dynamic cursor and I'm sure BOL have some
> topics to describe it.
>
> EXEC ('declare cursTemp Cursor for '+@sqlstring)
>
>
> "Chandra" <Chandra@discussions.microsoft.com> wrote in message
> news:52BC15D1-165C-4832-AF2F-B98F1E977287@microsoft.com...
> >I wrote the following statements..
> >
> > set @sqlstring = 'select * from employee'
> >
> > declare cursTemp Cursor for @sqlstring
> >
> > ...
> >
> > in sql server it is giving error.. what is wrong with this? How can I use
> > string variable for declaring cursors?
> >
> > Thanks
> > Chandra
> >
> >
>
>
First you should ask yourself if there is a better solution. Cursors are
almost never necessary and are rarely a good idea. Dynamic cursors are
particularly undesirable.
If you want help to find a better way then please give us some more
information. DDL and sample data would help.
--
David Portas
SQL Server MVP
--
[quoted text, click to view] "Chandra" wrote:
> I wrote the following statements..
>
> set @sqlstring = 'select * from employee'
>
> declare cursTemp Cursor for @sqlstring
>
> ...
>
> in sql server it is giving error.. what is wrong with this? How can I use
> string variable for declaring cursors?
>
> Thanks
> Chandra
>
Chandra
If I remember well it is called dynamic cursor and I'm sure BOL have some
topics to describe it.
EXEC ('declare cursTemp Cursor for '+@sqlstring)
[quoted text, click to view] "Chandra" <Chandra@discussions.microsoft.com> wrote in message
news:52BC15D1-165C-4832-AF2F-B98F1E977287@microsoft.com...
>I wrote the following statements..
>
> set @sqlstring = 'select * from employee'
>
> declare cursTemp Cursor for @sqlstring
>
> ...
>
> in sql server it is giving error.. what is wrong with this? How can I use
> string variable for declaring cursors?
>
> Thanks
> Chandra
>
>
Don't see what you're looking for? Try a search.