Home » SQL & PL/SQL » SQL & PL/SQL » Update statement updates all rows (Oracle 7)
Update statement updates all rows [message #630807] Mon, 05 January 2015 08:45 Go to next message
rmccleave559
Messages: 5
Registered: January 2015
Junior Member
Hi basically I have a procedure wrote in a package but it seems to update all rows instead of the one used in the where clause can anyone tell me why?

PROCEDURE UPDATE_DETAILS(id NUMBER, membershipNumber VARCHAR2, surname VARCHAR2, forename VARCHAR2,changeStatus VARCHAR2) AS
  BEGIN
    IF CHANGESTATUS = 'true' THEN
    UPDATE members
    SET MEMBERSHIP_NO = membershipNumber, SURNAME = surname, FORENAME = forename, SUBMITTED_DATE = SYSDATE, CHANGED_DATE = SYSDATE
    WHERE ID = id;
  ELSE
    UPDATE members
    SET MEMBERSHIP_NO = membershipNumber, SURNAME = surname, FORENAME = forename, SUBMITTED_DATE = SYSDATE
    WHERE ID = id;
  END IF;

END UPDATE_DETAILS;
Re: Update statement updates all rows [message #630811 is a reply to message #630807] Mon, 05 January 2015 08:51 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Welcome to this forum.

We don't have your table.
We don't have your data.

Without the above we can't run your code or know how to make it better.
Re: Update statement updates all rows [message #630813 is a reply to message #630811] Mon, 05 January 2015 08:51 Go to previous messageGo to next message
rmccleave559
Messages: 5
Registered: January 2015
Junior Member
I can't post the data its confidential!
Re: Update statement updates all rows [message #630814 is a reply to message #630807] Mon, 05 January 2015 08:52 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
I suspect your problem is one of syntax. This will cause a problem:

WHERE ID = id;


[Edit: typo]

[Updated on: Mon, 05 January 2015 08:54]

Report message to a moderator

Re: Update statement updates all rows [message #630816 is a reply to message #630813] Mon, 05 January 2015 08:53 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Surely a mock test case with dummy data which reproduces the issue isn't impossible?
Re: Update statement updates all rows [message #630817 is a reply to message #630814] Mon, 05 January 2015 08:56 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Not reserved words - name scope.
If you have a name in a select statement the first thing oracle will do is see if that name corresponds to a column name in one of the tables referenced in the query. If it doesn't oracle will then check to see if the name refers to a variable.
So what you've written is basically:
UPDATE members
SET .....
WHERE members.ID = members.id;


Most oracle shops have coding standards that say parameters and variables should have a prefix to make it obvious what they are and avoid this problem.
Re: Update statement updates all rows [message #630818 is a reply to message #630817] Mon, 05 January 2015 08:57 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>(Oracle 7)
REALLY?

post full results from SQL below


SELECT * FROM V$VERSION;
Re: Update statement updates all rows [message #630820 is a reply to message #630817] Mon, 05 January 2015 09:01 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
Quote:
Not reserved words - name scope.

Bingo Smile
Re: Update statement updates all rows [message #630822 is a reply to message #630816] Mon, 05 January 2015 09:04 Go to previous messageGo to next message
rmccleave559
Messages: 5
Registered: January 2015
Junior Member
ID (NUMBER) MembershipNo (VARCHAR2(15 BYTE)) Forname (VARCHAR2(20 BYTE)) Surname (VARCHAR2(20 BYTE)) SubmittedDate(VARCHAR2(20 BYTE)) ChangedDate(VARCHAR2(20 BYTE))
10 1 Alan Zalan 05-JAN-15 05-JAN-15
11 2 Balan Yalan 05-JAN-15 05-JAN-15
12 3 Calan Xalan 05-JAN-15 05-JAN-15
13 4 Dalan Walan 05-JAN-15 05-JAN-15
14 5 Elan Vlan 05-JAN-15 05-JAN-15
15 6 Flan Ulan 05-JAN-15 05-JAN-15

Okay theres some dummy data along with the column info

gazzag: The column is not actually called ID
Re: Update statement updates all rows [message #630823 is a reply to message #630822] Mon, 05 January 2015 09:06 Go to previous messageGo to next message
rmccleave559
Messages: 5
Registered: January 2015
Junior Member
BlackSwan: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production

Re: Update statement updates all rows [message #630824 is a reply to message #630822] Mon, 05 January 2015 09:07 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>ID (NUMBER)
>The column is not actually called ID

Please stop lying to us.

Problem Exists Between Keyboard And Chair.
Re: Update statement updates all rows [message #630825 is a reply to message #630822] Mon, 05 January 2015 09:07 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
But is the column the same name as the parameter?
If it is - that's the problem.
If it isn't - you're wasting our time posting code that doesn't represent what you are actually running.
Re: Update statement updates all rows [message #630826 is a reply to message #630822] Mon, 05 January 2015 09:07 Go to previous messageGo to next message
gazzag
Messages: 1118
Registered: November 2010
Location: Bedwas, UK
Senior Member
A reply will be more forthcoming if you supply actual CREATE TABLE and INSERT statements.

HTH
-g
Re: Update statement updates all rows [message #630830 is a reply to message #630826] Mon, 05 January 2015 09:29 Go to previous messageGo to next message
rmccleave559
Messages: 5
Registered: January 2015
Junior Member
cookiemonster: thanks that was the problem!

And sorry guys thanks for your help!
Re: Update statement updates all rows [message #630831 is a reply to message #630830] Mon, 05 January 2015 09:38 Go to previous message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You'll have the same problem with the set clause by the way.
Previous Topic: String to Number conversion
Next Topic: Insert Query in between rows - SQL
Goto Forum:
  


Current Time: Thu Apr 25 06:19:17 CDT 2024