Solved

Problem with SELECT in cursor

Posted on 2008-10-14
4
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

710 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