I have Sybase Statement which is mentioned below :
select s.airbill_nbr, s.orig, s.dest,p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name,s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt from shipment s, shipment_outbnd_stn o, payment p
where s.sa_shpmt_nbr = o.sa_shpmt_nbr
and o.sa_shpmt_nbr = p.sa_shpmt_nbr
and o.outbnd_date=convert(date,'01/29/2015',101)
and s.outbnd_empl_id is not null
and p.fop_cd = 'SW'
and p.swa_acct_nbr =40584
and s.void_flag is null
I have to create a prepared statement for the above code in Java and I have created the below One :
String variable1="29/01/2015";
psRetrieveAwbData = cattsDbc.prepareStatement( "select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = '"+ variable1 +"'" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );
but I dont know how to put date in date Format because the way I have put it is not working by taking the variable1, as variable1 It is taking as String and not finding out the Solution.Could you please tell me how to create the prepared statement where I can directly put date or put the date by storing it in some variable.
PreparedStatement pstmt = con.prepareStatement("select s.airbill_nbr, s.orig, s.dest, " +
"p.swa_acct_nbr, s.prod_type, s.outbnd_date, s.shppr_name, s.consgn_name, " +
"s.shipping_chrg, s.misc_chrg, s.decl_val_chrg, s.tax_amt, s.fuel_chrg, " +
"s.scrty_chrg, s.tot_chrgs, s.tot_pcs, s.tot_chrgbl_wt, s.tot_actl_wt " +
"from shipment s, shipment_outbnd_stn o, payment p " +
"where s.sa_shpmt_nbr = o.sa_shpmt_nbr " +
"and o.sa_shpmt_nbr = p.sa_shpmt_nbr " +
"and o.outbnd_date = ?" +
"and s.outbnd_empl_id is not null " +
"and p.fop_cd = 'SW' " +
"and p.swa_acct_nbr = " + iAccount +
"and s.void_flag is null" );");
pstmt.setDate (dt);
Related
I have this query, and I would like to pass the null value. How to do this using Preparement state
The column that must be null is E.RTCA_CD_QMNUM IN (:1)
String sqlSigiop = "SELECT E.RTCA_CD_QMNUM"
+ ", E.RTIT_NR_ITEM"
+ ", D.RTSI_NR_SUBITEM"
+ ", B.PLOP_DT_INICIO"
+ ", B.PLOP_DT_FECHAMENTO"
+ ", D.RTSI_DT_MAIS_CEDO"
+ ", A.SSRT_CD_STATUS_SUBITEM_RT"
+ ", D.RTSI_DT_MAIS_TARDE"
+ ", A.PORT_DT_INCLUSAO"
+ ", C.PLCO_DS_PLANEJ_CONFIG"
+ ", F.ROTA_NM_ROTA"
+ ", H.CLLO_NM_CLUSTER_LOG"
+ ", G.ATEM_DT_INICIO"
+ ", I.TIAM_DS_TIPO_ATEND_MAR "
+ "FROM SIGIOP.PLANEJAMENTO_OPERACIONAL_RT A "
+ "INNER JOIN SIGIOP.PLANEJAMENTO_OPERACIONAL B ON B.PLOP_SQ_PLANEJ_OPER = A.PLOP_SQ_PLANEJ_OPER "
+ "INNER JOIN SIGIOP.PLANEJAMENTO_CONFIG C ON C.PLCO_SQ_PLANEJ_CONFIG = B.PLCO_SQ_PLANEJ_CONFIG "
+ "INNER JOIN SIGIOP.RT_SUBITEM D ON D.RTSI_CD_RTSUBITEM = A.RTSI_CD_RTSUBITEM "
+ "INNER JOIN SIGIOP.RT_ITEM E ON E.RTIT_CD_RTITEM = D.RTIT_CD_RTITEM "
+ "INNER JOIN SIGIOP.ROTA F ON F.ROTA_SQ_ROTA = A.ROTA_SQ_ROTA "
+ "INNER JOIN SIGIOP.ATENDIMENTO_MAR G ON G.ATEM_SQ_ATEND_MAR = A.ATEM_SQ_ATEND_MAR "
+ "INNER JOIN SIGIOP.CLUSTER_LOG H ON H.CLLO_SQ_CLUSTER_LOG = G.CLLO_SQ_CLUSTER_LOG "
+ "INNER JOIN SIGIOP.TIPO_ATENDIMENTO_MAR I ON I.TIAM_SQ_TIPO_ATEND_MAR = G.TIAM_SQ_TIPO_ATEND_MAR "
+ "WHERE (B.PLOP_DT_FECHAMENTO >= TO_DATE('"+dataInicio+"', 'DDMMYYYY') OR B.PLOP_DT_FECHAMENTO IS NULL)"
+ "AND (B.PLOP_DT_FECHAMENTO < TO_DATE('"+dataFim+"', 'DDMMYYYY') OR B.PLOP_DT_FECHAMENTO IS NULL) "
+ "AND A.MORE_SQ_MOTIVO_REPLANEJ IS NULL "
+ "AND B.PLOP_IN_STATUS = 2 "
+ "AND E.RTCA_CD_QMNUM IN (:1) "
+ "ORDER BY E.RTCA_CD_QMNUM, E.RTIT_NR_ITEM, D.RTSI_NR_SUBITEM, B.PLOP_DT_INICIO";
/
Statement stSigiop = cnnSigiop.createStatement();
ResultSet resultSigiop = stSigiop.executeQuery(sqlSigiop.replace(nullable ":1", rts.toString().replace("[", "").replace("]", "")));
while (resultSigiop.next()) {TO DO}
you have to change your query to this :
...
+ "AND (E.RTCA_CD_QMNUM IS NULL OR E.RTCA_CD_QMNUM IN (:1)) "
...
hi guys I tried to make query on java for access data from database but when I compile the error is syntax error at or near "v", when I try to compile my query on postgresql it work and the data can show, but when I compile on eclipse it can't
this the query
#Override
protected StringBuffer buildQuery(String sql, Object... objects) {
StringBuffer sbSQL = new StringBuffer("SELECT a.master_user_id, b.name, d.group_name, a.role, "
+ "(SELECT count (v.id) FROM project_member_dtl v "
+ "LEFT JOIN project_timeline p ON v.project_timeline_id = p.id "
+ "LEFT JOIN master_project_dtl j ON p.master_project_dtl_rid = j.rid "
+ "LEFT JOIN master_project aa ON j.master_project_sid = aa.sid "
+ "where ((v.member_plan_date > now()) AND (v.member_real_date IS NULL)) "
+ "AND j.is_freeze = 'n' "
+ "AND p.is_active = 'y' "
+ "AND aa.is_deleted = 'n' "
+ "AND v.master_user_id = a.master_user_id"
+ "AND v.master_role_id = a.master_role_id ) as in_progress, "
+ "(SELECT count (w.id) FROM project_member_dtl w "
+ "LEFT JOIN project_timeline q ON w.project_timeline_id = q.id "
+ "LEFT JOIN master_project_dtl k ON q.master_project_dtl_rid = k.rid "
+ "LEFT JOIN master_project bb ON k.master_project_sid = bb.sid "
+ "where ((w.member_plan_date < now()) "
+ "AND (w.member_real_date IS NULL)) "
+ "AND k.is_freeze = 'n' "
+ "AND q.is_active = 'y' "
+ "AND bb.is_deleted = 'n' "
+ "AND w.master_user_id = a.master_user_id "
+ "AND w.master_role_id = a.master_role_id ) as in_progress_late, "
+ "(SELECT count (x.id) FROM project_member_dtl x "
+ "LEFT JOIN project_timeline r ON x.project_timeline_id = r.id "
+ "LEFT JOIN master_project_dtl l ON r.master_project_dtl_rid = l.rid "
+ "LEFT JOIN master_project cc ON l.master_project_sid = cc.sid "
+ "where (x.member_plan_date IS NULL) "
+ "AND l.is_freeze = 'n' AND r.is_active = 'y' "
+ "AND cc.is_deleted = 'n' "
+ "AND x.master_user_id = a.master_user_id "
+ "AND x.master_role_id = a.master_role_id ) as waiting_list, "
+ "(SELECT count (y.id) FROM project_member_dtl y"
+ "LEFT JOIN project_timeline s ON y.project_timeline_id = s.id "
+ "LEFT JOIN master_project_dtl m ON s.master_project_dtl_rid = m.rid "
+ "LEFT JOIN master_project dd ON m.master_project_sid = dd.sid "
+ "where (y.member_plan_date > y.member_real_date) "
+ "AND m.is_freeze = 'n' AND s.is_active = 'y' "
+ "AND dd.is_deleted = 'n' "
+ "AND y.master_user_id = a.master_user_id "
+ "AND y.master_role_id = a.master_role_id ) as finish_on_time, "
+ "(SELECT count (z.id) FROM project_member_dtl z "
+ "LEFT JOIN project_timeline t ON z.project_timeline_id = t.id"
+ "LEFT JOIN master_project_dtl n ON t.master_project_dtl_rid = n.rid"
+ "LEFT JOIN master_project ee ON n.master_project_sid = ee.sid"
+ "where (z.member_plan_date < z.member_real_date)"
+ "AND n.is_freeze = 'n' AND t.is_active = 'y'"
+ "AND ee.is_deleted = 'n' "
+ "AND z.master_user_id = a.master_user_id "
+ "AND z.master_role_id = a.master_role_id ) as finish_over_time"
+ "FROM project_member_dtl a");
sbSQL.append("LEFT JOIN master_user b ON a.master_user_id = b.id ");
sbSQL.append("LEFT JOIN user_group c ON a.master_user_id = c.master_user_id ");
sbSQL.append("LEFT JOIN master_group d ON c.group_id = d.id ");
sbSQL.append(sql);
return sbSQL;
}
thanks
It's a typo. Make sure you keep a space at the end of each line
+"AND v.master_user_id = a.master_user_id "
In the first query you missed a space
+"AND v.master_user_id = a.master_user_id"
+ "AND v.master_role_id = a.master_role_id ) as in_progress, "
Now the query would be
...AND v.master_user_id = a.master_user_idAND v.master_role_id =....
SQL engine thinks that a.master_user_idAND is a column. So the next v is a problem. That's why the error
syntax error at or near "v"
I am building a web portal through android and a query i am running through JDBC drivers is returning 0 where data should not be zero.
This is the query:
ResultSet set = statement.executeQuery("select it.itcod, it.itnam, it.packn, it.tradp, " +
"sum(nvl(itd.slbox,0) - nvl(itd.srbox,0) - nvl(itd.brbox,0) - nvl(itd.gsbox,0)) as sbox, " +
"sum(nvl(itd.slbbx,0) - nvl(itd.srbbx,0) - nvl(itd.brbbx,0) - nvl(itd.gsbbx,0)) as sbbx, " +
"SUM(NVL(itd.PRBOX,0) - NVL(itd.RPBOX,0) - NVL(itd.TRBOX,0)) as pbox, " +
"SUM(NVL(itd.PRBBX,0) - NVL(itd.RPBBX,0) - NVL(itd.TRBBX,0)) as pbbx " +
"from items it " +
"LEFT join item_daily itd " +
"on (it.cocod = itd.cocod " +
"and it.itcod = itd.itcod " +
"and ITD.ddate between " + fdate + " and " + tdate + ")" +
"WHERE IT.COCOD = " + COCOD +
"AND IT.DCODE = " + DCODE +
"AND NVL(IT.FREZE,'N')!='Y' " +
"group by it.cocod, it.itcod, it.itnam, it.packn, " +
" it.tradp, it.pkqty, it.dcode, it.freze, " +
" it.ishow, it.sltax, it.dcont, it.mcode, " +
" it.nwcod " +
"order by itnam ");
I have tried using resultsetmetadata but that does not work either.
you should be careful to this line
+ fdate + " and " + tdate
because you should use like this:
" .. to_date('"+fdate+"','***your_date_format') and to_date('"+fdate+"','***your_date_format')"
I have PreparedStatement like this:
PreparedStatement st = conn
.prepareStatement("Select * from "
+ "(select Count(*) from uch_otstyp b,prov_uch p "
+ "where b.\"ID_Poezd\"=?"
+ "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
+ "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
+ "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
+ "and b.\"ID_Uch\"=p.\"ID_Uch\" "
+ "and p.\"MES\"=? "
+ "and p.\"GOD\"=? "
+ "and p.\"Nput\"=? "
+ "and b.\"Kod_Otstup\"=? "
+ "and b.\"DEPTH\"<1),"
+ ""
+ "(select Count(*) from uch_otstyp b,prov_uch p "
+ "where b.\"ID_Poezd\"=?"
+ "and (b.\"NachKm\"*1000+b.\"NachM\")>? "
+ "and (b.\"NachKm\"*1000+b.\"NachM\")<=? "
+ "and b.\"ID_Poezd\"=p.\"ID_Poezd\" "
+ "and b.\"ID_Uch\"=p.\"ID_Uch\" "
+ "and p.\"MES\"=? "
+ "and p.\"GOD\"=? "
+ "and p.\"Nput\"=? "
+ "and b.\"Kod_Otstup\"=? "
+ "and b.\"DEPTH\">=1)"
+ "and b.\"DEPTH\"<2)");
Parameter values of each subquery are identic. How can I set parameters only for one subquery and automaticaly fill parameters of another(not for each subquery)?
You could use named parameters, so that when a parameter appears multiple times in a query you would only have to specify it once. JDBC doesn't support named parameters, but you can write code to find the parameter names in a SQL string and figure out the order they appear so that the arguments can be added to the PreparedStatement. Spring-Jdbc does this for you with the NamedParameterJdbcTemplate, here's an example. This is in core spring, the package that implements the parameter-munging is org.springframework.jdbc.core.namedparam.
Basically, I have a table called ratings with 3 columns FILM, EMAIL, and RATING in mysql.
I can get back the average rating for all the films, but I must then display the ratings
of the movies that I haven't already rated , so I must only display all the films everybody else rated except me.
String email = Main_Login.accounttext.getText().trim();
statement = (PreparedStatement) con
.prepareStatement(""
+ "SELECT FILM,(film_total_ratings/number_of_ratings) as ratings_rating "
+ "FROM( "
+ "SELECT COUNT(*) as number_of_ratings, FILM, "
+ " SUM(RATING) as film_total_ratings "
+ " FROM ratings GROUP BY film "
+ "ORDER BY rating DESC"
+ ") TMP_Film");
result = (ResultSet) statement.executeQuery();
int i = 0;
while (result.next() && i <= 4) {
i++;
// if (result.getString(1).contains(email)) {
System.out.println((i) + ")" + " " + "[Movies You Might Like]"
+ " " + result.getString(1) + " " + "[Rating]" + " "
+ result.getString(2));
You may try to use CASE to add a column indicating that movie has been rated by the respective email or not. This is not tested but you may get the concept:
statement = (PreparedStatement) con
.prepareStatement(""
+ "SELECT FILM,(film_total_ratings/number_of_ratings) as ratings_rating "
+ "FROM( "
+ "SELECT COUNT(*) as number_of_ratings, FILM, "
+ " SUM(RATING) as film_total_ratings, "
+ " SUM(CASE WHEN EMAIL LIKE '%"+email+"%' THEN 1 ELSE 0 END) as rated"
+ " FROM ratings GROUP BY film HAVING rated=0 "
+ "ORDER BY rating DESC"
+ ") TMP_Film");