Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
Cursors and variables
Cursors and variables
Rate Topic
Display Mode
Topic Options
Author
Message
jsmith7290
jsmith7290
Posted Thursday, May 29, 2008 9:00 AM
Forum Newbie
Group: General Forum Members
Last Login: Tuesday, December 20, 2011 10:48 AM
Points: 1,
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
Post #508460
Christopher Stobbs
Christopher Stobbs
Posted Thursday, May 29, 2008 9:13 AM
SSCommitted
Group: General Forum Members
Last Login: Wednesday, February 24, 2010 4:10 AM
Points: 1,553,
Visits: 2,232
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
Posting Best Practices
Numbers / Tally Tables
SQL-4-Life
Post #508465
Matt Miller (#4)
Matt Miller (#4)
Posted Thursday, May 29, 2008 9:22 AM
SSCertifiable
Group: General Forum Members
Last Login: Friday, May 17, 2013 1:56 PM
Points: 6,997,
Visits: 13,941
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?
Post #508476
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.