Solved

Problem with SELECT in cursor

Posted on 2008-10-14
4
165 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
Comment Utility
you can't have the table name inside a variable
you will have to use dynamic sql
0
 
LVL 1

Author Comment

by:nkewney
Comment Utility
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
Comment Utility
what is the problem with this code?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

744 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

18 Experts available now in Live!

Get 1:1 Help Now