May 29, 2008 at 9:01 am
Hi,
Im trying to use a variable within a cursor but I actually populate the variable after the cursor declaration and it doesn't appear to use the value. I created code for a sample as the code this is not working in is rather long and would be confusing if I posted it
My question is ...
Is this NORMAL behavior ?
For example
declare @tab_name varchar(200), @res_name varchar(200)
--set @tab_name = 'sysowners' -- uncomment this line and the cursor will return a value
--
declare sample cursor for
select name from sysobjects
where lower(name) = lower(@tab_name)
--
set @tab_name = 'sysowners'
open sample
fetch sample into @res_name
while @@fetch_status = 0
begin
print @res_name
fetch sample into @res_name
end
close sample
deallocate sample
May 29, 2008 at 9:13 am
Hi,
Not sure if this is normal.
I have never used a cursor in my life 🙂 (very happy about that)
Do you have to use a cursor for this problem?
Thanks
Chris
----------------------------------------------
Try to learn something about everything and everything about something. - Thomas Henry Huxley
:w00t:
Posting Best Practices[/url]
Numbers / Tally Tables[/url]
May 29, 2008 at 9:22 am
I think you're misunderstanding what is going on. The DECLARE CURSOR does use the variable as it's defined at the time when the DECLARE is issued. Meaning - in programmer terminology - the DECLARE CURSOR sets up and initializes the cursor in one statement. There's no deferred resolution going on. The only thing that hasn't happened yet is that the cursor hasn't been materialized (which occurs with the OPEN statement).
In this case you get nothing, since at the time of the declare - the variable is NULL.
This is normal behavior and to be expected.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply