SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cursors and variables


Cursors and variables

Author
Message
jsmith7290
jsmith7290
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 12
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
Christopher Stobbs
Christopher Stobbs
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2436 Visits: 2233
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
Numbers / Tally Tables

SQL-4-Life
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12249 Visits: 18574
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?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search