Using Procedure in SQL statement
Dear Sir,
As you know, I can use any function in SQL statement. For example:
SELECT systimestamp,
Function_Name(variable1, variable2,...)
FROM anytable;
So the previous function could only retrieve one value -as functions concepts-. Anyhow, Can I, in someway, use Procedure that take multiple in parameters and return multiple out parameters in SQL statement.
Thank you in advance.
Sir,
I got a way in order to use the benefit of procedure in function. It's trough your idea in using TYPE OBJECT as the following:
===================================================================
create or replace type Missed_Txn_type AS OBJECT
(Txn_Timestamp_obj timestamp,
Txn_Type_Obj Number(12));
===================================================================
Then I created function and used this type as returned value:
FUNCTION Get_Shift_Missed_Txn_Obj(F_Date_In Date,
F_Time_In Timestamp,
F_Employee_Id number)
RETURN Missed_Txn_type;
The issue is: I want send the variables of the function through SELECT statement which they come from another table like:
SELECT
EMP.ID,
sd.date_value,
shf.Time_In,
T.OBJ.Txn_Timestamp_obj,
T.OBJ.Txn_Type_Obj
FROM
EMPLOYEE EMP,
Stored_Date SD,
Shifts shf,
(select Get_Shift_Missed_Txn_Obj(sd.date_value,
shf.time_in,
EMP.Id) OBJ from dual) T
WHERE
[where clause]
But the previous statement returned an error shows that it couldn't determine the (EMP.ID, shf.time_in, sd.date_value...)...
And the same if I use it in the select list!
So sir, there is any way in order to solve this issue?
Thank you in advance.
Similar Messages
-
CALL PROCEDURE IN SQL STATEMENT
Why we cant call a procedure inside SQL statement?
Hitesh Nirkhey wrote:
Hi Karthick_Arp
as you said
The procedure that is used inside a function cannot contain DDL or DML statements or COMMIT/ROLLBACK.
Said that it does not make much sence to use a procedure within a function.IT make sense if we DECLARE FUNCTION AS PRAGMA AUTONOMUS_TRANSACTION
then we can execute DDL or DML statements or COMMIT/ROLLBACK in that function.
Regards
HiteshBut why would you do that? -
ALDSP 3.0 -- schema owner for stored procedure or SQL Statement
Using ALDSP, I have a need to create a physical service based on a stored procedure or a SQL statement. I am wondering what will happen when I move to another deployment environment where the schema owner changes. In our QA and Prod environments, we have a different schema owner for all tables in the application (the DBAs believe this prevents unwanted updates to a prod environment). DSP elegantly supports this for normal table- and view-based physical services by mapping schemas through the DSP console after deployment. Will I get the same type of mapping capability for stored procedures and SQL statements? I noticed that I can add a SQL-based function to a physical service...is there a way to pass in the physical table name from that data service to the procedure or SQL statement?
Thanks,
JeffSchema name substitution should work for stored procedures just like it does for tables. If it doesn't - report a bug.
You don't get any help for sql-statement based data services - dsp doesn't parse the sql provided. One thing you could do is use the default schema (following the user of your connection pool), and not specify the schema in your sql-statement. -
JDBC - Pl/SQL-Procedure or SQL-Statement???
Hi,
we have got the following problem:
Our program is entirely written in Java. It has to communicate with an ORACLE 8i database (Version 8.1.7). The big question is whether to call pl/sql-procedures which include the sql-statement (select/ insert/update) or to write the sql-statements in the Java-code directly.
We messure the time in our test-database. The procedure took twice the time of the direct statement, althoug the sql-statement had to be parsed each time.
What is the better way: pl/sql-procedure or sql-statements? On which conditions depend the choose?
(For your information: our statements aren't difficult. In our testdatabase aren't much datasources.)
Here is a snippet of our code:
1. PL/SQL-Prozedure:
CallableStatement cs = conn.prepareCall("begin dbrb_test_pkg.test(?,?,?); end;");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.registerOutParameter(2, OracleTypes.CURSOR);
cs.registerOutParameter(3, OracleTypes.CURSOR);
cs.execute();
ResultSet rs1 = ((OracleCallableStatement)cs).getCursor(1);
ResultSet rs2 = ((OracleCallableStatement)cs).getCursor(2);
ResultSet rs3 = ((OracleCallableStatement)cs).getCursor(3);
2. SQL-Statements directly in Java-Code:
String statement1b = "SELECT bva_id, bva_datva FROM bva";
ResultSet rs1b = stmt.executeQuery(statement1b);
String statement2b = "SELECT brb_id, brb_datein FROM brb";
ResultSet rs2b = stmt.executeQuery(statement2b);
String statement3b = "SELECT bper_id, bper_nz1 FROM bper";
ResultSet rs3b = stmt.executeQuery(statement3b);
Thanks a lot.
Claudia and Nicolethis is a case-to-case basis.
it's ok to use the Statement/PreparedStatement if you're constructing your SQL or DML (insert/update/delete) statements. like when you're WHERE condition is dynamically created. this can be done also in PL/SQL (CallabeStatement) but passing of data is very tedious for you.
the PL/SQL is much better to use if all your SQL or DML statements are fixed. besides, this is easier to maintain. when you need to change a statement, you don't need to check the statement in SQL prompt then make the necessary changes in your java codes, then compile the class. when you're using PLSQL, you just need to change and recompile the PL you made. then presto! it's done. as long as you don't change the parameters the PL receives and sends, it should perfectly work with your java code. =) -
Hello All,
I am getting below error can you please help me
Error:-
The transaction associated with the current connection has completed but has not been disposed. The transaction must be disposed before the connection can be used to execute SQL statements.Perhaps this thread will help you out
http://stackoverflow.com/questions/11453066/error-the-transaction-associated-with-the-current-connection-has-completed-but
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Why I Can't use procedure in select statement
Why I Can't use procedure in select statement
We can use function in select statement but we couldn't use procedure with one out parameters in select statement... You can use Function because they are designed for this but procedure are not. Functions can return value (without OUT parameter) which can be used in SELECT whereas procedures do not have such concept. As you can see in the above post you can not call even functions also if it has any out parameter.
I have just trying to use procedure in select statement ..for this I require technical answer..The technical answer is because conceptually procedure is for doing set of operation, performning DMLs on the tables , whereas functions are for processing and producing a single result. Functions are basically for not using INSERT/UPDATE/DELETE in it. That is the reason they are allowed to be used in SELECT because conceptually they are not supposed to do any data changes.
Regards,
Avinash -
Function or pseudo-column 'DECODE' may be used inside a SQL statement only
Hi everyone!
I got the error in the subject concernig the follow piece of sql code
+ v_str_sql_body :=
v_str_sql_body || ' and cod_entita ='
|| decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
execute immediate v_str_sql_body; +
I can't understand what's the problem with it.
Can anyone help me to find the error?
Thank you in advance!
Samuel
Edited by: 996508 on 27-mar-2013 5.29Hi, Samuel,
Welcome to the forum!
996508 wrote:
Hi everyone!
I got the error in the subject concernig the follow piece of sql code
+ v_str_sql_body :=
v_str_sql_body || ' and cod_entita ='
|| decode(cur.cod_entita_a,str_all,cur_ent.cod_entita,cur.cod_entita_a);
execute immediate v_str_sql_body; +
I can't understand what's the problem with it.Sorry, I can't understand what you're trying to do.
Whenever you have a problem, post a complete script that people can run to re-create the problem and test their ideas. Include your complete PL/SQL code (if the problem involves PL/SQL) including code to call your procedure, a little sample data (CREATE TABLE and INSERT statements) for any tables involved, and the results you want the given sample data.
Always say which version of Oracle you have (e.g., 11.2.0.3.0).
See the forum FAQ {message:id=9360003}
Can anyone help me to find the error?As the error message says, you can only use DECODE in a SQL statement. That is:
v_str_body := v_str_body
|| ' and cod_entita = '
|| DECODE (...);is always an error, no matter what ... stands for.
Perhaps you meant to have the DECODE inside the string literal.
If not, then (depending on your version) you can use CASE instead of DECODE:
v_str_body := v_str_body
|| ' and cod_entita = '
|| CASE cur.cod_entita_a
WHEN str_all THEN cur_ent.cod_entita
ELSE cur.cod_entita_a
END;Anything that DECODE can do, CASE can do, too.
DECODE is sometimes a little simpler than CASE to use, but only a little. It is never faster than CASE.
CASE is sometimes much, much simpler to use than DECODE, and sometimes much faster, too.
I won't say never use DECODE; but I will say rarely use DECODE. Use CASE instead, unless you have a specific reason why DECODE is better in that particular situation.
In PL/SQL, you can also use IF ... THEN ... ELSE in places where you might want to use DECODE or CASE.
For example:
v_str_body := v_str_body
|| ' and cod_entita = ';
IF cur.cod_entita_a = str_all
THEN
v_str_body := v_str_body
|| cur_ent.cod_entita;
ELSE
v_str_body := v_str_body
|| cur.cod_entita_a;
END IF;Edited by: Frank Kulash on Mar 27, 2013 5:59 PM
Added IF ... THEN ... ELSE example. -
PL SQL using variable in SQL statement
I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dual
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1
select to_char(val) from dual
Thanks in advance to all who answerHi,
840386 wrote:
I am trying to execute several sql statements, that have the same format but different values. Does this
mean I need a bind variable?No, you don't need a bind variable, though bind variables may be more efficient than using PL/SQL variables. I don't see where you're trying to use any varibables at all in your examples. Is it in place of the literals, such as 2 or 'YYYY_MM_DD'? You can use either bind varibales or PL/SQL variables in those places.
ie
select TO_CHAR ( (SYSDATE - 2), 'YYYY_MM_DD') from dual
select TO_CHAR ( (SYSDATE - 4), 'YYYY_MM_DD') from dual
select to_char(add_months(sysdate,-2*1) from dualIn PL/SQL, when you have a SELECT statement, you must specify what you want to do with the results. For example, a SELECT ... INTO statement:
SELECT AVG (sal)
INTO avg_salary
FROM scott.emp;There's usually no point in SELECTing from dual in PL/SQL. It's not an error, but it's simpler just to use an assignment statement.
When I try to put the values into a varaiable (date, varchar2 or number) I am getting a conversion
error.Post a complete procedure or anonymous block, including variable declarations, that shows exactly what you're trying to do.
>
Can somebody show me an example of how to do something like this? Or at least point me to the correct
part of the documentation that provides and example. Pardon my syntax as I know it is incorrect
val :=add_months(sysdate,-2*1Assuming val is a DATE, that's basically correct. You have unbalanced parentheses (there's a '(', but no matching ')' ), and you need a semicolon (';') at the end of the statement. Perhaps ');' just got cut off when you were posting this.
select to_char(val) from dualAgain, SELECTing from dual is unnecessary, but if you had some way to catch the returned value, that would work.
Usually, the reason why you need to call TO_CHAR is that you want a value in a particular format, which is specified in the 2nd argument to TO_CHAR. Calling TO_CHAR with only one argument is a possible mistake, but not something that would raise an error.
Here's an example that works:
SET SERVEROUTPUT ON
DECLARE
d DATE;
v VARCHAR2 (30);
BEGIN
d := ADD_MONTHS (SYSDATE, -2); -- -2 = (-2*1)
v := TO_CHAR (d, 'DD-Mon-YYYY');
dbms_output.put_line (v || ' = v');
END;
/Output (when run on March 13, 2011):
13-Jan-2011 = v -
Using procedure in SELECT statement
I have a select statement that currently uses 4 functions to receive necessary values. All the functions are recursive and returns values from the same row.
What I would like to do is replace these for function calls with 1 procedure. Does anybody know if it possible to use a procedure in this way inside a select statement?
If so, do you have the syntax for doing this?
E.g
SELECT
Mdbrd_Pkg.calculate_fixed_charge_fn(in_rc_id, ap.CONFIGSET_ID) AS FIXED_CHARGE,
Mdbrd_Pkg.calculate_charge_rate_fn(in_rc_id, ap.CONFIGSET_ID) AS CHARGE_RATE,
Mdbrd_Pkg.tax_liable_fn(in_rc_id, ap.CONFIGSET_ID) AS TAX_LIABLE,
Mdbrd_Pkg.charge_unit_fn( in_rc_id, ap.CONFIGSET_ID) AS CHARGEUNIT_ID
FROM .....This cannot be done. The part of the function used in the SELECT statement is the return value: procedures don't have return values (that's what makes tham procedures and not functions).
Obviously I don't know what your code does, but you should consider putting them into a single function that returns a TYPE with four attributes and then using the TABLE() function to cast them into something you could reference in the FROM clause of a correlated sub-query. Sounds a bit messy though.
Do these functions actually select data? Where does the recursion fit in?
Cheers, APC -
Hello,
I would like to use integer array( say..int count[] = new int[3];) on my sql statement to retrieve some values. Is there a way to use int array variable in sql statement?. If yes, how should I approach doing that?.
Thank You in Advance.
Regards,
Pinal.I'm going to be honest, I'm not so sure there is such a thing in standard SQL syntax so it would depend upon the database you were using as to whether this option was available as part of the SQL language.
My suggestion would be to cheat, naughty I know:String arrayString = "";
for (int i = 0; i < arrayInt.size(); i++) {
arrayString += "**" + arrayInt;
arrayString = arrayString.substring(2, arrayString.length());Then just parse arrayString in to an SQL VARCHAR or TEXT field.
Obviously when you return it just use StringTokenizer to split the string up using your deliminator (in this case **) and then convert the values back into an int array.
I'm not sure how efficient that code would be but it should do the job. -
HI guys:
I need code three SQL statements.the returned field and selected table are all same,there is only a difference in their "where" statement.
Sample code:
select marcmatnr marcwerks
into table it_data
from MARC inner join MBEW on marcmatnr = mbewmatnr
where marcmatnr like 'A%' and mbewzplp1 = '001'.
second one........................ mbew~zplp2 = '001'
third one......................... mbew~zplp3 = '001'
Could I write a FORM gather them with transporting a parameter ZPLPX to determine which condiniton will be execute?
thank you very much.Hi tianli,
1. source text
This concept of dynamic where
condition is called source text.
2. use like this.
This is important in the code ---> WHERE (mywhere).
REPORT abc LINE-SIZE 80.
DATA : it_data LIKE TABLE OF mara WITH HEADER LINE.
QUOTES ARE IMPORTANT
PERFORM mysql USING 'mbew~zplp2 = ''001'''.
FORM mysql USING mywhere.
SELECT marcmatnr marcwerks
INTO TABLE it_data
FROM marc INNER JOIN mbew ON marcmatnr = mbewmatnr
WHERE (mywhere).
ENDFORM. "mysql
regards,
amit m. -
Same WITH used in many SQL statements
Hi,
I have a need to use the same WITH clause in many places of my code. Is it ever possible to code without writing the same code in every SQL which need my WITH?
Thanks in advance.As Tubs mentioned, views.
There only 2 basic methods of modularisation of SQL source code. Using the WITH clause (modularises in the same statement), or VIEWS (modularises across SQL statements).
Using PL/SQL functions or pipelined tables are poor choices for SQL code modularisation. And should not be considered. These tools address a different set of requirements. -
Using store procedure within sql statement?
I have the following sample tables:
project id project_name
====== ===============
1 project one
2 project two
3 project three
employee_id fname lname
=========== ============= =====
100 amy A
200 better B
300 carrie C
project_id employee_id
=========== ===========
1 200
2 300
3 100
there is a procedure getFullname(empployee_id) in place that combines the fname with lname to full name
what i need to accomplish is the following.
project is created by employee
==============================
project one is created by better b
project two is created by carrie C
project three is created by amy A
but the rule is: you have to call the procedure getFullName(employee_id) to accomplish the task above, and don't use the join tables to get the full employee nameCREATE OR REPLACE FUNCTION YOUR_FUNCTION(i_nEmpId IN NUMBER)
RETURN VARCHAR2 IS
BEGIN
RETURN 'Lastname ' || TO_CHAR(i_nEmpId) || ' Firstname';
END;
WITH PROJECTS AS (SELECT 1 PROJECT_id, 'project one' PROJECT_NAME FROM DUAL
UNION ALL
SELECT 2 PROJECT_id, 'project two' PROJECT_NAME FROM DUAL
UNION ALL
SELECT 3 PROJECT_id, 'project three' PROJECT_NAME FROM DUAL
PROJ_TO_EMP AS (SELECT 1 PROJ_ID, 100 EMP_ID FROM DUAL
UNION ALL
SELECT 2 PROJ_ID, 200 EMP_ID FROM DUAL
UNION ALL
SELECT 3 PROJ_ID, 300 EMP_ID FROM DUAL
SELECT PROJECT_ID,
PROJECT_NAME,
EMP_ID,
YOUR_FUNCTION(EMP_ID),
PROJECT_NAME || ' is created by ' || YOUR_FUNCTION(EMP_ID)
FROM PROJECTS,
PROJ_TO_EMP
WHERE PROJECT_ID=PROJ_ID; -
Help needed in SQL performance - Using CASE in SQL statement versus 2 query
Hi,
I have a requirement to find count from a bunch of tables.
The SQL I have gives the count of all members.
I have created 2 queries to find count of active and inactive members.
The key difference is only the active dates.
Each query takes 20 seconds to execute.
I modified the SQL to use CASE statement in the SELECT.
So after the data is fetched the CASE statement will evaluate the active date and gives 2 counts (active and inactive)
Is it advisable to use this approach. Will CASE improve SQL performance ? I have to justify this.
Please let me know your thoughts.
Thanks,
JHi,
If it can be done in single SQL do it in single SQL.
You said:
Will CASE improve SQL performance There can be both cases to prove if the performance is better or worse.
In your case you should tell us how it is.
Regards,
Bhushan -
DropDownList using result in sql statement
hi
i made a dropdownlist in which i get the data from a sql table.
when i select a date i wanna press a button and this button starts a procedure which is creating a view on a table in which the where statement is the result of the selected item in the drop down box
how do i get the selected item in my procedure?
thanks a lot
Falkok my problem is how do i get the selected item in this :P1_DATE?
do i have to declare it anywhere? where do i have to write the var into?
i am a completely beginner so plz give me a simple how to
ok i think ur :P1_DATE is the name of my dropdown menu
so if i try this method i get an error "Bindevariables are not allowed for data definition operations
i have to creat a view of the table because the table contains more than 100.000 entries and if i want to search s.th. i takes about 4 sec
i want to do a pre selection with the view so people can select a date in the dop down menu and then the createtd view will contain about 400 entries and if u search in the new view it takes only 1 sec
i also want to do this because of performing the database
sorry for my english, hope u will understand all
Edited by: user10247044 on 19.09.2008 07:11
Maybe you are looking for
-
How to set background color in a docking container?
Hi guys! Is it possible to set a background color for a docking container? I don't find any appropriate method to do that. Is there actually a way?
-
Fixed Deposit Report with TDS Details
Hello All, Is there any report available to show Fixed Deposit Report with TDS Details ?
-
Custom Process Task Adapter for AD OIM 11gR2
Hi, Has anyone written a Custom Process Task Adpater for AD? We want to update bunch of attributes from a single task adapter and wondering how we can extend the OOTB ones? - Kaushik
-
Production order component backflush
Hi all I have the backflush indicator for the components in my production order. Also the " Goods Movement all components" tick in the Production Order Confirmation Parameters. But when i am confirming the production order (Final Confirmation), the s
-
Documents related to SUP complete flow(Device to Server and Vice versa).
Hi, Iam using SUP 2.1 ESD#1 licensed version, Please send some documents related to SUP complete flow(Device to Server and Vice versa. Thanks, B.Ushasri