Solved

Problem with SELECT in cursor

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

778 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