Solved

Problem with SELECT in cursor

Posted on 2008-10-14
4
167 Views
Last Modified: 2012-05-05
Dear Experts,

I'm having problems with a SELECT statement within a cursor:

This line:  SELECT @deletable = [delete] FROM @tblMain WHERE ID = @ID  returns the same value each time, usually the last record.

Can you use select within a cursor in this way or is there a better way to do it?

Thanks

NIck
DECLARE crsPhrase3 CURSOR FOR

		SELECT ID

		FROM @tblMain WHERE Processed=0

		OPEN crsPhrase3
 

		FETCH NEXT FROM crsPhrase3 INTO @ID
 

			WHILE @@FETCH_STATUS = 0

			BEGIN      --crsPhrase3
 

			SELECT @deletable = [delete] FROM @tblMain WHERE ID = @ID

     IF @deletable .......

Open in new window

0
Comment
Question by:nkewney
  • 2
4 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22709785
you can't have the table name inside a variable
you will have to use dynamic sql
0
 
LVL 1

Author Comment

by:nkewney
ID: 22709807
Hi there,

Thanks for the response.

It exhibits exactly the same behaviour in the following example also.

Nick


		DECLARE crsLeaderboard CURSOR FOR

		SELECT Username

		FROM Users

		OPEN crsLeaderboard
 

		FETCH NEXT FROM crsLeaderboard INTO @un
 

			declare @name varchar(255)

			select @name = Person_nameFirst + ' ' + Person_nameLast FROM Users where Username = @un

Open in new window

0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22755250
what is the problem with this code?
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now