PL/SQL: how to use in parameter in select sql where clause
Hi
in a procedure, I need to apply 'in parameter' in 'where clause' along with other table fields. Purpose is to create dynamic select querry with multiple conditions.
select count(*) from table xx
where y_code=2008 and v_type in ('SI', 'TI', 'DI') ;
my requirement is replace 'and v_type in ('SI', 'TI', 'DI')' with in parameter. pls note in paramter may contain null value.
Regards
... e.g. why on earth do you want to pass in a string to be appended to the WHERE clause of an SQL.I second that and I strongly advice NOT to do it. If you really want to do it, then come back and show us, how you would prevent SQL injection. This approach is too dangerous (and too complex) IMHO.
Do it straight forward as in the article of Tom Kyte (link in the post of BluShadow above)
Similar Messages
-
How to use the MAX DATE condition in WHERE CLAUSE FILEDS
Hi,
I am trying to fetch the result for getting maximun date but when i try to execute the query i am getting the error as follows.
CONDITION : trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')
ERROR: Group function is not allowed here.
CHEERS,
PRABU AMMAIAPPANI see a couple of problems here.
First, what you posted below is not a syntactically valid query. It seems to be part of a larger query, specifically, this looks to be only the GROUP BY clause of a query.
Prabu ammaiappan wrote:
Hi,
I Have a group function in the Query. Below is the Query i have used it,
GROUP BY S.FREIGHTCLASS,
R.CONTAINERKEY,
S.SKU,
S.DESCR ||S.DESCRIPTION2,
S.PVTYPE,
RD.LOTTABLE06,
R.WAREHOUSEREFERENCE,
RD.TOLOC,
R.ADDWHO,
R.TYPE,
S.CWFLAG,
S.STDNETWGT,
S.ORDERUOM,
R.ADDDATE,
C.DESCRIPTION,
(CASE WHEN P.POKEY LIKE '%PUR%' THEN 'NULL' ELSE to_char(P.PODATE,'dd/mm/yyyy') END),
NVL((CASE WHEN R.ADDWHO='BOOMI' THEN RDD.SUPPLIERNAME END),SS.COMPANY),
RDD.BRAND,
S.NAPA,
RD.RECEIPTKEY,
R.SUSR4,
P.POKEY,
RDD.SUSR1,
r.STATUS, DECODE(RDD.SUSR2,' ',0,'',0,RDD.SUSR2),
rd.SUSR3Second, the answer to your primary question, "How do I add a predicate with with a MAX() function to my where clause?" is that you don't. As you discovered, if you attempt to do so, you'll find it doesn't work. If you stop and think about how SQL is processed, it should make sense to you why the SQL is not valid.
If you want to apply a filter condition such as:
trunc(max(RD.DATERECEIVED)) BETWEEN TO_DATE('01/08/2011','DD/MM/YYYY') AND TO_DATE('01/08/2011','DD/MM/YYYY')you should do it in a HAVING clause, not a where clause:
select ....
from ....
where ....
group by ....
having max(some_date) between this_date and that_date;Hope that helps,
-Mark -
How to use a lexical variable in a WHERE clause and as a function argument.
Hello,
My report has a lexical variable which has values like 'and id in (1,3,4)'. I want to use this variable in two separate queries. In the first, it is used in the WHERE clause, something like:
.. where date is not null &var;
In the second query, the variable is used in a function call, something like:
select * from table (my_function(&var));
The trouble is that the first query works when the value of the variable is unquoted, but for the second query the value should be quoted.
Does anyone have an idea how I could solve this problem?
Thanks in advance,
Frans
Message was edited by:
fransiehttp://tkyte.blogspot.com/2006/06/varying-in-lists.html
Presents many options. -
How to use multiple search conditions in the where clause
Hi,
Below is my query
/****** Script for SelectTopNRows command from SSMS ******/
SELECT distinct
ctacct,sum(GLMN02)
FROM [ODS].[Staging].[tODS_INF_GLPCT]
inner join
[ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
where
CTACCT like '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
This one gives me the exact result i want, now if i add one more to my where clause like this
/****** Script for SelectTopNRows command from SSMS ******/
SELECT distinct
ctacct,sum(GLMN02)
--,ctdesc,CTPAGE
--SUM(GLMN02)
FROM [ODS].[Staging].[tODS_INF_GLPCT]
inner join
[ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
where
CTACCT like'[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%' or
CTACCT like '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
This query doesnt give me the exact values instead it gives me all the weird values.Can someone please help me with how to work on this where clause?
ThanksHi Patrick,
This is what i ve tried earlier and it isnt working .If i use the first query
/****** Script for SelectTopNRows command from SSMS ******/
SELECT distinct
ctdesc,SUM(GLMN02)
--,ctdesc,CTPAGE
--SUM(GLMN02)
FROM [ODS].[Staging].[tODS_INF_GLPCT]
inner join
[ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
where
CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
--OR
--CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
and GLYEAR = 2014
and CTDESC = 'Sales'
group by ctdesc
The result set is
Sales
-182273.96
And if i use the second query
SELECT distinct
ctdesc,SUM(GLMN02)
--,ctdesc,CTPAGE
--SUM(GLMN02)
FROM [ODS].[Staging].[tODS_INF_GLPCT]
inner join
[ODS].[Staging].[tODS_INF_GLPGL] ON tODS_INF_GLPCT.CTPAGE = tODS_INF_GLPGL.GLPAGE
where
CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4110%'
OR
CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
and GLYEAR = 2014
and CTDESC = 'Sales'
group by ctdesc
The result set i get is
Sales
-1455441.08
And i verified that CTAcct LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9]-63020-4115%'
this second line of where clause has not desc as Sales.
I am not sure whats causing the difference in the values.
Can you please help me with this?
Thanks -
Hi,
I have a column doc_id which value like abcd.01.023. If I
select floor(to_number(substr(doc_id, instr(doc_id, '.', 1)+1, instr(doc_id, '.', 1)+1 )))
it works,
but get ORA-01722: invalid number if I use in where clause like:
Where floor(to_number(substr(doc_id, instr(doc_id, '.', 1)+1, instr(doc_id, '.', 1)+1 ))) =0
Could you tell me how to fix?
ThanskMy guess is that you didn't process the same rows with each query. Maybe you have some where conditions on the 'select' query that filters out the offending rows. If the doc_id value could not be converted to a number is should raise the error in either case.
Besides that, your substring looks wrong to me. Why take "instr(doc_id, '.', 1)+1" characters? Why not just:
floor(substr(doc_id, instr(doc_id, '.') + 1))
Even then, the portion after the first '.' must be a valid number.
Richard -
How to use import parameter to be instead of SQL where sub-sentence ?
I wrote a RFC to read data from SAP table. To fetch data flexibility, I want to use import parameter xx instead of where sub-sentence in SQL sentence.
For example, "SELECT * FROM T WHERE XXX", and "XXX" is a importing parameter.
How can I use it.
Thanks a lot.
Frank.FUNCTION ZRFC_04.
*"*"Local Interface:
*" IMPORTING
*" VALUE(TARGETTABLE) LIKE MAKT-MAKTX
*" VALUE(TWHERE) LIKE MAKT-MAKTX
*" EXPORTING
*" VALUE(ZRETURN) LIKE MAKT-MAKTX
*" TABLES
*" TMP_TEST1 STRUCTURE ZTEST1
DATA:
TRANSACTION_ID LIKE ARFCTID,
V_VAILD(1) TYPE C,
scond(80) TYPE c.
V_VAILD = 'X'.
GET PARAMETER twhere fields scond.
The error " 'LATE FIELDS' expected, not 'TWHERE FIELDS' " generated. -
How to use shared parameter on request set
Hi
I have a request set with two stages
First stage contains From Date and To date where i have named the shared parameter as StartSP and EndSP
i have to use these shared parameter in one of the query in next stage where my Type is SQL Statment and i have these two date fields From date and To date in next stage as well where i am able to retirve the values from previous stage
based on these values i need to get the name in next parameter where my Type is SQL Statment
select name from tablename where '02-MAR-09' > =start_Date and '29-MAR-09' <=end_Date
I have written SQL as
select name from tablename where StartSP > =start_Date and EndSP <=end_Date but it throws an error.
I need the values of 02-Mar-09 and 29-Mar-09 from StartSP and EndSP
Appreciate if any one has idea about this.hi user;
please check:
http://download-west.oracle.com/docs/cd/A60725_05/html/comnls/us/fnd/repset04.htm
http://download-east.oracle.com/docs/cd/A60725_05/html/comnls/us/fnd/10gch609.htm
Also you can check this search:http://www.google.com.tr/search?hl=tr&source=hp&q=How+to+use+shared+parameter+on+request+set+&meta=&aq=f&oq=
Hope it helps
Regard
Helios -
How to use dynamic parameter when a report is created using Stored Procedures
Hi all,
any one have the idea of how to use dynamic parameter in crystal report XI R2
when report is created using Stored Procedure.
Regards
shashi kant chauhanHi
You can create an SQL command in Database Expert > Expand your datasource > Add command
Then enter the SQL query that will create the list of values to supply to the user
eg select field1,field2 from table
Then edit the parameter of the report. These will be the SP parameters adn can be seen in field explorer.
Change the parameter type to Dynamic
Under the word Value click on Click here to add item
Scroll down to your Command and select one of the values that you want to appear in the list
e.g field1
Then click on the Parameters field - this is essential to create the param
You can edit other options as required
That should do it for you.
I must say that i use CR 2008 connected to Oracle 10g SP, but i reckon this will work for SQL DB and CR XI as well
Best of luck -
How to use Temporary Table in PL-SQL
In MySQL there is no Temporary table concept.
So for intermediate calculation I have created a table as below
create table SequenceTempTable
SessionId VARCHAR(50),
Sequence VARCHAR(500),
CreatedDate DATE
) ENGINE=MEMORY;
Whenever I invoke a SP, I load the data into SequenceTempTable using Session Id as below
CREATE PROCEDURE `GetSequence`(
IN Start_Date VARCHAR(25),
IN End_Date VARCHAR(25)
BEGIN
SELECT UUID() INTO v_SessionId;
INSERT INTO SequenceTempTable values (v_SessionId,'1,2,5,3',now());
required code
DELETE FROM SequenceTempTable WHERE SessionId = v_SessionId;
COMMIT;
END;
i.e. I have created a table as temporary table (created once),
and load the data using Session Id and once session specific intermediate computation done,
I deleted the session specific data.
Could you give me examples of How to use Temporary table in PL-SQL code with respect to my above example.
Because I have gone through creating Temporary table but I stuck with use in PL-SQL. I mean to say Is there any need of creating table in advance before invoking SP.
And one more thing as in MySQL temp table I created which is using MEMORY engine i.e. this table will always be in MEMORY so there is no need of writing data on disk.
Regards
SanjeevHi Sanjeev
Read about GTT here
http://www.oracle-base.com/articles/8i/TemporaryTables.php
GTT always contains just session specific data. \
In case you want to use the GTT in the same session again you can use option
ON COMMIT PRESERVE ROWS;
Or if it is used just once in the session use can use
ON COMMIT DELETE ROWS;
Do remember that for GTT the data of one session can not be accessed in other session.
Also you can go away with Delete from GTT if not used again in same session.
Regards
Arun -
How to use a parameter of a report program from selection screen in a dialog program
how to use a parameter value(entered ) of a report program from a selection screen in a dialog program.
I have to fetch the value entered in the parameter of report program and display it in a dialog programHi Aasim,
Just mention like below in your ABAP report.
PARAMETERS: p_aufnr TYPE aufnr MEMORY ID ord.
and mention the same memory ID name in the module pool screen property it automatically populates the value to and fro -
How to use a parameter of a report program in a dialog program
how to use a parameter of a report program in a dialog program.
I have to fetch the value entered in the parameter of report program and display it in a dialog programHi Aasim,
Just mention like below in your ABAP report.
PARAMETERS: p_aufnr TYPE aufnr MEMORY ID ord.
and mention the same memory ID name in the module pool screen property it automatically populates the value to and fro -
How to use a parameter in a report program in a dialog program
how to use a parameter in a report program in a dialog program
Hi Shaikh,
Post it in http://scn.sap.com/community/abap
"ABAP Development".
sree -
How to use single column in a group by clause
hi frs
i hve used sum function in a sql how to use a single column in a query.
for ex
select sum(sal),ename,job,deptno,empno from emp
group by deptnowhether its possible to use single column in a group by class.
i want like this because in my custom report i need it. Report query has more than 30 column so its not possible to use 30 columns in a group by.
pls help.
Thanks
RajeshMaybe something like this -
Select ename, job, deptno, empno, sal, col1, col2, col3, col4 ......, coln
From
Select SUM(sal) sal, deptno deptno_i
From Emp
Group By deptno
,Emp
Where deptno = deptno_i
Shailender Mehta -
How to use a function in select clause
hi gems...good evening...
I want to write a select clause from a function. The scenario is like below:
function parameters
CREATE OR REPLACE FUNCTION FUNCTION_DEMO(p_pid IN NUMBER,
p_bankerNum IN NUMBER,
p_banker_name IN VARCHAR2,
p_business_num IN NUMBER,
p_businesses IN table_typ_businesses)
return table_typ_bankers
as .......
global object and table types
CREATE OR REPLACE TYPE obj_typ_businesses AS OBJECT (businesses NUMBER);
CREATE OR REPLACE TYPE table_typ_businesses AS TABLE OF obj_typ_businesses;
CREATE OR REPLACE TYPE obj_typ_bankers AS OBJECT (banker_num NUMBER(10,0));
CREATE OR REPLACE TYPE table_typ_bankers IS TABLE OF obj_typ_bankers;
The select query
select * from
table(cast(FUNCTION_DEMO(1,
2,
'ALEX',
2,
table(cast(select businesses_id from reference_businesses) as table_typ_businesses) as table_typ_bankers)But it is giving error with Missing expression.
My function is compiled successfully. I just want to make the select query to view the output given by the function i.e the table type "table_typ_bankers".
Please help...thanks in advance..Hi ,
You can check this and change your code by taking this as an example. I just noted down with simple one input and return as collection type
and how to use/call it in select statement.
SQL> CREATE OR REPLACE TYPE obj_typ_businesses AS OBJECT (businesses NUMBER)
2 /
Type created.
SQL> CREATE OR REPLACE TYPE table_typ_businesses AS TABLE OF obj_typ_businesses
2 /
Type created.
SQL> CREATE OR REPLACE TYPE obj_typ_bankers AS OBJECT (banker_num NUMBER(10,2))
2 /
Type created.
SQL> CREATE OR REPLACE TYPE table_typ_bankers IS TABLE OF obj_typ_bankers
2 /
Type created.
/* Now creating a demo function and just focusing on your one input as collection type */
SQL> create or replace function f11 ( tab_rec IN table_typ_businesses)
2 return table_typ_bankers
3 is
4 t_val table_typ_bankers:= table_typ_bankers();
5 begin
6 dbms_output.put_line('tab_rec count -'||tab_rec.count);
7
8 for i in 1..tab_rec.count
9 loop
10 dbms_output.put_line ('tab_rec value ('||i||')'||tab_rec(i).businesses);
11 t_val.EXTEND;
12 t_val(i) := obj_typ_bankers(tab_rec(i).businesses * 12.50);
13 end loop;
14 return t_val;
15 end;
16 /
Function created.
/* To run your function by a select statement */
SQL> SELECT * FROM TABLE(f11(table_typ_businesses(obj_typ_businesses(5),
2 obj_typ_businesses(12),
3 obj_typ_businesses(18))
4 ) )
5 /
BANKER_NUM
62.5
150
225
tab_rec count -3
tab_rec value (1)5
tab_rec value (2)12
tab_rec value (3)18Hope this will help you... :)
Thanks!
Ashutosh -
How to use case when in Select qry?
Hi Friends,
I want to use Case when in Select qry, my situation is like this
SELECT bmatnr blgort bj_3asiz bmat_kdauf b~mat_kdpos
SUM( ( case when bshkzg = 'S' then bmenge else 0 END ) -
( case when bshkzg = 'H' then bmenge else 0 END ) ) AS qty
INTO corresponding fields of table it_projsal
FROM mseg AS b
INNER JOIN mkpf AS a ON bmblnr = amblnr
AND bmandt = amandt
WHERE abudat < '20061201' AND blgort IN ('1050')
and b~mandt = '350'
GROUP BY bmatnr bj_3asiz bmat_kdauf bmat_kdpos b~lgor
If we give like this it gives an error.
Please help me, how to use or handle in select qry itself.
Regards
Shankarthis is not a way to select data from the DB tables.
first get all the data from the DB tables then u have to do SUM Ups .
Regards
prabhu
Maybe you are looking for
-
Since the last update my i tunes wont launch it just give error message Error 7 (Windows error 127) how do i fix it please?
-
MPD won't load after compiling x264 [Solved]
I had to compile a recent version of x264 so I could install mplayer-mt-git, but now I noticed MPD is not working. After some fiddling around, I figured out MPD wants to load libx264.so.67 although I have libx264.so.80. How do I fix this? Last edited
-
I think I know what the answer is going to be here, but I'm going to ask anyway. The background here is that I've made a few demo videos for new products and features my company is working on. I'm using CP4 for this. Our marketing department wants to
-
I just bought a 4s and it charges in the wall but not when plugged into my macbook pro
help!? my macbook pro ( up to date ) and my 4S ( up to date ) won't charge when plugged into the computer.. it works with the wall charger but not on the computer... do i need to resintall usb drivers?
-
Hi! Today we have two Exchange sites running Exchange 2010 sp3. Both sites are internet facing and have a DAG deployed. Currently we have two names for webmail. for site europe. internalurl:webmail-eu.company.xx externalurl:webmail-eu.company.xx For