How to : rownum and order by in select statement
Say I have a select statement with additional 'order by' clause.
The result set I want prepended by the rownum column.
But, and here comes the flaw, I want the rownum for the already ordered result set
not the unordered.
An example:
select firstname, lastname from myTable order by lastname;
When I add the rownum to the select clause,
'select rownum, firstname, lastname from myTable order by lastname;'
I might get something like:
20 Mike Adams
13 Nina Bravo
1 Tom Charlie
But I want the following result:
1 Mike Adams
2 Nina Bravo
3 Tom Charlie
I could now
'Select rownum, lastname, firstname from (select firstname, lastname from myTable order by lastname);
But I guess there is a better way!?!
which is the best way to accomplish that?
Thanks for your advice!
>
'Select rownum, lastname, firstname from (select firstname, lastname from myTable order by lastname)
>
Well if you ask me there is very little difference between this query and the above query
select rownum, lastname, firstname from mytable;Because rownum is assigned before the order by. The difference is in your query you are assigning a rownum to an ordered resultset but still there is no guarantee oracle is going to read the data in an ordered fashion. In the second query rownum is assigned to an unordered resultset. Again it is the samething. So if you want to guarantee it then I will go for the following option
select row_number() over(order by lastname) rn, lastname, firstname from mytable
order by lastnameAlso check this link.
http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html
Regards
Raj
Edited by: R.Subramanian on Jan 13, 2009 6:20 AM
Similar Messages
-
How to compile the hint to force selection statement to use index
Hello expert,
will you please tell me how to compile the hint to force selection statement to use index?
Many Thanks,Not sure what you mean by compile, but hint is enclosed in /*+ hint */. Index hint is INDEX(table_name,index_name). For example:
SQL> explain plan for
2 select * from emp
3 /
Explained.
SQL> @?\rdbms\admin\utlxpls
PLAN_TABLE_OUTPUT
Plan hash value: 3956160932
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 14 | 546 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS FULL| EMP | 14 | 546 | 3 (0)| 00:00:01 |
8 rows selected.
SQL> explain plan for
2 select /*+ index(emp,pk_emp) */ *
3 from emp
4 /
Explained.
SQL> @?\rdbms\admin\utlxpls
PLAN_TABLE_OUTPUT
Plan hash value: 4170700152
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 14 | 546 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 546 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | PK_EMP | 14 | | 1 (0)| 00:00:01 |
9 rows selected.
SQL> Hint in the above example is forcing optimizer to use index which resul;ts in a bad execution plan. Most of the time optimizer does not need hints and chooses an optimal plan. In most cases sub-optimal plan is result of stale or incomplete statistics.
SY. -
How to avoid data repetation when using select statements with innerjoin
how to avoid data repetation when using select statements with innerjoin.
thanks in advance,
satheeshyou can use a query like this...
SELECT DISTINCT
frg~prc_group1 "Product Group 1
frg~prc_group2 "Product Group 2
frg~prc_group3 "Product Group 3
frg~prc_group4 "Product Group 4
frg~prc_group5 "Product Group 5
prc~product_id "Product ID
txt~short_text "Product Description
UP TO 10 ROWS
INTO TABLE l_i_data
FROM
Joining CRMM_PR_SALESG and
COMM_PR_FRG_ROD
crmm_pr_salesg AS frg
INNER JOIN comm_pr_frg_rod AS prd
ON frgfrg_guid = prdfragment_guid
Joining COMM_PRODUCT and
COMM_PR_FRG_ROD
INNER JOIN comm_product AS prc
ON prdproduct_guid = prcproduct_guid
Joining COMM_PRSHTEXT and
COMM_PR_FRG_ROD
INNER JOIN comm_prshtext AS txt
ON prdproduct_guid = txtproduct_guid
WHERE frg~prc_group1 IN r_zprc_group1
AND frg~prc_group2 IN r_zprc_group2
AND frg~prc_group3 IN r_zprc_group3
AND frg~prc_group4 IN r_zprc_group4
AND frg~prc_group5 IN r_zprc_group5.
reward it it helps
Edited by: Apan Kumar Motilal on Jun 24, 2008 1:57 PM -
Trouble with subquery and rownum and ordering
I'm having trouble making this subquery work. The basic idea is that the web app will show only so many rows of results on the page, but right now I'm just playing around in SQL*Plus. The address book holds mixed case, so in order to sort by name properly I need to use UPPER to ignore case sensitivity. This SQL statement works fine for me:
select addressbookid from addressbook where addressbookid like '905430931|%' order by upper(addressbookid);I know that if you mention ROWNUM in the WHERE clause, you're referring to the row numbers of the ResultSet that Oracle returns. How do I use both ORDER BY UPPER(ADDRESSBOOKID) and WHERE ROWNUM > 25 AND ROWNUM <= 50? I figured a subquery would do it, but I can't write a correct one that does it! Below are my 2 attempts with the errors, and then I just tried to play with rownum:
SQL> select addressbookid from addressbook where addressbookid in (select addressbookid from address book where addressbookid like '905430931|%' order by upper(addressbookid));
select addressbookid from addressbook where addressbookid in (select addressbookid from addressbook
ERROR at line 1:
ORA-00907: missing right parenthesis
SQL> select addressbookid from addressbook where addressbookid in upper(select addressbookid from addressbook where addressbookid like '905430931|%');
select addressbookid from addressbook where addressbookid in upper(select addressbookid from address
ERROR at line 1:
ORA-00936: missing expression
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50 order by upper(addressbookid);
no rows selected
SQL> select addressbookid from addressbook where addressbookid like '905430931|%' and rownum > 25 and rownum <= 50;
no rows selectedLike I said, if I can get a working subquery, then I'd like to attach that restriction on the rownum stuff. I'm wondering if it will be a problem trying to get the ordering to happen and then the rownum restriction after that, and all in the same query...
Btw, we've made all the table and column names in our database uppercase, so that shouldn't matter.This is probably the most efficient way ...
select addressbookid
from
select addressbookid,
rownum rn
from
select addressbookid
from addressbook
where addressbookid like '905430931|%'
order by addressbookid
where rownum <= 50
where rn > 25 -
Rownum and order by as parameter
i want a procedure that will accept the 'from' and 'to' parameter (rownum) for paginaton as well as the order by column also as a parameter( the order by changes based on the parameter) , and my query is using multiple table which doesnt have Unique keys, the pagination is not working poperly at that time..
please have a look in to the procedure..
CREATE OR REPLACE Procedure P_pagination
(cur out sys_refcursor,end1 number,start1 number,ordr number)
as
Var_sql varchar2(4000);
begin
var_sql := ' Select * '||
' From '||
' (select rownum rwnum,aa.* from ' ||
' (select ti.a,t2.a,t3.a,t4.b from t1,t2,t3,t4 where < all the joins> order by '||ordr||' )aa'||
' where rownum <='|| end1 ||') ' ||
' where rwnum >='|| start1 ;
open cur for var_sql;
end ;
/If you don't specify explicit ORDER BY clause
Oracle wont guarantee the order of the sort. You specify ORDER BY ID , but
not ORDER BY ID, <<something>> , so you have unrepeatable results. Your order by ID but don't order inside ID. Look:
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id) a
8 where rownum <= 150
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 34 148
0 54 149
0 81 150
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id) a
8 where rownum <= 151
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 28 148
0 34 149
0 54 150
0 81 151
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id, rowid) a
8 where rownum <= 150
9 )
10 where rnum >= 148
11 /
ID DATA RNUM
0 24 148
0 21 149
0 35 150
SQL> select *
2 from
3 (select a.*, rownum rnum
4 from
5 (select id, data
6 from t
7 order by id, rowid) a
8 where rownum <= 151
9 )
10 where rnum >= 148;
ID DATA RNUM
0 24 148
0 21 149
0 35 150
0 68 151Rgds. -
How to create MIN/MAX limitations in SELECT statement ??
Hey,
I have a table which rank90 (city population ranked from 1>*) and state_abrv which has the corresponding state for each city rank.
Is there a way to select only the maximum AND minimum ranks for each state ??
I realise there is a max and min function, but i need to do it for EACH state_abrv.
An example say Los Angeles is ranked 2, San Diego is ranked 6, and San Fransico is ranked 14 (All of these citys are in california (CA)). How do i display a table which lists only Los Angeles (Highest rank) and San Fransico (lowest rank) but DOESNT list San diego ??
Thanks, you guys are helping me heaps and im starting to learn a lot more :P
Message was edited by:
user495524SQL> create table t (state varchar2(2), city varchar2(20), n number);
Table created.
SQL> insert into t values ('CA','San Francisco',14);
1 row created.
SQL> insert into t values ('CA','San Diego',6);
1 row created.
SQL> insert into t values ('CA','Los Angeles',2);
1 row created.
SQL> insert into t values ('NY','Buffalo',4);
1 row created.
SQL> insert into t values ('NY','Syracuse',7);
1 row created.
SQL> insert into t values ('NY','Mt Kisco',2);
1 row created.
SQL> insert into t values ('NY','Albany',5);
1 row created.
SQL> select * from t order by state, n desc;
ST CITY N
CA San Francisco 14
CA San Diego 6
CA Los Angeles 2
NY Syracuse 7
NY Albany 5
NY Buffalo 4
NY Mt Kisco 2
7 rows selected.
SQL> select state, city, n from
2 (
3 select t.*, min(n) over (partition by state) min_n,
4 max(n) over (partition by state) max_n from t
5 )
6 where n in (min_n, max_n) order by state, n desc;
ST CITY N
CA San Francisco 14
CA Los Angeles 2
NY Syracuse 7
NY Mt Kisco 2
SQL> -
How to create a mapping for a select statement containing DENSE_RANK( )?
Hi,
I want help with a select statement that I want to make a mapping of in OWB 11.1 g. Can anyone please tell me how is code can be incorporated in a mapping?
SELECT DISTINCT MAX (dimension_key) KEEP (DENSE_RANK FIRST ORDER BY day DESC) OVER (PARTITION BY calendar_week_name),
MAX (day) KEEP (DENSE_RANK FIRST ORDER BY DAY DESC) OVER (PARTITION BY calendar_week_name), calendar_week_end_date, calendar_week_number
FROM time_dim;I have been trying to use the Aggregator operator but I am not entirely sure how to go about it. Any help will be highly appreciated.
Thanks in advance,
Ann.Hi Ann
You can just use an EXPRESSION operator. Configure the mapping to be set based only code generation and operating mode.
You will have an expression output attribute for each one of your projected columns;
MAX (dimension_key) KEEP (DENSE_RANK FIRST ORDER BY day DESC) OVER (PARTITION BY calendar_week_name),
MAX (day) KEEP (DENSE_RANK FIRST ORDER BY DAY DESC) OVER (PARTITION BY calendar_week_name),
calendar_week_end_date,
calendar_week_number
Cheers
David -
How to use variables in an sql select statement.
Hi, I have seen examples using the update, but nothing using a select statement.
I have four variables I am getting from drop down menus in my JSP I set the user selected items to strings.
How would I create the select statement like this:
.String XName = request.getParameter("UserInputX");
rsInResult = stmtInResult.executeQuery("SELECT ColumxX FROM TableZ WHERE ColumnX = XName")Obviously it tries to read "XName" as a column and of course can't find it, how would I set it up to have it see the Value of XName and not the literal XName.read this:
rsInResult = stmtInResult.executeQuery("SELECT ColumxX FROM TableZ WHERE ColumnX = '"+XName+"')
{code}
better way is PreparedStatement:
{code}
// example code to change password to an user in the USERS table
String newPWD = "my password";
String user = "luxjava";
PreparedStatement prepStat;
String updatePWD = "UPDATE LOGISTIC.USERS "+
"SET logistic.users.password = ? "+
"WHERE logistic.users.username = ?";
prepStat = ql.conDb.prepareStatement(updatePWD);
prepStat.setString(1, newPwd);
prepStat.setString(2, user);
int zeroNotFound = prepStat.executeUpdate();
if (zeroNotFound==0)
ql.conDb.rollback();
prepStat.close();
else
ql.conDb.commit();
prepStat.close();
{code}
... use the SQLException to capture errors -
How: making column value as column of select statement
Hi
Quickly I have searched in this forum for following solution but not able to find it.
I need to make a query in such a way that the value of one column in one table should act as a column-name of another table
Eg:-
tab1
col1,col2,*col3*,col4
tab2
col11 col22 col33 col44
row1 a b col3 d
row2 aa bb cc dd
So the query should be something like --- in row2 if the value is col3 then from tab1 I should pick the col3 in select statement (basically there will be some join in tab1 and tab2 )
Hope I am able to make u all understand my query.
regardsHi all,
Sorry for late response.Just now I checked all the replies.
Actually I tried in this way.
CREATE OR REPLACE function Proc_caption (in_tan varchar2, in_order in number)
return varchar2
is
d varchar2(100) ;
begin
SELECT col1 into d
from tab1
Where
col2 = in_tanid
AND col3 = 'Y'
AND col4 = in_order ;
return d ;
Exception
WHen No_DATA_FOund then
return 'N' ;
end ;
create or replace procedure proc_generate_view(in_tan varchar2, in_soc varchar2)
is
x varchar2(2000);
v4 varchar2(50) ;
v5 varchar2(50) ;
cnt number ;
begin
v4:= PROC_Caption (in_tan,4) ;
v5:= PROC_Caption (in_tan,5) ;
x := ' Create view v_generate as '
|| ' SELECT a , b, c, d ' ;
if v4 <> 'N' THEN
x := x || ' , ' || PROC_Caption (in_tan,4) || ' as Caption4 ' ;
end if;
if v5 <> 'N' THEN
x := x || ' , ' || PROC_Caption (in_tan,5) || ' as Caption5 ' ;
end if;
x := x || ' FROM tab2 WHERE col1 = ''' || in_socid || ''''
|| ' AND col2 = ''' || in_tan || '''' ;
select count(1) into cnt from USER_OBJECTS where OBJECT_NAME = 'V_GENERATE'
and OBJECT_TYPE = 'VIEW' ;
if cnt > 0 then
EXECUTE IMMEDIATE ' DROP view V_GENERATE' ;
END IF ;
execute immediate x ;
end ;
NOTE:- I have just renamed the table name and column names in order to make it non confedential...
I have created this successfully with actual table name and column names and able to get the result as per my requirement.
regards -
In how many ways we can filter this select statement to improve performance
Hi Experts,
This select statement taking 2.5 hrs in production, Can we filter the where condition, to improve the performance.Plz suggest with coding ASAP.
select * from dfkkop into table t_dfkkop
where vtref like 'EPC%' and
( ( augbd = '00000000' and
xragl = 'X' )
or
( augbd between w_clrfr and w_clrto ) ) and
augrd ne '03' and
zwage_type in s_wtype .
Regards,
Sam.if it really takes 2.5 hours, try the followingtry to run the SQL trace and
select *
into table t_dfkkop
from dfkkop
where vtref like 'EPC%'
and augbd = '00000000' and xragl
and augrd ne '03'
and zwage_type in s_wtype .
select *
appending table t_dfkkop
from dfkkop
where vtref like 'EPC%'
and augbd between w_clrfr and w_clrto
and augrd ne '03'
and zwage_type in s_wtype .
Do a DESCRIBE TABLE after the first SELECT and after the second,
or run an SQL Trace.
What is time needed for both parts, how many records come back, which index is used.
Siegfried -
How do you include static text in select statement in Dynamic pl/sql
I want to include some atatic text and get the output of 4 columns joined with a ":" delimiter within them in a select statement built using Dynamic PL/SQL. How do I build it.
e.g.
Normal select statement would be
select 'MY SKU IS : ', col1||':'||col2||':'||col3||':'||col4 from table1 where ....where condition
and output looks like :
MY SKU IS A:B:C:D
MY SKU IS a:b:c:d
Dynamically I have -
SQL_Stmt := 'select 'MY SKU IS : ', col1||':'||col2||':'||col3||':'||col4 from table1 where '|| wherecondition;
I understand that this does not work because the single quote terminates the string. But my question is how do I achieve the same result with dynamic PL/SQL ? How do I include the static strings and the delimiters. I have tried using double quote, '\'....
????SQL_Stmt := 'select ''MY SKU is ' || col1 || '':'' ||
col2 || '':'' ||
col3 || '':'' ||
col4 ||
' from table1 where ' || wherecondition; -
Output in different order than in select statement.
I'm executing the following query. Just wondering if I'm missing something:
select a.constraint_name, a.table_name, a.r_constraint_name, b.column_name, c.table_name, c.column_name from user_constraints a
inner join user_cons_columns b on (a.table_name=b.table_name and a.constraint_name=b.constraint_name)
inner join user_ind_columns c on (a.r_constraint_name=c.index_name)
where a.r_constraint_name='QUE_PK' and b.position=c.column_position
order by b.position
(replace QUE_PK with your own primary key referenced in a foreign key)
The columns appearing in the resultset are in different order than in the select statement.
They came as follows: constraint_name, r_constraint_name, table_name, table_name, column_name, column_name. If I aliase one of the column, the result set is ok.
I'm using version 1.1.0.23 on Ubuntu 6.10 against a 10g r2.To double check, you can go to <sqldeveloper>/system/oracle.sqldeveloper.1.1.0 (this may be <user_dir>/system/oracle.sqldeveloper.1.1.0 on your computer) and delete all of the *TableSettings.xml files. That will erase any custom column orders that you may have saved.
-
How to pass datetype parameter in Sql Select Statements to
public void Mountain()
String dat=lastcollecteddatestr.substring(0,2);
String mon=lastcollecteddatestr.substring(3,5);
String yr=lastcollecteddatestr.substring(6,10);
try
SimpleDateFormat sdf = new SimpleDateFormat("yyyy"+"MM"+"dd");
java.util.Date utilDate = new java.util.Date(Integer.parseInt(yr)-1900,Integer.parseInt(mon),Integer.parseInt(dat));
Date newdate=sdf.parse(yr+"-"+mon+"-"+dat);
System.out.println(newdate);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(url);
Statement stat = con.createStatement();
ResultSet rs=stat.executeQuery("select empcd,date from mmm where date = '"+newdate+"' ");
while(rs.next())
String str1= new String(rs.getString("empcd"));
String str2= new String(rs.getString("date"));
System.out.println(str1+"\t"+str2);
}catch(Exception ex){System.out.println(ex);}
Error : [MicroSoft] [ODBC Visual FoxPro Driver]Operator/operand type mismatch.
myNote: There is no Problem with connecting to database of something like that
its working well with Different SQL Statment
like: ("select empcd,date from mmm where empcd = '"+empcdstr+"' ")
This is not Working and giving errors
like: ("select empcd,date from mmm where date = '"+newdate+"' ")
HOW TO SOLVE THIS PLZ HELP. It's Urgent.You will see this error if the search condition was entered with an invalid or missing relational operator.
You need to include a valid relational operator such as
=, !=, ^=, <>, >, <, >=, <=, ALL, ANY, [NOT] BETWEEN, EXISTS, [NOT] IN, IS [NOT] NULL, or [NOT] LIKE in the condition. in the sql statement.
Can you throw some more light on how are you designing your project? -
How to increment variable value in single select statement
Hi guys
in this select statement i have hard coded date value but i need to put variable instead of hard coded date and then i want to increment that variable value till sysdate.. i have tried using curser , type tables but they are very very slow .. any experiance guys can give me good hint what should i use.
my query
select
start_dt,
end_dt,
hi_start_dt,
hi_end_dt,
ph_start_dt,
ph_end_dt,
h_start_date,
h_end_date,
g_code,
emp_det.ref,
u_code,
costing,
emp_nm,
emp_no
from
emp_det,
emp_ph_det,
emp_hi_det,
emp_h_det
where
emp_det.ref(+) = emp_ph_det.ref
and emp_hi_det.p_ref(+) = emp_ph_det.p_ref
and emp_h_det.ph_ref = emp_ph_det.ph_ref
and emp_h_det.ph_st_dt(+) = emp_hi_det.st_date;
and to_date('01-MAR-2008') between i.start_dt and nvl(i.end_dt, to_date('01-MAR-2008') +1)
and to_date('01-MAR-2008') between i.hi_start_dt and nvl(i.hi_end_dt, to_date('01-MAR-2008') + 1)
and to_date('01-MAR-2008') between i.ph_start_dt and nvl(i.ph_end_dt, to_date('01-MAR-2008') + 1)
and to_date('01-MAR-2008') between i.h_start_date and nvl(i.h_end_date, to_date('01-MAR-2008') + 1)
or
(----emp has left this month
i.start_dt < i.emp_end_dt
and i.end_dt between add_months(to_date('01-MAR-2008'), -1) + 1 and to_date('01-MAR-2008')
and i.hi_start_dt < i.hi_end_dt
and i.hi between add_months(to_date('01-MAR-2008'), -1) + 1 and to_date('01-MAR-2008')
and i.ph_start_dt < i.ph_end_dt
and i.ph_end_dt between add_months(to_date('01-MAR-2008'), -1) + 1 and to_date('01-MAR-2008')
and i.h_start_date < i.h_end_date
and i.h_end_date between add_months(to_date('01-MAR-2008'), -1) + 1 and to_date('01-MAR-2008')Hi Anurag
Thanks for the reply.please find my sample data below . below i am only showing data for one employee only.. i want to write a query where i will query for a month like march 2008 and then i need to find out the record for employe where this month march 2008 is between all the start and end dates like it should be between start_dt and end_dt and h_start_date and h_end_date and hi_strt_dt and hi_end_dt and ph_start_dt and ph_end_dt and where all the combination are true show me that record only .. i don't want any other record.
h_start h_end_
start_dt end_dt date date histrt_dt hi_end_dt ph_start_dt ph_end_dt
1-Sep-07 31-Dec-08 8-Feb-08 31-Aug-08 1-Sep-07 31-Dec-07 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 1-Sep-07 31-Dec-07 1-Sep-07 31-Dec-07 1-Sep-07 31-Dec-07
1-Sep-07 31-Dec-08 1-Sep-08 31-Dec-08 1-Sep-07 31-Dec-07 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 8-Feb-08 31-Aug-08 1-Aug-08 31-Aug-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 1-Sep-07 31-Dec-07 1-Aug-08 31-Aug-08 1-Sep-07 31-Dec-07
1-Sep-07 31-Dec-08 1-Sep-08 31-Dec-08 1-Aug-08 31-Aug-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 8-Feb-08 31-Aug-08 1-Oct-08 31-Dec-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 1-Sep-07 31-Dec-07 1-Oct-08 31-Dec-08 1-Sep-07 31-Dec-07
1-Sep-07 31-Dec-08 1-Sep-08 31-Dec-08 1-Oct-08 31-Dec-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 8-Feb-08 31-Aug-08 1-Sep-08 30-Sep-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 1-Sep-07 31-Dec-07 1-Sep-08 30-Sep-08 1-Sep-07 31-Dec-07
1-Sep-07 31-Dec-08 1-Sep-08 31-Dec-08 1-Sep-08 30-Sep-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 8-Feb-08 31-Aug-08 8-Feb-08 31-Jul-08 8-Feb-08 31-Dec-08
1-Sep-07 31-Dec-08 1-Sep-07 31-Dec-07 8-Feb-08 31-Jul-08 1-Sep-07 31-Dec-07
1-Sep-07 31-Dec-08 1-Sep-08 31-Dec-08 8-Feb-08 31-Jul-08 8-Feb-08 31-Dec-08 -
How to use bind variable in this select statement
Hi,
I have created this procedure where table name and fieldname is variable as they vary, therefore i passed them as parameter. This procedure will trim leading (.) if first five char is '.THE''. The procedure performs the required task. I want to make select statement with bind variable is there any possibility to use a bind variable in this select statement.
the procedure is given below:
create or replace procedure test(tablename in varchar2, fieldname IN varchar2)
authid current_user
is
type poicurtype is ref cursor;
poi_cur poicurtype;
sqlst varchar2(250);
THEVALUE NUMBER;
begin
sqlst:='SELECT EMPNO FROM '||TABLENAME||' WHERE SUBSTR('||FIELDNAME||',1,5)=''.THE ''';
DBMS_OUTPUT.PUT_LINE(SQLST);
OPEN POI_CUR FOR SQLST ;
LOOP
FETCH POI_CUR INTO THEVALUE;
EXIT WHEN POI_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(THEVALUE);
SQLST:='UPDATE '||TABLENAME|| ' SET '||FIELDNAME||'=LTRIM('||FIELDNAME||',''.'')';
SQLST:=SQLST|| ' WHERE EMPNO=:X';
DBMS_OUTPUT.PUT_LINE(SQLST);
EXECUTE IMMEDIATE SQLST USING THEVALUE;
END LOOP;
COMMIT;
END TEST;
Best Regards,So you want to amend each row individually? Is there some reason you're trying to make this procedure run as slow as possible?
create or replace procedure test (tablename in varchar2, fieldname in varchar2)
authid current_user
is
sqlst varchar2 (250);
thevalue number := 1234;
begin
sqlst := 'update ' || tablename || ' set ' || fieldname || '= ltrim(' || fieldname || ',''.'') where substr(' || fieldname
|| ',1,5) = ''.THE ''';
dbms_output.put_line (sqlst);
execute immediate sqlst;
end test;will update every row that satisfies the criteria in a single statement. If there are 10 rows that start with '.THE ' then it will update 10 rows.
Maybe you are looking for
-
MEM_BAD_POINTER error while opening disocoverer application
Hi, I installed successfully Oracle discoverer 10G (desktop & administrator) version 10.1.2.48.18. OS is windows xp professional with service pack 2 & 1GB RAM. While trying to open the applicaiton intially itself, i'm getting the following error smar
-
Any solutions for URL based routing
Hi, I have an ASA 5505 that has 2 route (1 route connecting to MPLS VPN to HK branch office and 1 route connecting to Internet service provider). As you know, ISP in China blocking many web sites (such as facebook, youtube or etc.). So , I would like
-
Grid installtion failing with error Cannot connect to Node Manager. : Conf
I am trying to install Grid, however its failing with following error I installed Weblogic server 10.3.4 and 11g R2 database ============================================================== Code of Exception: Error occured while performing nmConnect :
-
Purchasing Document of specific Condition Type & Order Type
Dear Friends, Can someone adivse me quickly from which table i can locate the Purchase Order's with document type and specific ITEM level condition type for given period. Thanks for your quick inputs. Ravi
-
JavaFX stage crashing on program start, Windows 7 Home Premium, NB 6.9 RC2
Hey all, I'm creating a Java/JavaFX application and have been using an interface to pass data back between the two programs. Upon running the program in NetBeans 6.9 RC2, it sometimes runs but often crashes. I'll post up some code, but I think it is