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.29
Hi, 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.
Similar Messages
-
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 -
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. -
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 -
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. -
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. -
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 -
Help Using Count and SQL statements
Hello
This problem has been bugging me incesantly and I would be reaaaaaaally grateful if someone could help here
I want to create query that woud count the number of times a certain unedited word would be matched against different edited words
This query works well in MS ACCess but not in java
query =" SELECT Count(*) AS WordCount, Segmentations.EditedWord FROM [select Segmentations.EditedWord FROM Segmentations where Segmentations.UneditedWord = '"+Word+"']. AS c GROUP BY Segmentations.EditedWord";
I've tried removing the Segmentations. and changing it to
query =" SELECT Count(*) AS WordCount, EditedWord FROM [select EditedWord FROM Segmentations where UneditedWord = '"+Word+"']. AS c GROUP BY EditedWord";
But still nothing
I think that is due to the fact that you can't call Count(*) and another field at the same time
pleas eif anyone knows a way around this send it ASAP
thanxWhy not use,
query = "SELECT Count(*) AS WordCount, EditedWord FROM Segmentations WHERE UneditedWord = ? GROUP BY EditedWord"and use PreparedStatement to optimize.
HTH -
Hi,
I have a big sql file which contains a lot of insert statements and one of the columns which contains customer details the value can be 'Mr & Mrs xxx'. when i am running this from SQl*PLUS whenever the insert statement with a value like this comes up i am being prompted to enter a value for Mrs ... Can i set any parameter in Sql*plus so that sql*plus ignores the & and inserts the record.
Thanks in advanceSince scan seems obsolete in 10gR2, work with :<br>
set define off<br>
<br>
Nicolas.<br>
<br>
James has already answered, sorry.<br>
Message was edited by: <br>
N. Gasparotto -
Hi,
My question is how I can insert exeactly the value '&Job' to my table without being prompted by SQL*Plus for entering a value for Job?
Thanks
nullHere it is... set ESC and set DEF:
From SQL*Plus User's Guide and Reference
Release 8.1.6 A75664-01
SET ESC[APE] {\|c|ON|OFF}
Defines the character you enter as the escape character. OFF undefines the escape character. ON enables the escape character. ON changes the value of c back to the default "\".
You can use the escape character before the substitution character (set through SET DEFINE) to indicate that SQL*Plus should treat the substitution character as an ordinary character rather than as a request for variable substitution.
SET DEF[INE] {&|c|ON|OFF}
Sets the character used to prefix substitution variables to c. ON or OFF controls whether SQL*Plus will scan commands for substitution variables and replace them with their values. ON changes the value of c back to the default '&', not the most recently used character. The setting of DEFINE to OFF overrides the setting of the SCAN variable.
null -
Service that uses several insert sql statements
Hello! Is it possible to use one service, which updates two DB tables? Also, one field in these two tables is the same, like the ProcessID? For example, here is the service:
<tr>
<td>EUM_WORKFLOW_UPDATE_ACTIONS</td>
<td>Service
3
null
null
null<br>
null</td>
<td>2:IEumWorkflowActions:::null
2:IEumWorkflowDocumentProcess:::null</td>-->
</tr>
Here are two separate SQL queries called from the above service:
<tr>
<td>IEumWorkflowActions</td>
<td>INSERT INTO EUM_WORKFLOW_ACTIONS(WORKFLOWACTION, WKFLACTIONID) values (?, ?)</td>
<td>WORKFLOWACTION varchar
WKFLACTIONID int</td>
</tr>
<tr>
<td>IEumWorkflowDocumentProcess</td>
<td>INSERT INTO EUM_WORKFLOW_ACTIONS(WKFLACTIONID, DocumentProcessID, dDocName, dRevLabel) values (?, ?, ?, ?)</td>
<td>WKFLACTIONIDint
DocumentProcessID int
dDocName varchar
dRevLabel varchar</td>
</tr>
And here is how I call this service via GenericSoapPort call:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body xmlns:ns1="http://www.oracle.com/UCM">
<ns1:GenericRequest webKey="cs">
<ns1:Service IdcService="EUM_WORKFLOW_UPDATE_ACTIONS">
<ns1:User></ns1:User>
<ns1:Document>
<ns1:Field name="WORKFLOWACTION">Reject</ns1:Field>
<ns1:Field name="WKFLACTIONID">123</ns1:Field>
<ns1:Field name="WKFLACTIONID">123</ns1:Field>
<ns1:Field name="DocumentProcessID">45234</ns1:Field>
<ns1:Field name="dDocName">1554322</ns1:Field>
<ns1:Field name="dRevLabel">1A</ns1:Field>
</ns1:Document>
</ns1:Service>
</ns1:GenericRequest>
</soap:Body>
</soap:Envelope>
Would it be possible NOT to use the WKFLACTIONID twice in the service call somehow?
Thank you for your help!Thank you for the confirmation that this would work. I've also tried the Insert query like this:
insert all
into EUM_WORKFLOW_ACTIONS (WORKFLOWACTION, WKFLACTIONID) values (?, ?)
into EUM_WORKFLOW_DOCUMENT_PROCESS (WKFLACTIONID, DOCUMENTPROCESSID) values (?, ?)
select * from dual
Would this single SQL be better for my purposes then the two statements? If that would matter at all?
Thank you! -
Use param for SQL statement...
Hi, all,
Thanks for the help.
I have a select statement working in sqlplus:
SELECT A.addressid
FROM JOHNDOE.tb_Address A, JOHNDOE.tb_Address B
WHERE B.Addressid = 1
AND SDO_WITHIN_DISTANCE(A.Location, B.location, 'DISTANCE = 12.8 UNIT = MILE') = 'TRUE';
Now that I am coding in PL/SQL and to use a param to replace "12.8". Like the following:
var p_radius NUMBER;
exec :p_radius :=12.8;
print p_radius;
SELECT A.addressid
FROM JOHNDOE.tb_Address A, JOHNDOE.tb_Address B
WHERE B.Addressid = 1
AND SDO_WITHIN_DISTANCE(A.Location, B.location, 'DISTANCE = ' || p_radius || 'UNIT = MILE') = 'TRUE';
commit
Note that with "|| p_radius ||", this is NOT working. How should I put it?
Thanks a lot.
-xiaocaoTry putting a COLON in front of your reference to p_radius....
AND SDO_WITHIN_DISTANCE(A.Location, B.location, 'DISTANCE = ' || :p_radius || 'UNIT = MILE') = 'TRUE';
HTH
Jeff -
HELP - using variables in SQL statement
Hi,
I am sure that this is a small problems for the gurus out there.
I am trying to execute the following statement usign JDBC:
String myVariable = "XYZ";
sSQL = "select COL1, COL2 from TABLE1 where COL3 = '" + myVariable + "'";
CODE ABOVE DOES NOT WORK FOR ME!!!!!
HOWEVER, if I were to directly substitute the value XYZ as shown below, then things work. But that does not solve my problem.
sSQL = "select COL1, COL2 from TABLE1 where COL3 = '" + "XYZ" + "'";
Any suggestions?
nullUse methods setXXXX of the PreparedStatement class.
Connection conn =
DriverManager.getConnection("jdbc:default:connection:");
PreparedStatement stm=conn.prepareStatement(
"select name from persons where inn=?");
String inn="ASDDGF12345";
String name=new String;
stm.setString(1,inn);
ResultSet rss = stm.executeQuery();
if (rss.next())
name = rss.getString(1);
rss.close();
stm.close();
conn.close(); -
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 alter the page header in mode_show_help?
How can I change the page header on the page that gets opened by calling mode_show_help? I'd like to have the help pages maintain the consistancy of the look of the portal. thanks, adam
-
When I try to install Adobe Update, it tells me to close Internet Explorer. When I try to close it, I can't get it to close. Now what?
-
I was wondering if there was a way to do the following. I Like shooting B&W. I how to convert once the files are in PS or LR. I was wondering of it were possible to shoot in B&W in the camera and have some EXIF flag set to tell LR to apply a pre defi
-
J'ai la derniere version d'itunes, mais quand je veux faire la maj de mon iphone vers l'ios 5 le téléchargement se stoppe en plein milieu en affichant erreur 3259 et ce en wifi ou cablé, avec ou sans antivirus.
-
Email, calander and contact settings disables
I have a problem where my account settings for synchronizing my email, calender and contacts are disabled or "fall out". I've tried to reset the fabric setting and start over with the configuration steps a couple of times and a few days ago it seemed