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 2000 Forums
 Transact-SQL (2000)
 Cursor in a Function

Author  Topic 

real_pearl
Posting Yak Master

106 Posts

Posted - 2004-08-13 : 05:57:49
Can I use a cursor within a function, if yes how?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-13 : 06:09:37
i've never tried it (any cursor for that matter :)), but as cursors are opend for a resultset, i don't see why you couldn't...

and what are you trying to do with cursor?? maybe it can be set-solved :)

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

real_pearl
Posting Yak Master

106 Posts

Posted - 2004-08-13 : 06:15:16
I am getting this error
Cannot access temporary tables from within a function.

when I create a temporary table and run select query
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-13 : 06:19:19
i guessing your using a temp table with one # as in #MyTempTable?
i think that to use a temp table in a function you need to use ##MyTempTable.


Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

real_pearl
Posting Yak Master

106 Posts

Posted - 2004-08-13 : 07:18:23
No i was using ##Temp table but still got that error. Any ways I changed the approach and passed the table name of a permanent table, but with this its giving me error

Server: Msg 443, Level 16, State 2, Procedure UDF_TEST, Line 35
Invalid use of 'EXECUTE' within a function.


The stmt is

EXEC('DECLARE curLabelFields CURSOR SCROLL FOR' +
' SELECT ' + @column_name + ' FROM ' + @table_name)
Go to Top of Page

real_pearl
Posting Yak Master

106 Posts

Posted - 2004-08-13 : 07:18:55
No i was using ##Temp table but still got that error. Any ways I changed the approach and passed the table name of a permanent table, but with this its giving me error

Server: Msg 443, Level 16, State 2, Procedure UDF_TEST, Line 35
Invalid use of 'EXECUTE' within a function.


The stmt is

EXEC('DECLARE curLabelFields CURSOR SCROLL FOR' +
' SELECT ' + @column_name + ' FROM ' + @table_name)
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-08-13 : 07:23:57
look up this in BOL:
functions, user-defined

allowed:
EXECUTE statements calling an extended stored procedures.

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page
   

- Advertisement -