473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Cursor and row retrieval question

Hi everyone, I have a cursor that goes through one table and does a
comparison with another table, code below
==============
CREATE PROCEDURE spMyDataBaseMatch AS
set nocount on
Declare @FirstName varchar(100)
Declare @LastName varchar(100)
Declare @PhoneNumber varchar(100)
Declare @CourseCode varchar(50)
Declare myCursor Cursor FOR
select FirstName, LastName, PhoneNumber from person
Open myCursor
Fetch Next From myCursor INTO @FirstName, @LastName, @PhoneNumber
While @@fetch_status = 0
BEGIN
Select PERLEGALFIRSTNAMETEXT, PERLEGALSURNAMETEXT, OTEINTERNALIDCODE from
MyDataBase6.dbo.vwEnquiriesAllStudents WHERE PERLEGALFIRSTNAMETEXT =
@FirstName AND PERLEGALSURNAMETEXT = @LastName
if @@ROWCOUNT > 0
BEGIN
-----------------------------------------------------
-- I have a question about this next piece
-----------------------------------------------------
SELECT @CourseCode = OTEINTERNALIDCODE FROM
MyDataBase6.dbo.vwEnquiriesAllStudents WHERE PERLEGALFIRSTNAMETEXT =
@FirstName AND PERLEGALSURNAMETEXT = @LastName
-----------------------------------------------------
-- End question
-----------------------------------------------------

insert into MyDataBaseMatch VALUES (@FirstName + ' ' +
@LastName,@CourseCode )
END
Fetch Next From myCursor INTO @FirstName, @LastName, @PhoneNumber
END
deallocate myCursor
GO
=================

Is it possible for me to associate an attribute value in a Select statement?
Like the following

Select @CourseCode = OTEINTERNALIDCODE, PERLEGALFIRSTNAMETEXT,
PERLEGALSURNAMETEXT, from MyDataBase6.dbo.vwEnquiriesAllStudents WHERE
PERLEGALFIRSTNAMETEXT = @FirstName AND PERLEGALSURNAMETEXT = @LastName

instead of what I have written between the tags?
-----------------------------------------------------
--
-----------------------------------------------------

Thanks in advance
Mark


Jul 20 '05 #1
1 1624
Always avoid cursors where you can. It looks like your proc can be
re-written as a single INSERT statement that will perform far more
efficiently:

CREATE PROCEDURE spMyDataBaseMatch AS

SET NOCOUNT ON

INSERT INTO MyDataBaseMatch
SELECT firstname + ' ' + lastname, S.oteinternalidcode
FROM Person AS P
JOIN vwEnquiriesAllStudents AS S
ON P.firstname = S.perlegalfirstnametext
AND P.lastname = S.perlegalsurnametext

GO

In answer to the question I think you are asking, variables can be assigned
in a SELECT statement but only if the SELECT doesn't otherwise need to
return any rows to the client. For example, this is valid syntax:

SELECT @CourseCode = oteinternalidcode,
@firstname = perlegalfirstnametext,
@lastname = perlegalsurnametext
FROM vwEnquiriesAllStudents
WHERE ...

but this is not:

SELECT @CourseCode = oteinternalidcode,
perlegalfirstnametext, perlegalsurnametext
FROM vwEnquiriesAllStudents
WHERE ...

Hope this helps.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark | last post by:
Hi everyone, I have a cursor that goes through one table and does a comparison with another table, code below ============== CREATE PROCEDURE spMyDataBaseMatch AS set nocount on Declare...
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
2
by: Joe | last post by:
How can I control the cursor type in a webform - for instance I have a form that executes a function to retrieve rows from an oracle database and populate a grid with the results. I would like to...
3
by: Simon Wigzell | last post by:
I have an image with several links in it in mapped areas. I am using a custom cursor on the page and would like to have a custom cursor appear on mouse over of the mapped links. Something like: ...
7
by: P. Adhia | last post by:
Sorry for quoting an old post and probably I am reading out of context so my concern is unfounded. But I would appreciate if I can get someone or Serge to confirm. Also unlike the question asked in...
3
by: IW2FIV | last post by:
I'm a really beginner about sql2000. During my test I have created the following query. It's works ok until I do't add the code included in section A, when I add it the i obtain the error: Cursor...
3
by: Greg Smith | last post by:
I would like to set an hourglass style cursor during long operations like sql data retrieval. I this do-able? Any help is greatly appreciated.
0
by: JosAH | last post by:
Greetings, Introduction At the end of the last Compiler article part I stated that I wanted to write about text processing. I had no idea what exactly to talk about; until my wife commanded...
6
by: asp2 | last post by:
I have a cursor inside a program unit in Oracle Forms 6 and its supposed to get one row from this cursor .. but in some values (only some and others not) it gets the same row twice (duplicated) in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.