Solved

Problem with SELECT in cursor

Posted on 2008-10-14
4
174 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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…
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

821 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