Update using a function in the select statement
HelloIs it possible to do the follwing.
I tabele and a custom function, the custome service will update the column (amount) of the table based on the value of going through the function of the select statement.
SELECT id, stock, where an id = get_update (id);
Now
Get_update function will return the same id I'm passing and update the column amount to a value in the
table a.
When I run the satament selection I have the updated data in the column amount in my first executtion, but if execute the same statement, again, I see the changes reflected. is it possible to get data updates to the first performance himself.
the function is of type PRAGMA AUTONOMOUS_TRANSACTION
Concerning
Indeed a strange requirement and probably not the way to go in a production environment. But anyway is a way to achieve your goal
SQL> create table a
as
select 12 id, 500 amount from dual
union all
select 13 id, 600 amount from dual
/
Table created.
SQL> create or replace type a_typ as object (id number, amount number)
/
Type created.
SQL> create or replace function get_id (pid number)
return a_typ
is
pragma autonomous_transaction;
l_a_typ a_typ := a_typ (null, null);
begin
update a
set amount = 800
where id = pid
returning id, amount into l_a_typ.id, l_a_typ.amount;
commit;
return l_a_typ;
end get_id;
/
Function created.
SQL> select id, d.a_typ.amount amount
from a, (select get_id (13) a_typ from dual) d
where id = d.a_typ.id
/
ID AMOUNT
---------- ----------
13 800
1 row selected.
Tags: Database
Similar Questions
-
Use of UNION in the Select statement
IM using Union of two select queries. This will return 2 rows with the same different sales with sales person. Is it possible to get in one line.
for example: name sales
Person1 100
Person2 50
Person1 50
But I need the output voltage
Sales of name
Person1 150
Person2 50
Is this possible?
Everything is possible in oracle if the requirement is clear
You can do this by using option group.
Select Name, sum (sales) as Sales from
Group by name; -
SUM function in the select statement
Hi I have a table like this
EmpNo, ndahrs, swipedate
101, 00:01, 01-apr-2013
101, 01:03, 02-apr-2013
102, 02:00, 01-apr-2013
102, 03:00, 03-apr-2013
104, 01:00, 04-apr-2013
104, 00:30, 03-apr-2013
now, I want to SELECT the EMPNO, NDAHRS, SWIPEDATE, SUM (NDAHRS) OF the TABLE;
How can I get what I've written request is to show message not a group by clause even when I added group that does notHello
Here's one way:
WITH got_tot_mins AS ( SELECT empno, nda_hrs, swipedate , SUM ( (60 * TO_NUMBER (SUBSTR (nda_hrs, 1, 2))) + TO_NUMBER (SUBSTR (nda_hrs, 4)) ) OVER ( PARTITION BY empno ) AS tot_mins FROM staffndamuster -- WHERE swipedate BETWEEN :p_startdate -- AND :p_enddate ) SELECT empno, nda_hrs, swipedate , TO_CHAR ( TRUNC ( tot_mins / 60)) || ':' || TO_CHAR ( MOD (tot_mins, 60) , 'FM00' ) AS tot_hrs FROM got_tot_mins ORDER BY empno, swipedate ;
You post p_startdate and p_enddate, so I can't test with them.
Published by: Frank Kulash on 10 April 2013 12:52
Just saw your desired results -
function() nested in the select statement
Oracle 11g. Try to nest a function within the select statement. Goal: get the average customer rating for each of the qualified user.
Select user_name, f_get_avg_cust_rating() "Notation.
of u, OTHER_TABLES o TAB_USER
where u.id = o.id and another condition matched;
How Oracle would deal with the sql. Is it
(1) first of all display records based on the where clause, then call the f_get_avg_cust_rating() for each of the selected records OR
(2) for the analysis in tables, the f_get_avg_cust_rating() will be called for each record met?
Thank you
Scott
Words of Frank, I created two functions with dbms_output and used a function in where clause in select another function. See the bottom of the unit tests. Everyone says, where first clause then select...
-Where function clause
SQL > CREATE or REPLACE FUNCTION ret_empno
2 RETURN NUMBER
3 AS
4 BEGIN
5 DBMS_OUTPUT. Put_line (' where Clause Function');
6 RETURN 7839;
7 END;
8.
The function is created.
-Select the function
SQL > CREATE or REPLACE FUNCTION ret_1
2 RETURN NUMBER
3 AS
4 BEGIN
5 DBMS_OUTPUT. Put_line ('Select function');
6 RETURN 1;
7 END;
8.
The function is created.
SQL > SELECT empno, ename,ret_1
2 FROM emp
3. WHERE empno = ret_empno;
EMPNO, ENAME RET_1
---------- ---------- ----------
7839 KING 1
Where the function Clause
Select the function
-
How to pass parameter in the function using the select statement?
Hello
I had a problem. I can't pass as parameter to the function by using the select statement. But it can pass as a parameter using the "code". How can I solve this problem?
For example,.
Select * from table (SplitFunction ('HS750020, HS750021')) < < < this work.
but
Select * from table (SplitFunction (select LOT_NO in the TRACER_SEARCH_SCHEDULE where JOB_ID = '36')) < < < do not work.
Thank you for trying to help him. Thank you.Select * from table (SplitFunction (select LOT_NO in the TRACER_SEARCH_SCHEDULE where JOB_ID = '36'))< do="" not="">
Try like this
select * from table(select splitfunction(lot_no) from tracer_search_schedule where job_id='36')
Just make sure that your subquery returns only 1 row.
-
Why I can't use the procedure in the select statement
Why I can't use the procedure in the select statementAnd you may not usa a SQL function if it has out parameters
SQL> create function myfun(p1 in out number) return number is 2 begin 3 p1:=1; 4 return 2; 5 end; 6 / SQL> select myfun(5) from dual; select myfun(5) from dual * ERRORE alla riga 1: ORA-06572: Function MYFUN has out arguments
In fact, the problem is that an output parameter is passed 'ref' and not 'val '...
Max
-
How to use the Type of Oracle Table values in the Select statement.
Hello
I get the initial set of values in the Table of Type Records of the Oracle and want to use the list of values in the Select statement.
For example, try something like the following:
TYPE t_record () IS RENDERING
ID TABLEA.ID%type,
NO TABLEA.NO%type
);
v_record t_record;
T_table TYPE IS the v_record TABLE % TYPE;
v_table t_table;
-Code to fill the values of v_table here.
SELECT ID, NO, COLLECT in BULK IN < some other table variabes here > FROM TABLEA
WHERE ID IN (i) v_table USER.USER;
I want to know how to use the Type of Oracle Table values in the Select statement.Something like this:
create or replace type t_record as object ( id number, no number ) / CREATE or replace type t_table AS TABLE OF t_record; / set serveroutput on declare v_table t_table := t_table(); v_t1 t_table := t_table(); begin v_table.extend(1); v_table(1).ID := 1; v_table(1).No := 10; v_table.extend(1); v_table(2).ID := 2; v_table(2).ID := 20; SELEC t_record (ID,NO) BULK COLLECT INTO v_t1 from TableA FROM TABLEA WHERE ID IN (select t.ID from table(v_Table) t); for i in 1..v_t1.count loop dbms_output.put_line(v_t1(i).ID); dbms_output.put_line(v_t1(i).No); end loop; end; /
No test!
P;
Published by: bluefrog on March 5, 2010 17:08
-
How to use the variable instead of the table name in the select statement of procedure
I have a procedure with a select statement with dynamic from clause. How can I use instead of the name of the table variable.
create or replace procedure scc_chemical_analysis
is
v_table_name varchar2 (100);
Start
declare
cursor c1 is select * from v_table_name; -This is the variable name. How can I use it in the select statement.
.
.
.
.
end;Hello
something like that.
1 declare 2 vSQLString VARCHAR2(32000); 3 pTableName varchar2(1000):='EMP'; 4 BEGIN 5 vSQLSTRING := 6 'SELECT ''x'' FROM ' || DBMS_ASSERT.sql_object_name(pTableName) || ' WHERE rownum<=1 '; 7 EXECUTE IMMEDIATE vSQLSTRING; 8* END; SQL> / PL/SQL procedure successfully completed.
-
using variable in the select statement (php)
I have trouble using a variable in a select statement.
The following query to manually (using a static date) works very well:
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > ' 2006-06-01' ';"
However, if I use the following:
$dateVar = date('Y-m-d');
@mysql_select_db ($database_mw, $mw);
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > $dateVar";
All records are returned, which means that the $dateVar variable is not recognized. I compared the values of $dateVar <? PHP echo $datetime;? > against the real value of my date field <? PHP echo $row_Recordset1 ["classDate"];? > in my results to the table and it seems that the values are indeed accurate regarding a date 2006-06-04 for example.
My date field 'classDate' is of type 'date' in the mysql database. I use Dreamweaver MX 2004 with Mac OSX 10.3.9.
I'm sure it's just a syntax problem, at least, I hope it is.
Any help is greatly appreciated.
It worked:
$query_Recordset1 = "SELECT * from academyClasses WHERE the classDate > = CURRENT_DATE;
Thank you
MW -
Return multiple values from a function in a SELECT statement
I hope I've provided enough information here. If not, let me know what I'm missing.
I create a view that will combine the information from several tables. Most are pretty simple, but there are a couple of columns in the view that I need to get by running a function within a package. Even if this is quite simple (I have a function named action_date in a package called rp, for example, that I can use to return the date that I need through SOME rp.action_date (sequence_number).
Here is the question: I really need to return several bits of information of the same record (not only action_date, but also action_office, action_value, etc.)-a join of the tables will work not here, as I will explain below. I can, of course, perform a function separate for each statement, but this is obviously inefficient. Within the select statement of the view, however, I don't know how each of the values that I need to get back.
For example, right now, I have:
Table 1:
sequence_number NUMBER (10),
name varchar (30),
...
Table2:
Table1_seq NUMBER (10),
action_seq NUMBER (10),
action_date DATE,
action_office VARCHAR (3),
action_value VARCHAR (60),
...
I can't just simply join Table1 and Table2 because I have to perform processing in order to determine the rows returned matching, I really need to select. If the package opens a cursor and treats each line until it finds the one I need.
The following works but is ineffective since all calls to the package returns the columns of the same record. I don't know how to put all the values that I need in the SELECT statement.
CREATE VIEW all_this_stuff AS
SELECT sequence_number, name,
RP.action_date (sequence_number) action_date,
RP.action_office (sequence_number) action_office,
RP.action_value (sequence_number) action_value
FROM table1
Is there a way to return multiple values in my SELECT statement or I'm going about this all wrong?
Any suggestions?
Thank you very much!Hello
What you want is a Query of Top - N , what you can do using the ROW_NUMBER analytic function in a subquery, like this:
WITH got_rnum AS ( SELECT action_seq, action_dt, action_office, action_type, action_value , ROW_NUMBER () OVER ( ORDER BY action_date , action_seq , action_serial ) AS rnum FROM table2 WHERE action_code = 'AB' AND action_office LIKE 'E' -- Is this right? ) SELECT action_seq, action_dt, action_office, action_type, action_value FROM got_rnum WHERE rnum = 1 ;
As written, this returns a single line (at most).
I suspect you'll actually get a rank for each group , where a group is defined by a value in a table in which you join.
In this case, add a PARTITION BY clause to the ROW_NUMBER function.
If post you a small example of data (CREATE TABLE and INSERT statements), I could show you exactly how.
As I don't have your tables, I'll show you the use of the tables in the scott schema.
This is a view containing data in the scott.dept table and also to scott.emp, but only for the highest employee in each Department (in other words, the employee whose oldest hire date). If there be a tie for the first hire date, while the candidate with the lowest empno is selected.CREATE OR REPLACE VIEW senior_emp AS WITH got_rnum AS ( SELECT d.deptno , d.dname , e.empno , e.ename , e.hiredate , ROW_NUMBER () OVER ( PARTITION BY d.deptno ORDER BY e.hiredate , e.empno ) AS rnum FROM scott.dept d JOIN scott.emp e ON d.deptno = e.deptno ) SELECT deptno , dname , empno , ename , hiredate FROM got_rnum WHERE rnum = 1 ; SELECT * FROM senior_emp ;
Output:
. DEPTNO DNAME EMPNO ENAME HIREDATE ---------- -------------- ---------- ---------- --------- 10 ACCOUNTING 7782 CLARK 09-JUN-81 20 RESEARCH 7369 SMITH 17-DEC-80 30 SALES 7499 ALLEN 20-FEB-81
Moreover, one of the conditions to the query you posted has been
action_office LIKE 'E'
which equals
action_office = 'E'
(AS is always equivalent to = if the string that follows AS does not contain the winning cards.)
Did you mean say that or did you mean something like this:action_office LIKE 'E%'
Instead?
-
Can we call a procedure in the select statement?
Can we call a procedure in the select statement?Hello
Raghu_appsdba wrote:
Can we call a procedure in the select statement?# You can call functions, but not procedures.
If the procedure does not change the State of the database (for example, it isn't updated all tables), then you can wrap it in a function, or re - write function.
Here is an example of wrapping.
CREATE OR REPLACE FUNCTION fun_x (in_txt IN VARCHAR2) RETURN VARCHAR2 IS BEGIN proc_y (in_txt); RETURN in_txt END fun_x;
-
uniol all in the select statement
Hi I have a situation where I have to return the value based on the if statement, is it possible to put union all in the select statement
My use case is
IN the column State
If column01 > 0 to condtion 't'
IF column02 > 0 that the condition of 'Y '.
Condition IF COLUMN03 > 0 THAN 'w '.
If COLUMN04 > 0 that the condition of "Z".
Select 't'
TableName
where column01 > 0
Union of all the
Select "y".
TableName
where column02 > 0
Union of all the
Select 'W '.
TABLENAME
WHERE COLUMN03 > 0
I have what it takes to show value as it
EmpName, condition, empno
Jerry, T, 0158754585
TOM, Y, 0054789568
Am in oracle database 11 g 2
Yes you can put all THE UNION here
Select empname, 't' State, empno from tablename where column01 > 0
Union of all the
Select empname, 't' State, empno from tablename where column02 > 0
Union of all the
Select empname, condition of "W", empno FROM tablename WHERE COLUMN03 > 0
Union of all the
Select empname, condition of "Z", empno FROM tablename WHERE COLUMN04 > 0
and if you are looking for how to remove duplicates because your line might have satisfied all the conditions and my happen 4 times in your result set, you just need to give them an order and filter it.
For example, you want this
If column01 > 0 to condtion 't'
ELSE IF column02 > 0 that the condition of 'Y '.
Another condition of THAN > 0 IF COLUMN03 'w '.
otherwise if COLUMN04 > 0 that the condition of "Z".
Select empname, condition, empno, in
(
Select a.*, row_number() over (partition by empno arrested by myorder) rn
Of
(
Select empname, 't' State, empno, 1 myorder tablename where column01 > 0
Union of all the
Select empname, 't' State, empno, 2 myorder tablename where column02 > 0
Union of all the
Select empname, condition of "W", empno 3 myorder FROM tablename WHERE COLUMN03 > 0
Union of all the
Select empname, empno, condition of 'Z', 4 myorder FROM tablename WHERE COLUMN04 > 0
) a
) where rn = 1
-
mixture of column type in the select statement
Oracle 11g r2.
tab_test (name varchar2, blob... photo)
(1) select general use *.
Stored procedure: open ref_cursor to some * of table_test...
Coast of java: call.registerOutParameter (1, OracleTypes.CURSOR); Call.Execute ();
It works very well.
(2) enter the name of the column in the select such as
Stored procedure: open ref_cursor to select name, photo table_test...
Coast of java: call.registerOutParameter (1, OracleTypes.CURSOR); Call.Execute ();
ERROR: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent data types: expected - got BLOB
My question, if I specify the column names in the select statement, including the varchar2 and blob, how can I registerOutParameter (1, OracleTypes.CURSOR);?
Thank you
Scott
PS The number of returned columns, in real case schenario, is a little, so I prefer to specify column names.
Wrong forum. This forum is for questions of sql and pl/sql.
Enter the question ANSWER and the repost in the JDBC forum
When repost you provide the necessary information to help you:
1. information on the full version for Oracle DB
2 version of the JDK
3. the name and version of the JDBC jar file
4. real code that demonstrates the problem
You should also review the info re REF CURSOR in the JDBC Developer's Guide
http://docs.Oracle.com/CD/E16655_01/Java.121/e17657/oraint.htm#i1058743
-
Pivot will not get result in the select statement
This is the version of oracle 11g R2 on windows details 7 OS
CREATE TABLE OTBOOKINGDETAILS
(EMPNO VARCHAR2 (10 BYTE),
DATE OF OTBOOKEDDATE,
NUMBER (5.0) DAY OF THE WEEK.
DAYCEILINGHRS VARCHAR2 (10 BYTE));
Here is the data
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 MARCH 2013', 1,' 09:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 APRIL 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 MAY 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 JUNE 2013', 4' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 JULY 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 AUGUST 2013', 6,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 SEPTEMBER 2013', 7,' 00:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 MARCH 2013', 1,' 09:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 APRIL 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 MAY 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 JUNE 2013', 4,' 02:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 JULY 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 AUGUST 2013', 6,' 04:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (102 ', 3 SEPTEMBER 2013', 7' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 OCTOBER 2013', 1,' 00:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 NOVEMBER 2013', 2' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 3 DECEMBER 2013', 3,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 13 MARCH 2013', 4,' 04:45 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 14 MARCH 2013', 5,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 15 MARCH 2013', 6,' 01:00 ');
INSERT INTO OTBOOKINGDETAILS VALUES (101 ', 16 MARCH 2013', 7' 01:00 ');
Here, I want to Dayceiling hours below the output using pivot
EMPNO OTBOOKEDDATE DAY1_HRS DAY2_HRS DAY3_HRS DAY4_HRS DAY5_HRS DAY6_HRS DAY7_HRS
101 03/03/2013 09:00 01:00 01:00 01:00 01:00 01:00 00:00
102 03/03/2013 09:00 01:00 01:00 02:00 01:00 04:00 01:00
101 03/10/2013 00:00 01:00 01:00 04:45 01:00 01:00 01:00ChakravarthyDBA wrote:
I need to include the pivot query in the select statementCan' are you use PIVOT here since you do not know from the start what values OTBOOKEDDATE. So use:
select empno, otbookeddate - weekday + 1 otbookeddate, max( case weekday when 1 then dayceilinghrs end ) day1_hrs, max( case weekday when 2 then dayceilinghrs end ) day2_hrs, max( case weekday when 3 then dayceilinghrs end ) day3_hrs, max( case weekday when 4 then dayceilinghrs end ) day4_hrs, max( case weekday when 5 then dayceilinghrs end ) day5_hrs, max( case weekday when 6 then dayceilinghrs end ) day6_hrs, max( case weekday when 7 then dayceilinghrs end ) day7_hrs from otbookingdetails group by empno, otbookeddate - weekday + 1 order by otbookeddate, empno / EMPNO OTBOOKEDDA DAY1_HRS DAY2_HRS DAY3_HRS DAY4_HRS DAY5_HRS DAY6_HRS DAY7_HRS ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 101 03-03-2013 09:00 01:00 01:00 01:00 01:00 01:00 00:00 102 03-03-2013 09:00 01:00 01:00 02:00 01:00 04:00 01:00 101 10-03-2013 00:00 01:00 01:00 04:45 01:00 01:00 01:00 SQL>
SY.
-
validation for each 1,000 records to be inserted in the select statement
Hi, I have the following INSERTION in the SELECT statement.
The SELECT statement (who joined) data fo about 6 crores. I need to insert this data into another table.
Please suggest me the best way to do it.
I use the INSERT in the SELECT statement, but I want to use the statement commit for each 1,000 records.
How can I do this...
Thank youinsert into emp_dept_master select e.ename ,d.dname ,e.empno ,e.empno ,e.sal from emp e , dept d where e.deptno = d.deptno ------ how to use commit for every 1000 records .
Method 4 is the best
You could combine method 3 and 1 (without dynamic SQL)
Method 2 is the less optimal solution from the list.
Maybe you are looking for
-
Satellite M50-A-118 - cannot scroll using the touchpad
Hi people! I have a Satellite M50-A-118 and I can't scroll with my touchpad.How can I solve this problem?
-
How do I uninstall NessPluss that appears not desired as homepage
The website http://web.nessplus.com/en/ appears as my home page, even though I repeatedly put it is http://www.msu.edu with the TOOLS option.It is an accidental download of a program that was supposed to convert file formats. It does not appear in my
-
I have a Yahoo and now I have a Google mail account. On the PC, I did all my Yahoo mail go to my Google account. Now on the Meadow when I get email on my Yahoo account, everything is duplicated. They appear in the folder of Yahoo and Google. I de
-
Autostart is running, I'm doing it wrong?
Hi guys, I just had a W450 for 4 or 5 days and I'm still getting used to it. I noticed one thing though is that I lose the beginnings of my short. I run with my dog, and so I usually have to stop environ.5 miles for him do his thing. At that time, I
-
What version of Windows 10 will I receive if I have Windows Retail Pro 8.1?
Hi everyone, can someone help me with this issue? So basically, I have version 8.1 of Windows Pro retail (which allows me to build another computer and use the same license key, so that only have the Windows Installation and was bought in a store rat