473,729 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with FETCH LAST

Ann
Hello everyone, hope someone can help me with this.

I have a SQL stored procedure that inserts a record into a table,
creates a cursor to fetch the last record that was added to get the
unique key that was created and then writes that and other info to a
separate table. This procedure was working fine at our ISP under NT 4
and SQL 7.

We recently moved to another ISP on servers that are windows 2000 and
SQL 2000. Now this code is going kerplooey. It actually worked fine
in the staging area but now that it was moved into production, it is
not working. also wanted to mention that the production database was
restored from a backup. below is the code.

the first time this is run it is ok, for example the transaction
number is 1. the next time it is run, a new record is created in the
sweep results with a transaction number of 2. but for some reason,
when i declare the cursor to fetch the last record, it goes back to
the transaction number 1 record. so the counts from transaction 1
don't match counts from transaction 2 and the next step has an error
condition and doesn't work.

thanks in advance for any help you can provide

Ann Williams

-- update the sweep results table
INSERT tbl_sweepresult s (del_wrkfeedbac k_count,
updnull_feedbac k_count, swp_feedback_co unt,
swp_count_error , del_error, updnull_error, swp_error, init_error,
sweep_date)
VALUES (@var_del_wrkfe edback_count, @var_updnull_fe edback_count,
@var_swp_feedba ck_count,
@var_swp_count_ error, @var_del_error, @var_updnull_er ror,
@var_swp_error, @var_init_error , GETDATE())
-- create cursor
DECLARE tbl_sweepresult s_cursor SCROLL CURSOR FOR
SELECT transaction_no, sweep_date, init_error, updnull_feedbac k_count
FROM tbl_sweepresult s
OPEN tbl_sweepresult s_cursor
-- get transaction number, sweep date, init error, feedback sweep
count and pass to tbl_currentTran s for OPAL comparison
FETCH LAST FROM tbl_sweepresult s_cursor INTO @var_transactio n_no,
@var_sweep_date , @var_init_error , @var_swp_countz ero
DELETE tbl_currentTran s
INSERT tbl_currentTran s (current_transa ction_no, current_sweep_d ate,
current_init_er ror, current_swp_cou ntzero)
VALUES (@var_transacti on_no, @var_sweep_date , @var_init_error ,
@var_swp_countz ero)
-- close the cursor
CLOSE tbl_sweepresult s_cursor
DEALLOCATE tbl_sweepresult s_cursor
Jul 20 '05 #1
1 4884
Is the value you are trying to retrieve an IDENTITY column? If so,
SCOPE_IDENTITY( ) is what you need. It returns the last inserted identity
value.

Since your cursor declaration doesn't include an ORDER BY clause you've been
lucky that it ever gave a meaningful result. FETCH LAST will just return an
indeterminate row from the table. Moving to another system (perhaps one with
more read-ahead cacheing) has shown up this defficiency which relied on the
engine always returning the last-inserted row.

Q. Why insert the row you've just added into another table
(tbl_currentTra ns)? After all you already have it in a table and you know
the primary key.

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #2

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

Similar topics

6
4790
by: Ravi | last post by:
Hi All, I am trying to execute a select statement using the DBI module of perl in a for loop. I am getting a strange behaviour, the select statement is excuting correctly only for the last element in the for loop. I am including the portion of the code : #Get the connection to the database my $dbh = &getConnection(); my @acodes;
2
8448
by: J.Bijleveld | last post by:
Hello colleagues, At this moment we have a real big problem using a .NET application with an Oracle database (v8.1.6). I hope someone has encountered this problem before and is able to help me with it. The problem is that we *Sometimes* get the ORA-24338 error when executing a query. This problem occurs random, sometimes twice a day, sometimes not at all. In the eventlog we get an exception like this one (stacktrace):
4
2071
by: R. Z. | last post by:
I 've have a stored procedure that compares fields across databases. In order to do so it requires 2 values it acquires from 2 tables. The search is based on the ID of the data owner and a subject: proc_evaluate_results @StudentId = '222222', Course = 'PSY101' In order to obtain those values I run a cursor accross my records and SELECT THEM INTO 2 @variables, which then replace 222222 and PSY101 with dynamic values eg.
12
3262
by: Bart | last post by:
Hallo, I'm have a problem with the following script: function wr(s) { //Just got tired of writing document.write, //so I created a shorthand version document.write(s);
15
3618
by: Hemant Shah | last post by:
Folks, We have an SQL statement that was coded in an application many years ago (starting with DB V2 I think). When I upgraded to UDB 8.2, the optimizer does not use optimal path to access the data. It takes about 4 minutes to get the data. In previous versions it was instantaneous. What do I need to do to increase the performance?
3
2392
by: cnplnsk | last post by:
I have mysql server version: 5.0.15 I am facing this problem last a week about cursor. well ! i have created cursor program as follows... I written this program as external file, named as c:\sqldata\cur_test.sql and compiled it using command mysql> \. c:\sqldata\cur_test.sql #PROGRAM HERE drop procedure if exists cur_test; delimiter $$
1
1818
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any error. But now problem in fetching, not able to fetch data from table correctly. Here is my code. I hav following problems with this coding
2
3172
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if i change something in php file using in ajax function.it not refreshed,means its shows the previous result it not get updated.i can't understand whats the prob.this is the code i m using: <? include("config.inc.php"); //error_reporting(0); ...
14
2638
by: ESHA1 | last post by:
CREATE OR REPLACE procedure TEST_EMAIL(errbuf OUT VARCHAR2, retcode OUT VARCHAR2, p_from_date date, p_to_date date, P_CANDIDATE_NAME VARCHAR2) as x_manager_name1 assignment.manager_name%type; x_EMAIL_ADDRESS candidate.EMAIL_ADDRESS%type; x_candidate_name1 assignment.candidate_name%TYPE; x_customer_name1 assignment.customer_name%TYPE; x_manager_name ...
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9427
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9148
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.