dbTalk Databases Forums  

Bitwise manipulation for an update statement.

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Bitwise manipulation for an update statement. in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Adrian Scott-Day
 
Posts: n/a

Default Bitwise manipulation for an update statement. - 12-01-2004 , 11:05 AM






Could anyone shead some light on this problem that I'm faced with.

I have a stored procedure that has 2 parameters. The first is the
primary key for the table that is to be updated. The second is an
integer. This integer is actually a bitwise status flag.(1=locked,
2=reviewed, 4=invalid...)

The issue that I am facing is that I need to change the value for a
large number of fields within this particular table, but only modify
the relevant bits.
For example record1 has a value of 4 in it's statusA field which means
it is marked as invalid but not locked and not reviewed.
Record1 has a value of 5 in it's statusB field which means it is
marked as invalid and locked but not reviewed.
The stored procedure then passes the primary key to select record1 and
an integer value of 1, thereby indicating that the "locked" bit of the
statusA and statusB fields should be set.

How can I do this?

Reply With Quote
  #2  
Old   
KurtisK
 
Posts: n/a

Default Re: Bitwise manipulation for an update statement. - 12-01-2004 , 04:20 PM






update your_table
set StatusA = StatusA + case bitand(StatusA, 1) when 0 then 1 else 0 end
set StatusB = StatusB + case bitand(StatusB, 1) when 0 then 1 else 0 end
where id = your_id


Add 1 to each bit field if it is not currently locked .... this seems like
the solution for what you described?

Kurt


--


----------------------------------------------------
This mailbox protected from junk email by MailFrontier Desktop
from MailFrontier, Inc. http://info.mailfrontier.com

"Adrian Scott-Day" <adrian.scott.day (AT) gmail (DOT) com> wrote

Quote:
Could anyone shead some light on this problem that I'm faced with.

I have a stored procedure that has 2 parameters. The first is the
primary key for the table that is to be updated. The second is an
integer. This integer is actually a bitwise status flag.(1=locked,
2=reviewed, 4=invalid...)

The issue that I am facing is that I need to change the value for a
large number of fields within this particular table, but only modify
the relevant bits.
For example record1 has a value of 4 in it's statusA field which means
it is marked as invalid but not locked and not reviewed.
Record1 has a value of 5 in it's statusB field which means it is
marked as invalid and locked but not reviewed.
The stored procedure then passes the primary key to select record1 and
an integer value of 1, thereby indicating that the "locked" bit of the
statusA and statusB fields should be set.

How can I do this?



Reply With Quote
  #3  
Old   
DA Morgan
 
Posts: n/a

Default Re: Bitwise manipulation for an update statement. - 12-01-2004 , 07:10 PM



Adrian Scott-Day wrote:

Quote:
Could anyone shead some light on this problem that I'm faced with.

I have a stored procedure that has 2 parameters. The first is the
primary key for the table that is to be updated. The second is an
integer. This integer is actually a bitwise status flag.(1=locked,
2=reviewed, 4=invalid...)

The issue that I am facing is that I need to change the value for a
large number of fields within this particular table, but only modify
the relevant bits.
For example record1 has a value of 4 in it's statusA field which means
it is marked as invalid but not locked and not reviewed.
Record1 has a value of 5 in it's statusB field which means it is
marked as invalid and locked but not reviewed.
The stored procedure then passes the primary key to select record1 and
an integer value of 1, thereby indicating that the "locked" bit of the
statusA and statusB fields should be set.

How can I do this?
I am completely unclear as to what it is you are doing and why you have
any problem at all.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace 'x' with 'u' to respond)


Reply With Quote
  #4  
Old   
adrian.scott.day@gmail.com
 
Posts: n/a

Default Re: Bitwise manipulation for an update statement. - 12-02-2004 , 04:59 AM



Ok, sorry if I was unclear but Oracle programming is new to me.

The situation is this. I have a stored procedure which gets called
repeatedly from a cursor. This stored procedure has 2 parameters. One
being an id and the other being an integer. In the stored procedure I
need to update about 50 columns for the record with the id that matches
parameter 1.

Because there are so many columns that have to be updated, I was hoping
that I wouldn't have to do a select statement to retrieve the existing
values of those columns first.

Can you see my problem now?


Reply With Quote
  #5  
Old   
Connor McDonald
 
Posts: n/a

Default Re: Bitwise manipulation for an update statement. - 12-12-2004 , 06:39 AM



Adrian Scott-Day wrote:
Quote:
Could anyone shead some light on this problem that I'm faced with.

I have a stored procedure that has 2 parameters. The first is the
primary key for the table that is to be updated. The second is an
integer. This integer is actually a bitwise status flag.(1=locked,
2=reviewed, 4=invalid...)

The issue that I am facing is that I need to change the value for a
large number of fields within this particular table, but only modify
the relevant bits.
For example record1 has a value of 4 in it's statusA field which means
it is marked as invalid but not locked and not reviewed.
Record1 has a value of 5 in it's statusB field which means it is
marked as invalid and locked but not reviewed.
The stored procedure then passes the primary key to select record1 and
an integer value of 1, thereby indicating that the "locked" bit of the
statusA and statusB fields should be set.

How can I do this?
check out the bitand function
--
Connor McDonald
Co-author: "Mastering Oracle PL/SQL - Practical Solutions"
ISBN: 1590592174

web: http://www.oracledba.co.uk
web: http://www.oaktable.net
email: connor_mcdonald (AT) yahoo (DOT) com

Coming Soon! "Oracle Insight - Tales of the OakTable"

"GIVE a man a fish and he will eat for a day. But TEACH him how to fish,
and...he will sit in a boat and drink beer all day"

------------------------------------------------------------


Reply With Quote
  #6  
Old   
Noons
 
Posts: n/a

Default Re: Bitwise manipulation for an update statement. - 12-12-2004 , 02:58 PM



adrian.scott.day (AT) gmail (DOT) com wrote:
Quote:
Because there are so many columns that have to be updated, I was
hoping
that I wouldn't have to do a select statement to retrieve the
existing
values of those columns first.
With SQL, you NEVER, EVER have to select the columns
that you want to update: the UPDATE statement does not have
to read the columns before updating them. That is one of the
biggest differences between traditional file processing and
SQL.

If it is a bit-flip you want, you can use the BITAND
function like Connor suggested.

If you really want to do raw binary, you can use the
UTL_RAW package which gives you access to BITOR, BITAND
and BITXOR as well as a host of others. But you may have
to do some manipulations to make sure the type of the
parameters and results is consistent with RAW.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.