Passing parameter to a SQL query - Please help
Hi All,
I am new to JDBC. I have been trying to pass an external variable to an SQL Query.
The query is
String username1="le";
PreparedStatement pstmt = null;
pstmt = c.prepareStatement("select * from users where USER_NAME like '%?%'");
pstmt.setString(1, username1);
pstmt.executeQuery();
ResultSet rs = pstmt.getResultSet();
I am trying to retrieve values from the users table where the USER_NAME column value that is a String contains the supplied value username1.
I am using the question mark (?) character to pass the value from the variable username1. I am also using the '%' substitution character which matches for any number of characters. So, the above query should retrieve rows where the USER_NAME is something like "charles","leander","Elena" etc.( that contains "le")
I am getting the error:
SQLException: java.sql.SQLException: ORA-01006: bind variable does not exist
I changed the query to
PreparedStatement pstmt = null;
pstmt = c.prepareStatement("select * from users where USER_NAME like '% " + username1 + "%'");
//pstmt.setString(1, username1);
pstmt.executeQuery();
This time , it is not giving the error and retrieving properly.
But I want to use the original query and use the "pstmt.setString(1, username1); " . Is there any way of achieving this?
Please help.
Cheers,
charles_am
hi,
try this...
String username1="%le%";
pstmt = c.prepareStatement("select * from users where USER_NAME like ?")
pstmt.setString(1,username1);
cheers,
rpk
Similar Messages
-
Cluster bar chart- sql query please help-
Hi,
I am trying to create cluster bar chart and am stumped with this sql query.Any help is appreciated.
Here is my table
city region issue value
c1 north i1 y
c1 north i2 y
c2 north i1 n
c2 north i2 y
c3 south i1 y
c3 south i2 n
c4 east i1 n
c4 east i2 n
The bar chart will have 3 series, north south and east.
And labels will be i1 and i2. value will be number of times this issue was encountered(y) in this region.
How can I get something like this from the above table-
region issue count(yes)
north i1 1
north i2 2
south i1 1
south i2 0
east i1 0
east i2 0
thanksWITH table1 AS
(SELECT 1435177 qte_id, 2 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 5 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 7 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 8 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 12 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 14 seq_no
FROM dual
table2 AS
SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
FROM dual
SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
FROM
(SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
FROM table1 t1, table2 t2
WHERE t2.qte_id = t1.qte_id (+)
AND t2.seq_no = t1.seq_no(+)
ORDER BY 1,2
WHERE t1_seq_no IS NOT NULL
ORDER BY 1,2
QTE_ID T2_SEQ_NO DIFF
1435177 2 0
1435177 5 0
1435177 7 0
1435177 8 0
1435177 12 0
1435177 14 0 -
Hi experts,
what i want to do is write a single query which will show whether a employee
exits in the company or not.I have two tables emp and dept.There are as follows.
SQL> select * from emp;
NAME DEPTNO EMPNO
xxx 10 33036
YYY 12 2345
ZZZ 13 678
KKK 14 5678
RRR 15 7865
SQL> select * from dept;
DEPTNO LOCATION
10 AAA
11 BBB
12 CCC
13 DDD
what i want is it will select records from the emp table and find whether corrosponding
deptno really exists in the dept table.If the value is found in deptno column the dept table then it will set the value
Y other wise it will be N and all i have to do with the help of a single query.
expected result
name empno exists
xxx 33036 Y
YYY 2345 Y
ZZZ 678 Y
KKK 5678 N
RRR 7865 N
Please help.
Regards
RajatSELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N') EXIST
FROM EMP
ORDER BY 1
Demo
SQL> WITH EMP AS(
2 SELECT 'XXX' NAME , 10 DEPTNO ,33036 EMPNO FROM DUAL UNION
3 SELECT 'YYY', 12, 2345 FROM DUAL UNION
4 SELECT 'ZZZ', 13 ,678 FROM DUAL UNION
5 SELECT 'KKK', 14 ,5678 FROM DUAL UNION
6 SELECT 'RRR', 15 ,7865 FROM DUAL ),
7 DEPT AS(
8 SELECT 10 DEPTNO,'AAA' DNAME FROM DUAL UNION
9 SELECT 11 ,'BBB' FROM DUAL UNION
10 SELECT 12 ,'CCC' FROM DUAL UNION
11 SELECT 13 ,'DDD'FROM DUAL )
12 SELECT EMPNO, NAME , EMPNO , NVL( ( SELECT 'Y' FROM DEPT WHERE EMP.DEPTNO=DEPT.DEPTNO),'N')
EXIST
13 FROM EMP
14 ORDER BY 1
15 /
EMPNO NAM EMPNO E
678 ZZZ 678 Y
2345 YYY 2345 Y
5678 KKK 5678 N
7865 RRR 7865 N
33036 XXX 33036 Y
SQL> Edited by: Salim Chelabi on Dec 7, 2008 4:15 AM -
Table1
QTE_ID SEQ_NO
1435177 2
1435177 5
1435177 7
1435177 8
1435177 12
1435177 14
Table2
QTE_ID SEQ_NO CMMT_CURR_AMT
1435177 1 98500
1435177 2 98500
1435177 3 0
1435177 4 98500
1435177 5 98500
1435177 7 98500
1435177 8 98500
1435177 11 59300
1435177 12 59300
1435177 14 59300
The result should be
QTE_ID SEQ_NO CMMT_CURR_AMT
1435177 2 0 (where 0 = cmmt_curr_amt of seq 2 - cmmt_curr_amt of seq 1 from table 2)
1435177 5 0 (where 0 = cmmt_curr_amt of seq 5 - cmmt_curr_amt of seq 4 from table 2)
1435177 7 0 (where 0 = cmmt_curr_amt of seq 7 - cmmt_curr_amt of seq 5 from table 2)
1435177 8 0 (where 0 = cmmt_curr_amt of seq 8 - cmmt_curr_amt of seq 7 from table 2)
1435177 12 0 (where 0 = cmmt_curr_amt of seq 12 - cmmt_curr_amt of seq 11 from table 2)
1435177 14 0 (where 0 = cmmt_curr_amt of seq 14 - cmmt_curr_amt of seq 12 from table 2)
I have to get the difference of cmmt_curr_amt from the table2 from seq 14 to seq 12 for seq14 in table 1.
Please help me in writing the query.
Thanks in advance.
SrinivasWITH table1 AS
(SELECT 1435177 qte_id, 2 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 5 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 7 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 8 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 12 seq_no
FROM dual
UNION ALL
SELECT 1435177 qte_id, 14 seq_no
FROM dual
table2 AS
SELECT 1435177 qte_id, 1 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 2 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 3 seq_no, 0 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 4 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 5 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 7 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 8 seq_no, 98500 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 11 seq_no, 59300 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 12 seq_no, 59300 cmmt_curr_amt
FROM dual
UNION ALL
SELECT 1435177 qte_id, 14 seq_no, 59300 cmmt_curr_amt
FROM dual
SELECT qte_id, t2_seq_no, cmmt_curr_amt - cmmt_curr_lag diff
FROM
(SELECT t2.qte_id, t2.seq_no t2_seq_no, t1.seq_no t1_seq_no, cmmt_curr_amt, LAG(cmmt_curr_amt,1,0) OVER (PARTITION BY t2.qte_id ORDER BY t2.seq_no) cmmt_curr_lag
FROM table1 t1, table2 t2
WHERE t2.qte_id = t1.qte_id (+)
AND t2.seq_no = t1.seq_no(+)
ORDER BY 1,2
WHERE t1_seq_no IS NOT NULL
ORDER BY 1,2
QTE_ID T2_SEQ_NO DIFF
1435177 2 0
1435177 5 0
1435177 7 0
1435177 8 0
1435177 12 0
1435177 14 0 -
I need to denormalize data in sql query, please help!
With the query
select ref, start_time, end_time, person
from appointments
I get, eg:
REF START_TIME END_TIME PERSON
1234 10:00 11:00 USER1
1234 10:00 11:00 USER2
The users want to see it like this:
REF START_TIME END_TIME PERSON
1234 10:00 11:00 USER1, USER2
How do I do this just in sql?
cheers
Tracey.Apologies, my mistake, I forgot to connect by the ref as well...
(Note: you can ignore the CAST to VARCHAR2(40) as that just helped me get the formatted output.)
SQL> ed
Wrote file afiedt.buf
1 with t as (select 1234 as ref, '10:00' as start_time, '11:00' as end_time, 'USER1' as person from dual union all
2 select 1234, '10:00', '11:00', 'USER2' from dual union all
3 select 345, '11:00', '12:00', 'USER3' from dual)
4 -- END OF TEST DATA
5 select ref, start_time, end_time, CAST(MAX(LTRIM(SYS_CONNECT_BY_PATH(person,', '),', ')) AS VARCHAR2(40)) as users
6 from (select ref, start_time, end_time, person
7 ,row_number() over (partition by ref, start_time, end_time order by person) rn
8 from t) t
9 CONNECT BY rn = PRIOR rn+1 AND ref = PRIOR ref
10 START WITH rn = 1
11* GROUP BY ref, start_time, end_time
SQL> /
REF START END_T USERS
1234 10:00 11:00 USER1, USER2
345 11:00 12:00 USER3
SQL> -
SQL Query, please help very urgent
I am a newbie is sql.
I have two tables called test_master and test_detail.
Both tables contains pos_id as common field.
In test_detail got sub_id and to get the people under a given pos_id, I join with pos_id of test_master.
In the where condition, when I give the pos_id, it's returning only
the first level. How can I get the second level and get all the levels?
Any help is highly appreciable.It's very urgent.
Looking forward to hear from you.
Thanks
NewbieOk, I am pasting the description of the master and detail in the order.
Master
EMPLOYEE_NO VARCHAR2(30)
ORGANIZATION_ID NUMBER(15)
ORGANIZATION_NAME VARCHAR2(240)
POSITION_ID NUMBER(15)
POSITION_NAME VARCHAR2(240)
Detail
POSITION_ID NUMBER(15)
SUBORDINATE_ID NUMBER(15)
ORGANIZATION_ID NUMBER(15)
POSITION_NAME VARCHAR2(50)
Here is the sql, I want to get all the subordinates under a given position_id of the master.
Looking forward to hear from you.
select a.employee_no,a.POSITION_ID,a.POSITION_NAME,a.EMPLOYEE_NAME,
b.position_id,b.subordinate_id from portal_employee_master_test a,
portal_structure_test b
where a.POSITION_ID = b.POSITION_ID and a.POSITION_ID='xyz'
and b.position_ID=a.POSITION_ID -
I need sql query please help me out
I have two databases 1) erp 2) edusource
In erp database i have table master tables and columns like RecordId , value type ,Feildtext,description,Meaining,parentid etc
data stores based on value type like
1
Country
In
India
NULL
NULL
True
2
Country
Aus
Austrilia
NULL
NULL
True
3
Country
Usa
United States of America
NULL
NULL
True
4
Country
Uk
Great Britian
NULL
NULL
True
5
State
AP
Andhrapradesh
NULL
1
True
6
State
MH
Maharastha
NULL
1
True
7
State
TN
Tamilnadu
NULL
1
True
8
State
Sdy
Sydney
NULL
2
True
9
State
MEL
Melbourne
NULL
2
True
10
Location
IN-Hyd
Hyderabad
NULL
1
True
11
Location
In SEz
Sez-Hyderabad
NULL
1
True
NULL
NULL
NULL
NULL
NULL
NULL
NULL
for country and state we have a relation ship parentid and recordid
country
1 Country
In India
NULL
NULL 1
2 Country
Aus Austrilia
NULL
NULL 1
3 Country
Usa United States of America NULL
NULL 1
4 Country
Uk Great Britian
NULL
NUL 1
5 State
AndhraPradesh Ap
NULL 1
1
6 State
Maharastha MH
NULL 1
1
7 State
Tamilnadu TN
NULL 1
1
8 State
Sydney Sdy NULL
2 1
9 State Melbourne MEL
NULL 2
1
in edusource database i have location and columns like countryname,statename,feildtext,descriptiom,isactive
i need to insert data in edusource
database of tbl_ maplocation from that that databaseTry this out:
DECLARE @geog TABLE (ID INT, type VARCHAR(20), Abrev VARCHAR(6), name VARCHAR(30), parentID INT, Active VARCHAR(5))
INSERT INTO @geog (ID, type, Abrev, name, parentID, Active) VALUES
(1 , 'Country' ,'In ', 'India ', NULL, 'TRUE'),(2 , 'Country' ,'Aus ', 'Austrilia ', NULL, 'TRUE'),
(3 , 'Country' ,'Usa ', 'United States of America', NULL, 'TRUE'),(4 , 'Country' ,'Uk ', 'Great Britian ', NULL, 'TRUE'),
(5 , 'State' ,'AP ', 'Andhrapradesh ', 1 , 'TRUE'),(6 , 'State' ,'MH ', 'Maharastha ', 1 , 'TRUE'),
(7 , 'State' ,'TN ', 'Tamilnadu ', 1 , 'TRUE'),(8 , 'State' ,'Sdy ', 'Sydney ', 2 , 'TRUE'),
(9 , 'State' ,'MEL ', 'Melbourne ', 2 , 'TRUE'),(10 , 'Location' ,'IN-Hyd', 'Hyderabad ', 1 , 'TRUE'),
(11 , 'Location' ,'In SEz', 'Sez-Hyderabad ', 1 , 'TRUE')
SELECT g1.ID, COALESCE(g2.name,g1.name) AS countryName, g1.name AS stateName, g1.name AS fieldText, CASE WHEN COALESCE(g2.Active,'TRUE') = 'TRUE' AND g1.Active = 'TRUE' THEN 'TRUE' ELSE 'FALSE' END AS Active
FROM @geog g1
LEFT OUTER JOIN @geog g2
ON g1.parentID = g2.ID
AND g1.type IN ('location','state')
WHERE g1.Active = 'TRUE'
AND COALESCE(g2.Active,'TRUE') = 'TRUE'
I think you may want to further the relationship, and have 10 be the parent for 11, so you can get country, state, location. -
SQL experts please help for a query
I have following table1.
What query can give the result as given below, SQL experts please help on this.
TABLE1
Event DATETIME
in 2/JAN/2010
out 2/JAN/2010
in 13/JAN/2010
out 13/JAN/2010
in 5/JAN/2010
out 5/JAN/2010
RESULT REQUIRED FROM THE SQL QUERY
COL1_IN COL2_OUT
2/JAN/2010 2/JAN/2010
13/JAN/2010 13/JAN/2010
5/JAN/2010 5/JAN/2010I tried to help, but this puzzles me.
Why is this not returning pre-selected set of rows, why it's doing some merge join cartezian ?
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Prod
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Linux: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
SQL> select * from table1;
EVENT DATETIME
in 2/JAN/2010
out 2/JAN/2010
in 13/JAN/2010
out 13/JAN/2010
in 5/JAN/2010
out 5/JAN/2010
6 rows selected.
SQL> explain plan for
2 with a as
(select datetime from table1 where event='in'),
b as
(select datetime from table1 where event='out')
select a.datetime COL1_IN ,b.datetime COL2_OUT from a,b ;
Explained.
SQL> set wrap off
SQL> set linesize 200
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
Plan hash value: 185132177
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 9 | 288 | 8 (0)| 00:00:01 |
| 1 | MERGE JOIN CARTESIAN| | 9 | 288 | 8 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL | TABLE1 | 3 | 48 | 3 (0)| 00:00:01 |
| 3 | BUFFER SORT | | 3 | 48 | 5 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL | TABLE1 | 3 | 48 | 2 (0)| 00:00:01 |
PLAN_TABLE_OUTPUT
Predicate Information (identified by operation id):
2 - filter("EVENT"='in')
4 - filter("EVENT"='out')
Note
- dynamic sampling used for this statement
21 rows selected.
SQL> with a as
(select datetime from table1 where event='in'),
b as
(select datetime from table1 where event='out')
select a.datetime COL1_IN ,b.datetime COL2_OUT from a,b ;
COL1_IN COL2_OUT
2/JAN/2010 2/JAN/2010
2/JAN/2010 13/JAN/2010
2/JAN/2010 5/JAN/2010
13/JAN/2010 2/JAN/2010
13/JAN/2010 13/JAN/2010
13/JAN/2010 5/JAN/2010
5/JAN/2010 2/JAN/2010
5/JAN/2010 13/JAN/2010
5/JAN/2010 5/JAN/2010
9 rows selected.
SQL> -
How to improve the performance of the attached query, Please help
Hi,
How to improve performance of the below query, Please help. also attached explain plan -
SELECT Camp.Id,
rCam.AccountKey,
Camp.Id,
CamBilling.Cpm,
CamBilling.Cpc,
CamBilling.FlatRate,
Camp.CampaignKey,
Camp.AccountKey,
CamBilling.billoncontractedamount,
(SUM(rCam.Impressions) * 0.001 + SUM(rCam.Clickthrus)) AS GR,
rCam.AccountKey as AccountKey
FROM Campaign Camp, rCamSit rCam, CamBilling, Site xSite
WHERE Camp.AccountKey = rCam.AccountKey
AND Camp.AvCampaignKey = rCam.AvCampaignKey
AND Camp.AccountKey = CamBilling.AccountKey
AND Camp.CampaignKey = CamBilling.CampaignKey
AND rCam.AccountKey = xSite.AccountKey
AND rCam.AvSiteKey = xSite.AvSiteKey
AND rCam.RmWhen BETWEEN to_date('01-01-2009', 'DD-MM-YYYY') and
to_date('01-01-2011', 'DD-MM-YYYY')
GROUP By rCam.AccountKey,
Camp.Id,
CamBilling.Cpm,
CamBilling.Cpc,
CamBilling.FlatRate,
Camp.CampaignKey,
Camp.AccountKey,
CamBilling.billoncontractedamount
Explain Plan :-
Description Object_owner Object_name Cost Cardinality Bytes
SELECT STATEMENT, GOAL = ALL_ROWS 14 1 13
SORT AGGREGATE 1 13
VIEW GEMINI_REPORTING 14 1 13
HASH GROUP BY 14 1 103
NESTED LOOPS 13 1 103
HASH JOIN 12 1 85
TABLE ACCESS BY INDEX ROWID GEMINI_REPORTING RCAMSIT 2 4 100
NESTED LOOPS 9 5 325
HASH JOIN 7 1 40
SORT UNIQUE 2 1 18
TABLE ACCESS BY INDEX ROWID GEMINI_PRIMARY SITE 2 1 18
INDEX RANGE SCAN GEMINI_PRIMARY SITE_I0 1 1
TABLE ACCESS FULL GEMINI_PRIMARY SITE 3 27 594
INDEX RANGE SCAN GEMINI_REPORTING RCAMSIT_I 1 1 5
TABLE ACCESS FULL GEMINI_PRIMARY CAMPAIGN 3 127 2540
TABLE ACCESS BY INDEX ROWID GEMINI_PRIMARY CAMBILLING 1 1 18
INDEX UNIQUE SCAN GEMINI_PRIMARY CAMBILLING_U1 0 1Hello,
This has really nothing to do with the Oracle Forms product.
Please, send the SQL or/and PL/SQL questions in the corresponding forums.
Francois -
SQL query with parameter returns empty result set, please help !!!
Hi there,
When I use the following query :
<sql:query var="beroepsthemas" >
select *
from beroepsthemas
where beroepsthemaid = ?
<sql:param value="12"/>
</sql:query>
When I want to browse the result set with :
<c:forEach items="${beroepsthemas.rows}" var="rij">
it shows no records. But it must return at least one.
All my jsp pages with sql queries and parameters have the same problem.
This is all on my test environment. I'm using Ubuntu 5.10, Netbeans5.0, JDK 1.5_06, application runs in Bundeled Tomcat 5.5.9, MySQL 4.1.12, mysql-connector3.1.6
When the same code is run on the live environment, it works just fine.
The difference is :
Mysql 4.1.10a, tomcat5.5.9, mysql-connector3.1.6
What can there be wrong !!When the same code is run on the live environment, it
works just fine.
The difference is :
Mysql 4.1.10a, tomcat5.5.9, mysql-connector3.1.6
I didn't catch this. I think you may need to update the database driver. -
Passing user parameter into your sql query
I've created two user parameters...'Order_date_from' and 'Order_date_to'
I've written a simple query to list the total order amount, order number, party name and ordered date
Select sum ((unit_selling_price)*(ordered_quantity)), count(line_number), oe_order_headers_all.order_number, hz_parties.party_name, oe_order_headers_all.ordered_date
from oe_order_lines_all,
oe_order_headers_all,
hz_parties,
hz_cust_accounts
where oe_order_lines_all.header_id=oe_order_headers_all.header_id
and oe_order_headers_all.sold_to_org_id = hz_cust_accounts.cust_account_id
and hz_cust_accounts.party_id = hz_parties.party_id
group by order_number, party_name, ordered_date;
I want to know how I can pass the value for the above mentioned parameters into my sql query so that it reflects as the 'ordered_date' in the report and only shows the data for the dates that the user inputs.
Any feedback would be appreciated. Thanks.I want to know how I can pass the value for the above
mentioned parameters into my sql query so that it
reflects as the 'ordered_date' in the report and only
shows the data for the dates that the user inputs.
Any feedback would be appreciated. Thanks.And how does the "user" input these dates? -
Can't pass a parameter to an SQL Query
I've looked for about an hour or more now on the web, java.sun.com, but can't find what I need:
I have a page called display.jsp, and I call it from the browser
http://localhost/display.jsp?libnum=1
In the page I have the following code:
<sql:query var="profileQuery" dataSource="${profileDS}">
SELECT * FROM singles.profile where libnum=?
<sql:param value="${libnum}"/>
</sql:query>
However, the database sees the query as
" SELECT * FROM singles.profile where libnum=null"
So, my question is how do I make this line work ? :
" <sql:param value="${libnum}"/> "
can you help ?
thanks !
DennisDude .....
Here's my code now -- solved it... phew..
Just to think of it ... what a difference between "libnum" and "param.libnum". That was like 3-4, if not 5 hour difference for me.
<sql:query var="profileQuery" dataSource="${profileDS}">
SELECT * FROM singles.profile where libnum=?
<sql:param value="${param.libnum}"/>
</sql:query>
Dennis -
How to make recursive query.Please help
Dear Experts:
I want to retrieve all employees located in a department in addition to all other employees located in the child's nodes of this department too.
Problem Details:
I have "Employees" table and "Departments" Table
The structure of Dept Table is:
ID primary key
parent_dept foreign key(id)
deptname varchar2
deptType varchar2
The Employee table structure is
ID primary key
dept_id foreign key(Department.id)
empName varchar2Sample data for departments
ID : 1
parent_dept : null
deptname: General Manager office
deptType : 'GM'
ID :=2
parent_dept : 1
deptname: Information tech.
deptType : 'DPT'
ID :=3
parent_dept : 2
deptname: Software Development
deptType : 'SECTION'Sample Data for employees
ID : 101
dept_id :1
empName King
ID : 102
dept_id :2
empName ALAN
ID : 103
dept_id :2
empName SAM
ID : 104
dept_id :3
empName JANEI want to create a query that accepts a parameter "p_department_id" and returns All employees on the following conditions
1- In case the parameter value is null , then retrieve All Employees "king - alan- sam-jane"
2- In Case the parameter value is 1 , then retrieve all the employees under department id =1 in addition to all the employees located under the children departments.
in this case it will be "king - alan- sam-jane"
3- In case parameter value is 2 , then return all the employees under department id =2 in addition to all the employees located under the children departments.
In this case it will be " alan- sam-jane"
4- In case parameter value is 3 , then return all the employees under department id =3 in addition to all the employees located under the children departments.
in this case it will be only "JANE"
In brief , If I pass any value to the parameter :p_department_id , I want to retrieve all employees located in this department in addition to other employees located in the child's nodes of this department id
I use oracle database 11g release 2
Please help me
Thanks
Edited by: ta**** on Apr 3, 2013 5:56 PM
Edited by: ta**** on Apr 3, 2013 5:58 PMSQL> variable p_department_id number
SQL> exec :p_department_id := null
PL/SQL procedure successfully completed.
SQL> with employees as (
2 select 101 id,1 dept_id,'King' empName from dual union all
3 select 102,2,'ALAN' from dual union all
4 select 103,2,'SAM' from dual union all
5 select 104,3,'JANE' from dual
6 ),
7 departments as (
8 select 1 id,null parent_dept,'General Manager office' deptname,'GM' deptType from dual union all
9 select 2,1,'Information tech.','DPT' from dual union all
10 select 3,2,'Software Development','SECTION' from dual
11 )
12 select *
13 from employees
14 where dept_id in (
15 select id
16 from departments
17 start with (
18 (
19 :p_department_id is null
20 and
21 parent_dept is null
22 )
23 or
24 id = :p_department_id
25 )
26 connect by parent_dept = prior id
27 )
28 /
ID DEPT_ID EMPN
101 1 King
102 2 ALAN
103 2 SAM
104 3 JANE
SQL> exec :p_department_id := 1
PL/SQL procedure successfully completed.
SQL> /
ID DEPT_ID EMPN
101 1 King
102 2 ALAN
103 2 SAM
104 3 JANE
SQL> exec :p_department_id := 2
PL/SQL procedure successfully completed.
SQL> /
ID DEPT_ID EMPN
102 2 ALAN
103 2 SAM
104 3 JANE
SQL> exec :p_department_id := 3
PL/SQL procedure successfully completed.
SQL> /
ID DEPT_ID EMPN
104 3 JANE
SQL> SY. -
Pivot table query - PLEASE HELP
I have one requirement whichis similar like this.
I have following tables.
POSITIONS (one account holds multiple securities)
account_id security_id total
1 101 300
1 102 500
2 102 300
2 103 600
3 104 400
4 104 800
SECURITIES
security_id security_name country region
101 ABC USA NA
102 EFG USA NA
103 PQR GBR EUR
104 XYZ CANADA NA
I will get all the account numbers from front end application, for which I have to display output like this. For example I am getting accounts, 1,2,3 and 4 and have to display like columns at the end. I have to groupy by region and country.
REGION COUNTRY SECURITY_ID SECURITY_NAME 1 2 3 4
NA USA 101 ABC 300
102 EFG 500 300
COUNTRY TOTAL 800 300
CANADA 104 XYZ 400 800
REGION TOTAL 800 300 400 800
EUR GBR 103 PQR 600
COUNTRY TOTAL 600
REGION TOTAL 800 900 400 800
I am working on Oracle 9i Release 2 and front end is .net. Can anyone please help me out with a solution using a query or a stored proc. For Stored procedure, I should send refcurser as OUT parameter or any possible solution.
Thanks in advance.You could do the following:
In SQL
You can use the decode function and SUM over it.
Get the totals by region and country
select region,country, s.sec_id, sec_name,
sum(decode(acct_id,1,total,0)) a1,
sum(decode(acct_id,2,total,0)) a2,
sum(decode(acct_id,3,total,0)) a3,
sum(decode(acct_id,4,total,0)) a4
from positions p, securities s
where p.SEC_ID = s.SEC_ID
group by region,country, s.sec_id, sec_name
You can run similar sql to get totals by country or region separately ... just remove from select and group by clause the column name that you do not want to be displayed.
The limitation here is that you would need to write the decode statement for every account. Some programming may be needed here.
In PLSQL
create or replace procedure acct_sum (cr OUT sys_refcursor)
is
cursor c1 is
select distinct acct_id from positions order by 1 asc;
var1 varchar2(1000) := ' ';
var2 varchar2(1000);
begin
for i in c1 loop
var1 := var1 || ' sum(decode(acct_id, ' || i.acct_id || ', total,0) ),';
end loop;
var1 := substr(var1,1,length(var1)-1);
open cr for
'select region,country, s.sec_id, sec_name, ' || var1 ||
'from positions p, securities s
where p.SEC_ID = s.SEC_ID
group by region,country, s.sec_id, sec_name';
end acct_sum;
You may want to modify the code so pass in an array of accounts and loop through the content to get all the accounts.
Hope this helps
Shakti
http://www.impact-sol.com
Developers of Guggi Oracle - Tool for DBAs and Developers -
How to pass two parameters to sql query
I try to create a sql script to update two columns in one table. I want to set it as parameter. When people execute this sql script, they need to pass parameter into sql query, then query will be executed successfully. The problem is I am only able to pass one parameter. If set two parameters in one line code, it will get ORA-00933 errors. Please advice me where I was wrong. The code is simple and like this:
update MY_TABLE set year = &year AND month = &month where application_type = 'xxxx';
If I only have: update MY_TABLE set year = &year where application_type = 'xxxx'; It works. If I set two parameters to pass value. It will get error.Hi,
When you UPDATE two or more columns in the same statement, use ',' instead of 'AND' to separate them:
update MY_TABLE
set year = &year
, month = &month
where application_type = 'xxxx';The correct syntax for all SQL statements, including UPDATE, can be found in the [SQL Language manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_10008.htm#sthref9598].
Maybe you are looking for
-
Waiting for printer to become available
I'm on OS X 10.8.5, and have a USB printer: HP p4015dn. It used to work fine to print. All of a sudden, it shows "Waiting for printer to become available", forever, and will not print. I can successfully print to networked HP printers, but even th
-
How to add one more values in Search Criteria,
Hi All, OAF page Search Criteria : Search By "name"only Available already have in the search criteria .we are need to add Description in search criteria .How to add one more filed "Description" in Search Criteria, Please Help Me Thanks Rajavel
-
Trouble transferring iPhoto library from a time machine back up.
Hi! I recently upgraded to the new OS and decided to do a clean install as my mac book pro is running a little slow. I did a time machine back up to save my files. When i went to put my iphoto library back it would not work as i needed the newest ver
-
Need code sample to execute a command line in form builder
i need to write code in the fmb file to be executed when running a form
-
Camera Raw 7.3 Updating in PS CS6
How do I update my Camera Raw from 7.0 to 7.3? I have a Nikon D600 and need to import my RAW(NEF) files for editing. I tried searching the web and found no conclusion. It says to use the 'update mechanism in PS CS6', but I don't know what that means.